
古いファイルを圧縮して削除するスクリプトがあります。スクリプトは他のすべてのディレクトリでは正常に動作しますが、特定のディレクトリでは動作しません。圧縮しようとしますが、 という空の 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
答え1
問題は41行目にあります。objFile.パススペースが含まれていますが、コマンドライン引数に追加しています。つまり、スペースは引用符で囲む必要がありますが、引用符は文字列を連結するために使用されるため使用できません。次に、最終結果の文字列に含まれる引用符をエスケープする必要があります。VBS では、エスケープ文字は二重引用符です。したがって、行 41 は次のようになります。
strCommand = zipProgram & " " & zipArgs & " " & """" & objFile.Path & ".7z" & """" & " " & """" & objFile.Path & """"
基本的に次のように計算される「」シーケンスに注意してください。
- 最初の二重引用符: 文字列の先頭
- 2番目の二重引用符: 次の文字をエスケープします
- 3番目の二重引用符: 最終的な文字列に必要な文字です
- 4番目の二重引用符: 文字列の終わり
試してみたところ、問題なく動作しました。objFile.パス関数。