使用vbs压缩大文件时遇到问题

时间:2012-10-18 21:53:49

标签: windows vbscript compression

我混淆了一个vbs脚本,使用7za的命令行实用程序来压缩超过7天的文件。虽然大多数逻辑工作正常,但我能够将单个文件压缩成单个zip文件。

当我尝试将所有匹配的文件添加到一个zip文件时出现问题。以下是代码段:

strCommand = "7za.exe -mx=9 a " & ObjectFolder & sysDate & ".zip " & strFileName
strRun = objShell.Run(strCommand, 0, True)

现在根据2 nd 行,设置True将确保脚本将等待命令执行完毕。但问题是7za立即退出并进入下一个循环,处理下一个文件,因为它尝试创建相同的zip文件,我得到访问被拒绝错误。

有人可以帮我解决这个问题吗?

我还在命令提示符下测试了该方案。我所做的是,在单独的提示中同时执行以下2个命令:

提示1:     C:\ 7za.exe -mx = 9 test.zip c:\ sample1.pdf

提示2:     C:\ 7za.exe -mx = 9 a test.zip c:\ sample2.pdf

提示2导致以下错误:

  

错误:test.zip不支持存档

  系统错误:
  该进程无法访问该文件,因为它正由另一个进程使用。

这与我在脚本中遇到的错误相同,我需要帮助才能解决此问题。任何指针都会有所帮助!

更新

有了约翰和安斯加提供的伟大指示,我能够解决这个问题!原来这是我脚本中的一个错误!在我的脚本中,我检查了文件是否在处理归档之前被任何其他进程使用。所以我通过使用以下命令打开要附加的文件来检查这个:

Set f = objFSO.OpenTextFile(strFile, ForAppending, True)

但是在继续处理同一个文件之前,我没有在脚本中关闭它,因此出现错误: 进程无法访问该文件,因为它正由另一个进程使用 < /强>

关闭文件后,一切顺利!

再次感谢我在这里获得的所有支持!

为表示感谢,我正在分享整个剧本供任何人使用。请注意,我不是这个的原作者,我从各种渠道收集它并稍微调整一下以满足我的需求。

Archive.vbs

Const ForAppending = 8  ' Constant for file lock check

Dim objFSO, objFolder, objFiles, objShell
Dim file, fileExt, fileName, strCommand, strRun, strFile
Dim SFolder, OFolder, Extension, DaysOld, sDate

'''' SET THESE VARIABLES! ''''
SFolder = "C:\SourceFolder\"      'Folder to look in
OFolder = "C:\OutputFolder\"      'Folder to put archives in
Extension = "pdf"        'Extension of files you want to zip
DaysOld = 1        'Zip files older than this many days
''''''''''''''''''''''''''''''

sDate = DatePart("yyyy",Date) & "-" & Right("0" & DatePart("m",Date), 2) & "-" & Right("0" & DatePart("d",Date), 2)


'Create object for playing with files
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Create shell object for running commands
Set objShell = wscript.createObject("wscript.shell")

'Set folder to look in
Set objFolder = objFSO.GetFolder(SFolder)

'Get files in folder
Set objFiles = objFolder.Files

'Loop through the files
For Each file in objFiles
  fileName = Split(file.Name, ".")
  fileExt = fileName(UBound(fileName))
  'See if it is the type of file we are looking for
  If fileExt = Extension Then
    'See if the file is older than the days chosen above
    If DateDiff("d", file.DateLastModified, Now()) >= DaysOld Then
      strFile = file.Path
      'See if the file is available or in use
        Set f = objFSO.OpenTextFile(strFile, ForAppending, True)
        If Err.Number = 70 Then ' i.e. if file is locked

        Else
        f.close
        strFName = objFSO.GetBaseName(file.name)
        strCommand = "C:\7za.exe -mx=9 a " & OFolder & sDate & ".zip " & strFile

        strRun = objShell.Run(strCommand, 0, True)
        'wscript.echo strCommand  ' un-comment this to check the file(s) being processed
        'file.Delete  ' un-comment this to delete the files after compressing.
        End If
    End If

  End If
Next

'Cleanup
Set objFiles = Nothing
Set objFolder = Nothing
Set objFSO = Nothing
Set objShell = Nothing
wscript.Quit

===========================

由于

-Noman A。

2 个答案:

答案 0 :(得分:1)

不完全符合您的要求,但这是我用于类似任务的批处理脚本,以防止您解决直接问题:

ArchiveScriptLog.Bat

::ensure we're in the right directory, then run the script & log the output
cls
pushd "c:\backup scripts"
ArchiveScript.bat > ArchiveScript.log
popd

ArchiveScript.bat

::Paths (must include the \ on the end).  There must be no space between the equals and the value
::UNC paths are acceptable

Set FolderToBackup=F:\EnterpriseArchitect\Energy\
Set BackupPath=F:\EnterpriseArchitect\!ARCHIVE\
Set RemoteBackupPath=\\ukccojdep01wok\h$\Energy\cciobis01edc\
Set SevenZip=C:\Program Files (x86)\7-Zip\

::Get DATE in yyyymmdd format; done in two lines to make it easy to change the date format
FOR /F "TOKENS=2,3,4 DELIMS=/ " %%A IN ('echo %Date%') DO (SET mm=%%A&SET dd=%%B&SET yyyy=%%C)
SET strDate=%yyyy%%mm%%dd%

::Set the Backup File to be the backup path with the current date & .zip on the end
Set BackupFile=%BackupPath%%strDate%.zip

::create a zip containing the contents of folderToBackup
pushd %SevenZip%
7z a "%BackupFile%" "%FolderToBackup%"
popd

::go to the archive directory & copy all files in there to the remote location (this accounts for previous errors if the network were unavailable)
pushd "%BackupPath%"
move *.zip "%RemoteBackupPath%"
popd

::delete off backups in the remote location which are older than 90 days
pushd "%RemoteBackupPath%"
forfiles /D -90 /M *.zip /C "cmd /c del @file"
popd

答案 1 :(得分:1)

7za完成任务之前,您的命令不应该返回(并且在我的测试中没有)。尝试将代码更改为以下内容,以便了解正在进行的操作:

strCommand = "7za.exe -mx=9 a " & ObjectFolder & sysDate & ".zip " & strFileName
strCommand = "%COMSPEC% /k " & strCommand
strRun = objShell.Run(strCommand, 1, True)

引用文件名也可能是个好主意:

Function qq(str)
  qq = Chr(34) & str & Chr(34)
End Function

strCommand = "7za.exe -mx=9 a " & qq(ObjectFolder & sysDate & ".zip") & " " _
  & qq(strFileName)