批量提取多通道保护 .7z
Batch extract multiple pass protected .7z
我想编写一个 PowerShell 脚本来提取多个受密码保护的 .7z 文件,这些文件的密码保存在一个文本文件中,其文件名包含 .7z 的名称
文件结构示例:
EncryptedFile1_02192020.7z
EncryptedFile1_password.txt
EncryptedFile2_02192020.7z
EncryptedFile2_password.txt
仅当文件夹中有 1 个 .7z 和 1 个 .txt 文件时,下面的脚本才有效。
$pass = get-content *.txt
7z.exe x *.7z -p"$pass" -o*
问题:不知道如何结合foreach和-Like语句批量提取工作目录下的多个加密文件。在此先感谢您的帮助。
这是一种方法。它收集根文件夹中的文件,将它们分组到下划线 _
之前的文件名的第一部分,并在这些组中找到以 .txt
和 .7z
结尾的文件。
如果两者都有,则从文本文件中读取密码并提取 7z 文件。
Get-ChildItem -Path 'TheFolderWhereTheFilesAre' -File |
Group-Object @{Expression = {($_.Name -split '_')[0]}} |
Where-Object { $_.Count -gt 1 } |
ForEach-Object {
$pwFile = $_.Group | Where-Object { $_.Extension -eq '.txt' }
zFile = $_.Group | Where-Object { $_.Extension -eq '.7z' }
if (($pwFile) -and (zFile)) {
Write-Host "Extracting file '$(zFile.FullName)'"
# the single quotes are needed when the password contains spaces or special characters
$pass = "'{0}'" -f (Get-Content -Path $pwFile.FullName -Raw)
7z.exe x $(zFile.FullName) -p"$pass" -o*
}
}
注意,如果文件都以 EncryptedFile_
开头,那么您可以将 -Filter 'EncryptedFile_*.*'
添加到 Get-ChildItem cmdlet 以加快处理速度
我想编写一个 PowerShell 脚本来提取多个受密码保护的 .7z 文件,这些文件的密码保存在一个文本文件中,其文件名包含 .7z 的名称
文件结构示例:
EncryptedFile1_02192020.7z
EncryptedFile1_password.txt
EncryptedFile2_02192020.7z
EncryptedFile2_password.txt
仅当文件夹中有 1 个 .7z 和 1 个 .txt 文件时,下面的脚本才有效。
$pass = get-content *.txt
7z.exe x *.7z -p"$pass" -o*
问题:不知道如何结合foreach和-Like语句批量提取工作目录下的多个加密文件。在此先感谢您的帮助。
这是一种方法。它收集根文件夹中的文件,将它们分组到下划线 _
之前的文件名的第一部分,并在这些组中找到以 .txt
和 .7z
结尾的文件。
如果两者都有,则从文本文件中读取密码并提取 7z 文件。
Get-ChildItem -Path 'TheFolderWhereTheFilesAre' -File |
Group-Object @{Expression = {($_.Name -split '_')[0]}} |
Where-Object { $_.Count -gt 1 } |
ForEach-Object {
$pwFile = $_.Group | Where-Object { $_.Extension -eq '.txt' }
zFile = $_.Group | Where-Object { $_.Extension -eq '.7z' }
if (($pwFile) -and (zFile)) {
Write-Host "Extracting file '$(zFile.FullName)'"
# the single quotes are needed when the password contains spaces or special characters
$pass = "'{0}'" -f (Get-Content -Path $pwFile.FullName -Raw)
7z.exe x $(zFile.FullName) -p"$pass" -o*
}
}
注意,如果文件都以 EncryptedFile_
开头,那么您可以将 -Filter 'EncryptedFile_*.*'
添加到 Get-ChildItem cmdlet 以加快处理速度