user1285511
user1285511

Reputation: 11

VB script: if file exists then run if not then end

I'm looking to use a script to go about disabling a file (REAgentc.exe) on Windows 7 machines and if it does not exist (i.e. on XP machines) then end. Below is what I've got so far but can anyone assist me in implementing the rest of what I'm after? My knowledge on VB script is admittedly not great but any help that can be offered forward would be greatly appreciated, thanks.

'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

Upvotes: 1

Views: 6898

Answers (1)

Alex K.
Alex K.

Reputation: 175766

You can modify your RunCommand to detect & run;

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

Upvotes: 1

Related Questions