Torgeir
Mon May 30 07:28:49 CDT 2005
karthick.ramachandran@gmail.com wrote:
> Hi!
>
> Say I have a file called c:\foobar.txt in my harddisk. I want to check
> using vbscript whether that particular file is currently opened using
> notepad or not. How will I acheive it?
Hi
As long as the following requirements are met, the script below will
work:
1) Script is running on WinXP or Win2k3 Server
(the Win32_Process.CommandLine property is not implemented
for Win2k)
2) The file c:\foobar.txt is opened as part of the Notepad launch
(and not with the File\Open... menu in Notepad, or not by dragging
and dropping the file onto an existing Notepad instance)
'--------------------8<----------------------
sProcessName = "notepad.exe"
sTextFile = "c:\foobar.txt"
sComputer = "." ' use "." for local computer
Set oWmi = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& sComputer & "\root\cimv2")
Set colProcessList = oWmi.ExecQuery _
("Select * from Win32_Process Where Name = '" & sProcessName & "'")
bolFound = False ' init value
For Each oProcess In colProcessList
If InStr(1, oProcess.CommandLine, sTextFile, vbTextCompare) > 0 Then
bolFound = True
' value found, abort the process enumeration
Exit For
End If
Next
If bolFound Then
WScript.Echo sTextFile & " is open in Notepad"
Else
WScript.Echo sTextFile & " is not open in Notepad"
End If
'--------------------8<----------------------
--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx