VB脚本:如果文件存在则运行,如果不存在则结束

时间:2012-03-22 10:05:54

标签: file vbscript operating-system

我希望在Windows 7计算机上使用脚本来禁用文件(REAgentc.exe),如果它不存在(即在XP计算机上),则结束。以下是我到目前为止所得到的,但任何人都可以协助我实现我追求的其余内容吗?我对VB脚本的了解确实不是很好,但是非常感谢任何可以提供的帮助,谢谢。

'Declare Variables
Dim strApp 
Dim arrPath
Dim strPath
Dim strAppPath

' main if statement to run the script and call functions and sub's
If (CheckRegistryForValue)= True Then
   'msgbox( strPath & " I am here")
   WScript.Quit (0)
Else 
   RunCommand
   WriteRegkey
   WScript.Quit (0)
End If 

'Sub to run the REAgent disable command
Sub RunCommand
   Set objShell = CreateObject("Wscript.Shell")
   strApp = "C:\Windows\System32\REAgentc.exe /disable"
   arrPath = Split(strApp, "\")

   For i = 0 To Ubound(arrPath) - 1
     strAppPath = strAppPath & arrPath(i) & "\"
   Next 

   objShell.CurrentDirectory = strAppPath
   objShell.Run(strApp)
End Sub

'Function to check registry for value, Return check registry for value
Function CheckRegistryForValue
   Set WshShell = WScript.CreateObject("WScript.Shell")
   On Error Resume Next
   dong = wshShell.RegRead ("HKLM\SOFTWARE\REAgent\")
   If (Err.Number <> 0) Then
      CheckRegistryForValue = False
   Else 
      CheckRegistryForValue = True
   End If
End Function

' sub to write registery key to flag computers that the script has run On
Sub WriteRegkey
   HKEY_LOCAL_MACHINE = &H80000002
   strComputer = "."

   Set ObjRegistry = GetObject("winmgmts:{impersonationLevel = impersonate}!\\" & strComputer & "\root\default:StdRegProv")

   strPath = "SOFTWARE\REAgent\Script Complete"
   Return = objRegistry.CreateKey(HKEY_LOCAL_MACHINE, strPath)
End Sub

1 个答案:

答案 0 :(得分:1)

您可以修改RunCommand以检测&amp;运行;

dim FSO, objShell, strApp
set FSO = CreateObject("Scripting.FileSystemObject")
set objShell = CreateObject("Wscript.Shell")

'//get system path
strApp = FSO.GetSpecialFolder(1) & "\REAgentc.exe"
if FSO.FileExists(strApp) then
    '//no need to change directory as you have the full path
    objShell.Run(strApp & " /disable")
else
    '//does not exist
end if