I want to make a small VBS script which tells user if file is in use or not. I have one file and if this file is in use VBS should give me a message that file is in use. If file is not in use by any process, VBS should give me message that file not in use. I tried this but nothing works.
Asked
Active
Viewed 7,692 times
5
-
The KB article has sample code for VBA, which is the macro language of Microsoft Office. VBScript is different from VBA. – Ansgar Wiechers Mar 10 '13 at 19:16
-
@Fesiitis - Can you become more specific? – Panayot Karabakalov Mar 10 '13 at 21:53
-
I want create VBS file and put it into same folder with one my media file. When I open media file and then this VBS, VBS needs to show message box with text that media file is in use by somebody process (no need to show what is this process). But if I open only this VBS, not media, then VBS shows me message box with text that media file is not in use by somebody process. – Mar 10 '13 at 22:52
-
https://stackoverflow.com/questions/12300678/how-can-i-determine-if-a-file-is-locked-using-vbs This answer worked for me. – Jay Brown May 14 '20 at 16:45
1 Answers
9
You could try with a WMI query:
filename = "..."
Set wmi = GetObject("winmgmts://./root/cimv2")
qry = "SELECT * FROM Win32_Process WHERE CommandLine LIKE '%" & filename & "%'"
For Each p In wmi.ExecQuery(qry)
WScript.Echo "Media file in use."
WScript.Quit 0
Next
WScript.Echo "Media file not in use."

Ansgar Wiechers
- 193,178
- 25
- 254
- 328