从Powershell调用的7-Zip无法找到要归档的一个或多个文件

时间:2014-02-28 22:27:03

标签: powershell 7zip

我有一个PowerShell(Windows 7上的v1.0)脚本,它将7-Zip存档时间戳与源文件时间戳进行比较,并在任何源文件较新时重新创建存档。记录脚本正在执行的操作的顺序(如果需要重新创建存档)是:

$logFile = "$env:temp\$zipFile.log"      # the backup log file
write-host "  Compressing ${newest}${otherDocDirList}"
write-host "  The log file is $logFile."

del $zipFile -erroraction:silentlycontinue
echo "  $(pwd)"
echo "  C:\progra~1\7-zip\7z.exe a $zipFile ${newest} ${otherDocDirs}"
C:\progra~1\7-zip\7z.exe a $zipFile ${newest} ${otherDocDirs} | set-content $logFile -encoding ascii

在某些情况下,这个工作文件,特别是

Compressing UserGuide_2303.docm, Graphics, and gettoolbar.
The log file is C:\Users\jim\AppData\Local\Temp\ST_SUG.7z.log.
C:\users\jim\desktop\ST\userguide\working
C:\progra~1\7-zip\7z.exe a ST_SUG.7z UserGuide_2303.docm Graphics gettoolbar

Compressing AdminGuide_0300.docm and Graphics.
The log file is C:\Users\jim\AppData\Local\Temp\ST_AUG.7z.log.
C:\users\jim\desktop\ST\adminguide\working
C:\progra~1\7-zip\7z.exe a ST_AUG.7z AdminGuide_0300.docm Graphics

但是我收到错误“SystemOverview_TrngMan_0103.pptx SystemOverview_TrngMan_0103.docm:警告:系统找不到指定的文件。”在我尝试时在日志文件中:

Compressing SystemOverview_TrngMan_0103.pptx SystemOverview_TrngMan_0103.docm and Graphics.
The log file is C:\Users\jim\AppData\Local\Temp\ST_SOTG.7z.log.
C:\users\jim\desktop\ST\Training\SystemOverview\working
C:\progra~1\7-zip\7z.exe a ST_SOTG.7z SystemOverview_TrngMan_0103.pptx SystemOverview_TrngMan_0103.docm Graphics

当我将命令复制为echoed并在命令行中运行时,7-Zip会创建存档而不会出现任何错误。

前两个示例和失败的示例之间的一个区别是$ newest有两个文件名,用空格分隔。查找最新文件的代码使用哈希表,并为$ newest分配一个值:

$newest = $newhash.Values -join ' '

我怀疑生成的命令行中可能有一个额外的字符,7-Zip在解析命令行时没有预料到。当我在文件名之间放置2个空格时,我之前遇到了同样的错误。 有什么建议?在此先感谢!!

1 个答案:

答案 0 :(得分:0)

而不是

    $newest = $newhash.values -join ' '

    $newest = $newhash.values

PowerShell会自动将值数组扩展为单个参数。