Friday, 4 October 2013

Count the Running instance of a process




howmanyareRunning("notepad.exe")

Function howmanyareRunning(iprocessName)


Dim objWMIService, objProcess, colProcess, strComputer, processName, instances

strComputer = "."
instances = 0
processName = iprocessName '"firefox.exe"

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

Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process")

For Each objProcess in colProcess
If objProcess.Name = processName Then instances = instances + 1
Next

If processName = "wscript.exe" Then
    If instances = 1 Then
    WScript.Echo "There is no other VBScript running except this one!"
    Else
    WScript.Echo "There are currently " & "(" & instances _
       & ") " & "VBScript" & " Instances running!"
    End If
Else
    WScript.Echo "There are currently " & "(" & instances & ") " & _
            """" & processName & """" & " Instances running!"
End If

End Function


OUTPUT
=======


Sunday, 8 September 2013

Get Files list from a folder

OUTPUT


 
Function GetFileNamesfromFolder(sFolder)

    Dim filenames
    Dim fso, folder,files
    Dim firstfile
    On Error Resume Next

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder(sFolder)
    Set files = folder.Files
    
     firstfile =true 
      For each folderIdx In files
   
       if firstfile then
        filenames =folderIdx.Name
        firstfile =false
       else
           filenames = filenames & "," & folderIdx.Name
       End If
        'msgbox filenames 
      Next
    GetFileNamesfromFolder=filenames

End Function

Example: 


AllFiles =GetFileNamesfromFolder("c:\") 
  msgbox AllFiles
 msgbox Replace(AllFiles,",",vbcrlf)


OUTPUT
=======
All file names in comma seperated format

Further:
you can use split to spilt all names into array

AllFiles =GetFileNamesfromFolder("c:\") ArrayOfFiles = Split(AllFiles,",")

Thursday, 22 August 2013

Send Mail from MS Outlook

Function SendMailFromOutlook(sMailto,sMailCC,sSubject,SBody,sAttachment,MyName)
 
 
        set oMailobj=CreateObject("Outlook.Application")
        
        set oSendmail=oMailobj.CreateItem(0)
        oSendmail.To=sMailto
        oSendmail.Subject=sSubject
        oSendmail.CC=sMailCC
        oSendmail.HTMLBody = sBody & "<br><br><b> -Regards</b><br>" & MyName
         If (sAttachment <> "") Then
                 oSendmail.Attachments.Add(sAttachment)
              End If
 
        oSendmail.Send
        set oSendmail=Nothing
        set oMailobj=Nothing
End Function

Example: Call the above Function with the values
(1)
sMailto="rohan@gmail.com;sameep@yahoo.com"
sMailCC="richa@gmai.com;"
sSubject="Party at home"
sBody=" be there at 10"
sAttachment="c:\partyimage.jpeg"
MyName="Mitesh"

Call SendMailFromOutlook(sMailto,sMailCC,sSubject,SBody,sAttachment,MyName)

(2) Or you can call this function by using inline values

Call SendMailFromOutlook("mitesh@gmai.com","","mysubject","how are you","","Rohan")



OUTPUT
=======
 Mail will be sent from Outlook in your machine