Wednesday, January 4, 2012

How to create a list of dates in a table - SQL SERVER

Many time we need a table with all the dates listed in it to confront with some other table which has missing dates. here is the simple method - SQL SERVER
DECLARE @myTable TABLE ( TheDate datetime )

declare @StartDate datetime

declare @Days int

declare @CurrentDay int

set @StartDate = '1/1/2006'

set @Days = 10

set @CurrentDay = 0

while @CurrentDay < @Days

begin

insert @myTable (TheDate) values (dateadd(dd, @CurrentDay, @StartDate))

set @CurrentDay = @CurrentDay + 1

end

select * from @myTable

Wednesday, November 30, 2011

To check if any process is running on the local or a remote computer.

Many times with automation we need to find if a particular process is running on the pc. Then we need to kill it, for example a scheduling script..

here is a sample code to see if the process is running and then kill it-
option explicit
Dim strComputer,strProcess, colProcess, runningProcess
Dim objWMIService, strWMIQuery

strComputer = "."
strProcessName = "calc.exe"


strWMIQuery = "Select * from Win32_Process where name like '" & strProcessName & "'"

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")

Set colProcess = objWMIService.ExecQuery (strWMIQuery)

' Suppose i want to terminate all these process
For Each runningProcess in colProcess
runningProcess.Terminate()
Next

Thursday, October 27, 2011

PHP to CSV simplest way

Many times we need to download a CSV file from database using PHP. Here is a simple method..

Step 1: Prepare Query.

$query="SELECT * FROM TABLEA";

Step 2 : Fetch data from database using above query.
$result= $Dbase->execute($query);

Remember in above i using my own function coded by me. but you can use mysql_fetch_assoc or mysql_fetch_array for the data fetch.

Now prepare the header to put a CSV for download -

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=myfile.csv");
header("Pragma: no-cache");
header("Expires: 0");


myfile is semicolon (; seperated values with a new line for end of file) csv file. I am preparing it as follows

Now Write in this file using simple ECHO - (Header Column)
echo "COLUMN-A; COLUMN-B; COLUMN-C; \n";

Now the values (Data values)
for( $i=0; $i<=Count($result)-1; $i++ ) { echo $result[$i][0].";".$result[$i][1].";".$result[$i][2]."; \n"; }

at the end you have a CSV ready (simple and clear).

Tuesday, October 25, 2011

Sending mail with CDO Message

If you need to send an email from EXCEL or VBscript here is the simple method

' To Send mail
Dim objmessage
Set objmessage = CreateObject("CDO.Message")
objmessage.Subject = "NOTIFICATION EMAIL ----"
objmessage.From = "FROM MAIL FIELD"
objmessage.To = "EMAIL TO WHICH YOU NEED TO SEND"
objmessage.TextBody = "ATTENTION YOU HAVE GOTTA MAIL " & vbCrLf &SOMESTRING
objmessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objmessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "YOURSMTPSERVER"
objmessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objmessage.Configuration.Fields.Update
objmessage.Send

with above you can send mail easily and decide also what to send in "TextBody" field. In the configuration field "sendusing" has 3 Enum values
1 - Send using Pickup Folder
2 - Send using Port (default is 25)
3 - Send using Exchange

If your smtp server requires authentication you need to add "authentication" in configuration fields with username and password, also you can use SSL. Here is a simple example how (remember all must be written before Fields.Update..

http://msdn.microsoft.com/en-us/library/ms873029%28v=EXCHG.65%29.aspx

Monday, October 24, 2011

Copy files into another folder using vbscript

Here is a VBScript to copy files (maybe only specific extension type or ALL) to another folder.

Dim sOriginFolder, sDestinationFolder, sFile, oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
sOriginFolder = "C:\Reports"
sDestinationFolder = "\\10.10.10.100\Report"
For Each sFile In oFSO.GetFolder(sOriginFolder).Files
'msgbox(oFSO.GetExtensionName(sFile))
If UCASE(oFSO.GetExtensionName(sFile)) = "PDF" Then
oFSO.GetFile(sFile).Copy sDestinationFolder & "\" & oFSO.GetFileName(sFile),True
'Wscript.echo "Copying : " & Chr(34) & oFSO.GetFileName(sFile) & Chr(34) & " to " & sDestinationFolder)
End If
Next

In above i am copying only PDF files we can put wildcard or select multiple types. If we want to launch this operation we can call this vbs in a bat file and the bat file can be put in a "scheduled operation" which can RUN every X minutes/hours.

If you need to run on a specific condition like change of file etc please look here
http://blogs.technet.com/b/heyscriptingguy/archive/2004/10/11/how-can-i-automatically-run-a-script-any-time-a-file-is-added-to-a-folder.aspx

Wednesday, October 19, 2011

Free charting library

I am using http://teethgrinder.co.uk/open-flash-chart/ for my projects. it seems promising and easily customizable. You can do a lot of things with innovative interface. please check it..

You can use this from PHP/ASP/JSP..its so simple and promising. There is also a JSON version available.

Thursday, October 13, 2011

Bug in Google? or a Feature

If you have a google id with [dot] like ABC.XYZ@gmail.com read as ABC[DOT]XYZ@gmail.com
Try to put your username as ABCXYZ@gmail.com and the password.... google works fine. It seems for google there is no difference in username with [dot] and without [dot]

Strange but true ! ;) comments??