【问题标题】:VB script: if file exists then run if not then endVB脚本:如果文件存在则运行,如果不存在则结束
【发布时间】:2012-03-22 10:05:54
【问题描述】:

我正在寻找使用脚本来禁用 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

【问题讨论】:

  • 它有什么问题/失败/不行吗?
  • 您好,上面的摘录实际上运行良好。我只是不确定如何实现一个函数来查找文件是否存在/机器是7还是XP
  • 这个脚本已经做了你想做的事情。我不明白你还想要什么。

标签: file vbscript operating-system


【解决方案1】:

你可以修改你的RunCommand来检测&运行;

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

【讨论】:

  • 好的,谢谢,如何做到这一点?一位同事实际上最初构建了脚本,我只是根据我所拥有的知识稍微调整了它。再次感谢。
猜你喜欢
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
  • 2021-11-15
  • 2017-12-17
  • 2010-11-18
  • 2020-08-18
  • 2014-07-20
  • 2011-10-19
相关资源
最近更新 更多