Constantly look for file, when file exist, run command

淺唱寂寞╮ 提交于 2019-12-08 11:17:05

问题


I need a vbscript that monitors a folder for a specific file, when file is found it needs to execute a command then delete that file but continue to monitor the folder again for the same file incase it needs to run again.

This...

Set FSO = CreateObject("Scripting.FileSystemObject")
Do While 1>0
   If FSO.FileExists (file.txt) Then 
       FSO.DeleteFile (file.txt)
       CreateObject("WScript.Shell").Run "c:\windows\notepad.exe"
   End If
   WScript.Sleep 1000
Loop

Gave me an "object required: file" error.

update, this worked...

FileName = "c:\vbscript\cat.txt"
Set FSO = CreateObject("Scripting.FileSystemObject")
Do
   If FSO.FileExists(FileName) Then 
       FSO.DeleteFile FileName
       CreateObject("WScript.Shell").Run "c:\windows\notepad.exe"
   End If
   WScript.Sleep 1000
Loop

回答1:


Simply create a script that infinitely loops, testing for file existence and if it does delete it.

FileName = "Path\To\FileName"
Set FSO = CreateObject("Scripting.FileSystemObject")
Do
   If FSO.FileExists(FileName) Then 
       FSO.DeleteFile FileName
   End If
   WScript.Sleep 1000
Loop


来源:https://stackoverflow.com/questions/16341402/constantly-look-for-file-when-file-exist-run-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!