7zip compactará apenas alguns arquivos

7zip compactará apenas alguns arquivos

Eu tenho um script para compactar arquivos antigos e excluí-los. O script funciona bem em todos os outros diretórios, mas um diretório específico não funcionará. Ele tentará compactá-lo, mas acabará criando apenas um arquivo zip vazio chamado Program.7z. Isso me faz pensar que os espaços não estão sendo escapados corretamente no script. Usei aspas duplas e simples em meus caminhos e verifiquei a concatenação dos caminhos dos arquivos. Não descobri o que poderia ser. Alguma ideia?

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

Responder1

Seu problema está na linha 41. SeuobjFile.Pathcontém espaços, mas você o adicionou a um argumento de linha de comando. Isso significa que os espaços devem ser encapsulados entre aspas, mas você não pode usar aspas, pois elas são usadas para concatenar a string. Você deve então escapar das aspas que deveriam estar na string do resultado final. No VBS, o caractere de escape são aspas duplas. Então sua linha 41 deve ficar assim:

strCommand = zipProgram & " " & zipArgs & " " & """" & objFile.Path & ".7z" & """" & " " & """" & objFile.Path & """"

Observe as sequências """", que são basicamente calculadas como tal:

  • Primeira aspa dupla: início da string
  • Segunda aspa dupla: escapa do próximo caractere
  • Terceira aspa dupla: é o caractere que você deseja na string final
  • Quarta aspa dupla: fim da string

Eu testei e funciona bem. Você tem que escapar do tempo em que usa oobjFile.Pathfunção.

informação relacionada