powershell - 解压缩文件并将包含的文件重命名为原始 zip 文件的名称
问题描述
目前,我正在编写一个脚本,将错误压缩到某个文件夹的 PDF 文件移动。然后这些文件将被解压缩,并且包含的文件将被移动到不同的文件夹中。目前这一切都有效。
我现在需要添加的是以下内容:我想使它自动将包含的文件重命名为原始 ZIP 文件的名称。因此,当我有一个名为“testzip”的 ZIP 文件并且包含的文件名为“testcontained”时,我希望它将“testcontained”重命名为“testzip”。
这将有助于跟踪哪些文件与哪些 ZIP 文件相关联。
Param (
$SourcePath = 'Z:\Documents\16_Med._App\Auftraege\PDFPrzemek\struktur_id_1225\',
$ZipFilesPath = 'Z:\Documents\16_Med._App\Auftraege\PDFPrzemek\Zip',
$UnzippedFilesPath = 'Z:\Documents\16_Med._App\Auftraege\PDFPrzemek\Zip\unzipped'
)
$VerbosePreference = 'Continue'
#region Test folders
@($SourcePath, $ZipFilesPath, $UnzippedFilesPath) | Where-Object {
-not (Test-Path -LiteralPath $_)
} | ForEach-Object {
throw "Path '$_' not found. Make sure that the folders exist before running the script."
}
#endregion
#region Get all files with extension .pdf
$Params = @{
Path = Join-Path -Path $SourcePath -ChildPath 'ext_dok'
Filter = '*.pdf'
}
$PDFfiles = Get-ChildItem @Params
Write-Verbose "Got $($PDFfiles.count) files with extension '.pdf' from '$($Params.Path)'"
#endregion
#region Move PDF and HL7 files
$MDMpath = Join-Path -Path $SourcePath -ChildPath 'MDM'
foreach ($PDFfile in ($PDFfiles | Where-Object {
(Get-Content $_.FullName | Select-Object -First 1) -like 'PK*'})
) {
$MoveParams = @{
Path = $PDFfile.FullName
Destination = Join-Path -Path $ZipFilesPath -ChildPath ($PDFfile.BaseName + '.zip')
}
Move-Item @MoveParams
Write-Verbose "Moved file '$($MoveParams.Path)' to '$($MoveParams.Destination)'"
$GetParams = @{
Path = Join-Path -Path $MDMpath -ChildPath ($PDFfile.BaseName + '.hl7')
ErrorAction = 'Ignore'
}
if ($HL7file = Get-Item @GetParams) {
$MoveParams = @{
Path = $HL7file
Destination = $ZipFilesPath
}
Move-Item @MoveParams
Write-Verbose "Moved file '$($MoveParams.Path)' to '$($MoveParams.Destination)$($HL7file.Name)'"
}
}
#endregion
#region Unzip files
$ZipFiles = Get-ChildItem -Path $ZipFilesPath -Filter '*.zip' -File
foreach ($ZipFile in $ZipFiles) {
$ZipFile | Expand-Archive -DestinationPath $UnzippedFilesPath -Force
Write-Verbose "Unzipped file '$($ZipFile.Name)' in folder '$UnzippedFilesPath'"
}
#endregion
您可能已经注意到,我正在解压缩 PDF 文件,这有点奇怪。让我解释一下:我们有很多无法打开的 PDF。在进行了一些检查后,我们得出结论,其中一些被错误地压缩了。为了找到哪些,我检查了 PDF 文件的第一个字节。如果它们包含“PK*”,则它们是 ZIP 文件。
要使用脚本解压缩它们,我需要重命名它们以将文件扩展名更改为“.zip”。如果文件有“.pdf”,那么即使它们在技术上是压缩的,您也无法解压缩它们。
那么,有没有人知道我可以做什么以使其自动重命名?
提前致谢!如果有任何缺失的信息,请告诉我。
更新
我现在已经测试了一些东西,但它不起作用。这是我做这项工作的尝试:
ForEach ($File in (Get-ChildItem $UnzippedFilesPath))
{ #rename unzipped file
Rename-Item $File.Name -NewName ($ZipFile.BaseName)
}
问题是我不知道如何回调原始 zip 文件。我不认为使用 $ZipFile 有效。这是我得到的错误:
Rename-Item : Cannot rename because item at 'test.txt' does not exist.
At line:4 char:2
+ Rename-Item $File.Name -NewName ($ZipFile.BaseName)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
解决方案
推荐阅读
- c - 删除未使用的数组时出现分段错误,有什么想法吗?
- android - 为框架 android 生成构建时缺少完整的 debug.jar 类
- android - Android更新资产文件夹中的数据库并将其复制到手机
- python - 如何获取数据框中特定案例的最后一个值
- loops - SWI Prolog foreach/2
- css - 视频全屏后按钮丢失
- ruby - 红宝石:LocalJumpError
- javascript - 使用 JS 或 JQUERY 在页面加载时选择第一个 html 表格行
- javascript - 我的页面中有表单验证功能,但它不起作用
- amazon-web-services - 使用 Terraform 创建的 Kubernetes 服务帐户导致 'doc is missing path: "/spec/volumes/0"' 副本集错误