我正在尝试自动提取不同目录中的一堆压缩文件(.ARJ)。
我目前正在使用2个文本文件来存储2位信息:
我正在尝试使用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
答案 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
' ↑ ↑
如果fDLocation
或fTLocation
包含空格
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