
Tengo un script para comprimir archivos antiguos y eliminarlos. El script funciona bien en todos los demás directorios, pero un directorio específico no funcionará. Intentará comprimirlo pero terminará creando solo un archivo zip vacío llamado Program.7z
. Esto me hace pensar que los espacios no se escapan correctamente en el guión. Utilicé comillas dobles y simples en mis rutas y verifiqué la concatenación de las rutas de los archivos. No he encontrado que podría ser. ¿Algunas ideas?
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
Respuesta1
Su problema está en la línea 41. SuobjFile.Rutatiene espacios pero lo estás agregando a un argumento de línea de comando. Esto significa que los espacios deben estar encapsulados entre comillas, pero no puedes usar comillas ya que se usan para concatenar la cadena. Luego debe escapar de las comillas que deberían estar en la cadena de resultado final. En VBS, el carácter de escape es la comilla doble. Entonces tu línea 41 debería verse así:
strCommand = zipProgram & " " & zipArgs & " " & """" & objFile.Path & ".7z" & """" & " " & """" & objFile.Path & """"
Observe las secuencias """", que básicamente se calculan así:
- Primera comilla doble: comienzo de cadena
- Segunda comilla doble: escapa al siguiente carácter
- Tercera comilla doble: es el carácter que desea en su cadena final
- Cuarta comilla doble: fin de cadena
Lo probé y funciona bien. Tienes que escapar las dos veces que uses elobjFile.Rutafunción.