使用从文本文件加载的字符串运行外部命令

时间:2017-01-12 07:33:43

标签: vbscript winrar

我正在尝试自动提取不同目录中的一堆压缩文件(.ARJ)。

我目前正在使用2个文本文件来存储2位信息:

  1. 压缩文件的当前位置和名称(ARJFileNames.txt) sample - D:_Work_Splunk_TestBed \ Branch00 \ LOAN.ARJ
  2. 要提取的文件的目标位置(ARJFileLocations.txt) 示例 - D:_Work_Splunk_TestBed \ Branch00
  3. 我正在尝试使用WScript.Shell命令运行WinRAR,将文件从当前位置提取到目标位置。

    我的问题是当我从循环中调用外部命令时,似乎无法获得正确的语法来附加我从文本文件中提取的字符串与实际调用WinRar及其switch / command

    这是我目前的代码:

    'Declaring Constants
    Const ForReading = 1, ForWriting = 2, ForAppending = 3
    'Declaring Variables
    Dim fso, strFilePath, strFileName, fFilePath, fFileName, objShell, WinRAR, strCMD, SevenZip, ARJ
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set objShell = WScript.CreateObject ("WScript.shell")    
    
    'Open Text Files for use
    Set strFilePath     = fso.OpenTextFile("D:\_Work\_Splunk\_TestBed\ARJFileLocations.txt", ForReading, TristateFalse)
    Set strFileName = fso.OpenTextFile("D:\_Work\_Splunk\_TestBed\ARJFileNames.txt", ForReading, TristateFalse)
    
    
    Do Until strFilePath.AtEndOfStream
      fFilePath = strFilePath.ReadLine  'Get the location of the ARJ file
      fFileName = strFileName.ReadLine  'Get the target location for ARJ file contents 
      'Storing the command as 1 string'
      strCMD = "winrar x -y " & " " & fFileName & " " & fFilePath
      'Running the command in CLI'
      objShell.Run strCMD 
    Loop
    
    'Cleaning Up
    Set strFilePath = Nothing
    Set strFileName = Nothing
    Set objShl = Nothing
    

1 个答案:

答案 0 :(得分:1)

Read Concatenation Operator (&)参考。

命令行应该最终显示为在命令提示符下键入命令行(由Wscript.Echo Command验证):

Command = """" & WinRAR & "\WinRAR.exe"" X " & fDLocation & " " & fTLocation
'         ↑↑↑↑                        ↑↑
' results to
' "D:\Program Files\WinRAR\WinRAR.exe" X ARJLocation TargetLocation
' ↑                                  ↑

如果fDLocationfTLocation包含空格

,请使用以下内容
Command = """" & WinRAR & "\WinRAR.exe"" X """ & fDLocation & """ """ & fTLocation & """"
'                                           ↑↑                 ↑↑ ↑↑                 ↑↑↑↑
' results to
' "D:\Program Files\WinRAR\WinRAR.exe" X "ARJ Location" "Target Location"
' ↑                                  ↑   ↑            ↑ ↑               ↑

此外,我考虑同步运行脚本和WinRAR.exe程序(参见Run Method (Windows Script Host)文章),如下所示:

Dim intRunResult
Do Until strARJLocations.AtEndOfStream
  fDLocation = strARJLocations.ReadLine     'Get the location of the ARJ file'
  fTLocation = strTargetLocation.ReadLine   'Get the target location for ARJ file contents'
  Command = """" & WinRAR & "\WinRAR.exe"" X """ & fDLocation & """ """ & fTLocation & """"
  intRunResult = objShell.Run ( Command, 1, True)
Loop