我想通过powershell-Script使用7zip压缩文件夹。此文件夹包含一个特定的子目录“\pdfs”,其中包含大量的pdf文件。我只想在此目录中保留最新的PDF。我的代码是这样的:
$Date = get-date -format "yyy-MM-dd"
$ThisFolder = Get-Location
$ziptargetFolder = Split-Path -parent $ThisFolder
$folder = (Get-Item $ThisFolder)
$folderName = $folder.Name
$excludingpdflist =""
$PDFFolder = -join ("$ThisFolder", "\pdfs")
$ziptarget = -join("$ziptargetFolder", "\", "$Date", "_", "$folderName", ".zip")
$ExcludeFilelist = -join("$ziptargetFolder", "\ExcludeFiles.txt")
$ExludeBackupFolder = -join("$ThisFolder", "\ProjectBackup\backups")
Get-ChildItem "$PDFFolder" | Sort CreationTime | Select Object -SkipLast 1 -ExpandProperty Name |Out-File "$ExcludeFilelist" -encoding ascii
$excludingzips = -join("-xr!", "*.zip")
$excludingpdfs = -join("-x!", "*\*.pdf")
$excludingpdflist = -join("-x@", "$ExcludeFilelist")
$zipExePath= "C:\Program Files\7-Zip\7z.exe"
[Array]$zipArgs = "a", "-tzip", "$ziptarget", "$ThisFolder", "$excludingzips", "$excludingpdfs", "$excludingpdflist"
& $zipExePath $zipArgs
代码行
Get-ChildItem "$PDFFolder" | Sort CreationTime | Select Object -SkipLast 1 -ExpandProperty Name |Out-File "$ExcludeFilelist" -encoding ascii
创建一个txt文件,其中包含“\pdfs”目录中除最新文件以外的所有文件-这些文件应该从zip-Archiv中排除,但实际上没有。
MyFile.Rev0100.pdf
MyFile.Rev0101.pdf
MyFile.Rev0102.pdf
MyFile.Rev0103.pdf
我还尝试了要排除的文件的“全名”:
Get-ChildItem "$PDFFolder" | Sort CreationTime | Select Object -SkipLast 1 -ExpandProperty FullName |Out-File "$ExcludeFilelist" -encoding ascii
这段代码的输出如下:
C:\MyDirectories\pdfs\MyFile.Rev0100.pdf
C:\MyDirectories\pdfs\MyFile.Rev0101.pdf
C:\MyDirectories\pdfs\MyFile.Rev0102.pdf
C:\MyDirectories\pdfs\MyFile.Rev0103.pdf
我也试着 * 包含 * 最新的文件,但这也不起作用;代码更改为:
Get-ChildItem "$PDFFolder" | Sort CreationTime -Descending | Select Object -First 1 -ExpandProperty Name |Out-File "$IncludeFilelist" -encoding ascii # txt contains: MyFile.Rev0104.pdf
...
$Includingpdflist = -join("-i@", "$IncludeFilelist")
...
我做错了什么?或者我该怎么做,排除那些文件?
1条答案
按热度按时间jyztefdp1#
我刚想明白。我排除了整个“\pdfs”目录,但包括最新的文件-这个文件将被写入zip的根目录。现在我使用7zip函数“rn”来重命名zip文件中的pdf: