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

Tuesday 28 May 2013

EXCEL - Get Values from a Cell by matching values from some other cell in same row


I found that there is a Vlookup function in Excel which returns the value from a column by comparing values from some other column. Followinf function also works like that. User have to enter following values
 
myfile = Excel File path

SheetName = Excel Sheet Name

Columnametocheck = Name of the source column

valtoCheck = Value to match

ColumnNameforReturnValues =   Will return the value from this coulmn


Ex.
getCellValues (myfile,mySheetname, "myColumn1", "B","myColumn3")
will search "B" in mycolumn1  and return the correspoding value from myColumn3 that is "2"

myColumn1
myColumn2
myColumn3
A
X
1
B
Y
2
C
Z
3



Function getCellValues (Excelfile,Sheetname, Columnametocheck, valtocheck, ColumnNameforReturnValues )
    returnvalue =""

   '--connecting Excel---
    set bdayExcel  =CreateObject("Excel.Application")
    bdayExcel.Visible = true
    bdayExcel.Workbooks.Open(Excelfile)

    '---connect with sheet --
    Set xlSheet =bdayExcel.Sheets(Sheetname)

   '-- get SourceColumn Number--
    colnumber = xlSheet.Range("A1:z7").find(Columnametocheck).column

   ' ---- Get Return Coulumn Number---
    returncolnumber = xlSheet.Range("A1:z7").find(ColumnNameforReturnValues).column


'--LOGIC---
    UsedRows= xlSheet.UsedRange.rows.count

    for i = 1 to usedRows
        val=xlSheet.Cells(i,colnumber).value
     
        if val = valtocheck then

            if len(returnvalue) = 0 then
                returnvalue = xlSheet.Cells(i,returncolnumber).value
             Else
                returnvalue =  returnvalue & "," & xlSheet.Cells(i,returncolnumber).value
            End If
        End IF
    Next


'---return found value---
getRowNumbers = returnvalue

End Function

Monday 20 May 2013

Print some charaters from a string

'Set printlength for total number of character to be displayed
printLength = 4

'Source String 
myString ="12345678ABCDEFGHIJ"

'Logic
stringLength = len(myString)

for i =0 to stringLength / printLength
     myAns = myAns & vbcrlf & mid(myString  ,i*printLength +1,printLength)
next
msgbox myAns


OUTPUT -  message box with these output
=======
1234 
5678
ABCD
EFGH
IJ

Lock Current Machine

'Create Shell Object
Set wsh= CreateObject("Wscript.Shell")


'Run user32.dll with argument Lockworkstation
wsh.run "rundll32.exe user32.dll, LockWorkStation"

Output
======

- Machine will be locked

Getting Emailid and Username from Outlook



'Creating outlook application object
Set olapp=createobject("Outlook.application") 

'Get Namespace
Set objNS =olApp.GetNamespace("MAPI")  


'Current EMail ID 
msgbox objNS.Session.CurrentUser.AddressEntry.GetExchangeUser.PrimarySmtpAddress

'Current UserName
msgbox objNS.Session.CurrentUser


Tuesday 14 May 2013

VBSCRIPT - GET LOGIN USERNAME

'Create Shell Object
Set shell = CreateObject("wscript.shell")

'Execute command Prompt(DOS) to get current username

Set oExec = shell.Exec("cmd.exe /c echo %username%")

'Print All output
msgbox oExec.Stdout.ReadAll
 
set oExec =nothing
set shell = nothing



OUTPUT
=======
 Current username will get displayed

VBSCRIPT - Get every information about each process




MAIN CODE

On Error Resume Next
strComputer = "."

' ---CREATE OBJECTS----
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Process",,48)

'---GET TITLES OF ALL PROCESS AND DISPLAY---
Titles=""
For Each objItem in colItems    
Titles = Title1 & UCASE(objItem.Caption) & " , "
Next
msg = vbtab & "Process List" & vbcrlf & "-----------------------------------" & vbcrlf & Titles
msgbox msg
msg =""

'--- DISPLAY PROCESS DETAILS 1 BY 1 ----
Set colItems = objWMIService.ExecQuery("Select * from Win32_Process",,48)
For Each objItem in colItems    
msg = "Process : " & UCASE(objItem.Caption)
   msg = msg & vbcrlf & "--------------------------------------------------------------"
