7zip - powershell -排除“listfile”中的文件

iaqfqrcu  于 2023-06-06  发布在  Shell
关注(0)|答案(1)|浏览(203)

我想通过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")
...

我做错了什么?或者我该怎么做,排除那些文件?

jyztefdp

jyztefdp1#

我刚想明白。我排除了整个“\pdfs”目录,但包括最新的文件-这个文件将被写入zip的根目录。现在我使用7zip函数“rn”来重命名zip文件中的pdf:

$Date = get-date -format "yyy-MM-dd"
$ThisFolder = Get-Location 
$ziptargetFolder = Split-Path -parent $ThisFolder
$folder     = (Get-Item $ThisFolder)
$folderName = $folder.Name
$PDFFolder = -join ("$ThisFolder", "\pdfs")
$ziptarget = -join("$ziptargetFolder", "\", "$Date", "_", "$folderName", ".zip")

$latest = (Get-ChildItem "$PDFFolder" | Sort-Object CreationTime -Descending | Select-Object -First 1)
$includelatestPDF = -join("-ir!", ".\pdfs\", "$latest")#".Name") #Name 

$excludingzips = -join("-xr!", "*.zip")
$excludingpdfs = -join("-x!", "*\*.pdf") #ignor pdfs in root directory (not xr! - just x!)

$excludingpdfsfolder = -join("-xr!", "*\pdfs\*.pdf")

$zipExePath= "C:\Program Files\7-Zip\7z.exe"

[Array]$zipArgs = "a", "-tzip", "-ssw", "$ziptarget", "$ThisFolder", "$excludingzips", "$excludingpdfs", "$excludingpdfsfolder", "$includelatestPDF"

& $zipExePath $zipArgs

$src= $latest.Name
$rename = -join("$folderName", "\pdfs\", "$src")
[Array]$zipArgs = "rn", "$ziptarget", "$src", "$rename"

& $zipExePath $zipArgs

相关问题