我有一个用于压缩旧文件并删除它们的脚本。该脚本在所有其他目录中工作正常,但一个特定目录将无法工作。它会尝试将其压缩,但最终只会创建一个名为 .zip 的空 zip 文件Program.7z
。这让我觉得脚本中的空格没有被正确转义。我在我的路径中使用了双引号和单引号并检查了文件路径的串联。我还没有找到它可能是什么。有任何想法吗?
Const fileZillaLogs = "C:\Program Files (x86)\Server\Logs"
Const zipProgram = """C:\Program Files\7-Zip\7zG.exe"""
Const zipArgs = "a -mx9"
Dim intZipAge
intZipAge = 7
Dim intDelAge
intDelAge = 90
Call DeleteLogFiles(fileZillaLogs, intZipAge, intDelAge)
Function DeleteLogFiles(strLogPath, intZipAge, intDelAge)
Const bDEBUG = True
Dim objFs
Dim objFolder
Dim objSubFolder
Dim objFile
Dim objWShell
Dim strCommand
Dim iResult
Set objWShell = CreateObject("WScript.Shell")
Set objFs = CreateObject("Scripting.FileSystemObject")
If Right(strLogPath, 1) <> "\" Then
strLogPath = strLogPath & "\"
End If
If objFs.FolderExists(strLogPath) Then
Set objFolder = objFs.GetFolder(strLogPath)
For Each objSubFolder in objFolder.subFolders
DeleteLogFiles strLogPath & objSubFolder.Name, intZipAge, intDelAge
Next
For Each objFile in objFolder.Files
If bDebug Then wscript.echo vbTab & "reviewing file = " & strLogPath & objFile.Name
If DateDiff("d",objFile.DateLastModified,Date) > intDelAge Then
If bDebug Then wscript.echo "Deleting because its old" End If
objFs.DeleteFile(strLogPath & objFile.Name)
Else If DateDiff("d",objFile.DateLastModified,Date) > intZipAge _
And (Right(objFile.Name, 4) = ".log") Then
If bDebug Then wscript.echo vbTab & "zipping file = " & objFile.Path
strCommand = zipProgram & " " & zipArgs & " " & objFile.Path & ".7z" & " " & objFile.Path
iResult = objWShell.Run(strCommand, 0, "false")
If bDebug Then wscript.echo vbTab & "zipping result = " & iResult
If bDebug Then wscript.echo vbTab & "deleting file = " & strLogPath & objFile.Name
objFs.DeleteFile(strLogPath & objFile.Name)
End If
End If
Next
Set objFs = Nothing
Set objFolder = Nothing
Set objWShell = nothing
End If
End Function
您的问题在第 41 行。您的objFile.Path中有空格,但您将其添加到命令行参数。这意味着空格应该用引号引起来,但不能使用引号,因为它们用于连接字符串。然后,您必须转义应包含在最终结果字符串中的引号。在 VBS 中,转义字符是双引号字符。所以你的第 41 行应该是这样的:
注意 """" 序列,它们基本上是这样计算的:
我测试了它并且工作正常。两次使用objFile.Path函数时都必须转义。