msg = msg & vbcrlf &  "CommandLine: " & objItem.CommandLine    
msg = msg & vbcrlf &  "CreationClassName: " & objItem.CreationClassName    
msg = msg & vbcrlf &  "CreationDate: " & objItem.CreationDate    
msg = msg & vbcrlf &  "CSCreationClassName: " & objItem.CSCreationClassName    
msg = msg & vbcrlf & "CSName: " & objItem.CSName    
msg = msg & vbcrlf & "Description: " & objItem.Description    
msg = msg & vbcrlf & "ExecutablePath: " & objItem.ExecutablePath    
msg = msg & vbcrlf & "ExecutionState: " & objItem.ExecutionState    
msg = msg & vbcrlf & "Handle: " & objItem.Handle    
msg = msg & vbcrlf & "HandleCount: " & objItem.HandleCount 
msg = msg & vbcrlf & "InstallDate: " & objItem.InstallDate    
msg = msg & vbcrlf & "KernelModeTime: " & objItem.KernelModeTime    
msg = msg & vbcrlf & "MaximumWorkingSetSize: " & objItem.MaximumWorkingSetSize    
msg = msg & vbcrlf & "MinimumWorkingSetSize: " & objItem.MinimumWorkingSetSize    
a= msgbox(msg & vbcrlf & vbcrlf & vbtab & "......continue with next process....?",vbYESNO)
if a= VBNO then
Exit For
End IF
Next

CODE  TO GET MORE INFORMATION

Wscript.Echo "Name: " & objItem.Name
    Wscript.Echo "OSCreationClassName: " & objItem.OSCreationClassName
    Wscript.Echo "OSName: " & objItem.OSName
    Wscript.Echo "OtherOperationCount: " & objItem.OtherOperationCount
    Wscript.Echo "OtherTransferCount: " & objItem.OtherTransferCount
    Wscript.Echo "PageFaults: " & objItem.PageFaults
    Wscript.Echo "PageFileUsage: " & objItem.PageFileUsage
    Wscript.Echo "ParentProcessId: " & objItem.ParentProcessId
    Wscript.Echo "PeakPageFileUsage: " & objItem.PeakPageFileUsage
    Wscript.Echo "PeakVirtualSize: " & objItem.PeakVirtualSize
    Wscript.Echo "PeakWorkingSetSize: " & objItem.PeakWorkingSetSize
    Wscript.Echo "Priority: " & objItem.Priority
    Wscript.Echo "PrivatePageCount: " & objItem.PrivatePageCount
    Wscript.Echo "ProcessId: " & objItem.ProcessId
    Wscript.Echo "QuotaNonPagedPoolUsage: " & objItem.QuotaNonPagedPoolUsage
    Wscript.Echo "QuotaPagedPoolUsage: " & objItem.QuotaPagedPoolUsage
    Wscript.Echo "QuotaPeakNonPagedPoolUsage: " & objItem.QuotaPeakNonPagedPoolUsage
    Wscript.Echo "QuotaPeakPagedPoolUsage: " & objItem.QuotaPeakPagedPoolUsage
    Wscript.Echo "ReadOperationCount: " & objItem.ReadOperationCount
    Wscript.Echo "ReadTransferCount: " & objItem.ReadTransferCount
    Wscript.Echo "SessionId: " & objItem.SessionId
    Wscript.Echo "Status: " & objItem.Status
    Wscript.Echo "TerminationDate: " & objItem.TerminationDate
    Wscript.Echo "ThreadCount: " & objItem.ThreadCount
    Wscript.Echo "UserModeTime: " & objItem.UserModeTime
    Wscript.Echo "VirtualSize: " & objItem.VirtualSize
    Wscript.Echo "WindowsVersion: " & objItem.WindowsVersion
    Wscript.Echo "WorkingSetSize: " & objItem.WorkingSetSize
    Wscript.Echo "WriteOperationCount: " & objItem.WriteOperationCount
    Wscript.Echo "WriteTransferCount: " & objItem.WriteTransferCount