我有一个文件夹,里面有几个子文件夹。每个子文件夹可能/可能不包含一些 *.ZIP 文件。
我试图将每个 zip 文件解压缩到与原始 zip 文件同名的单独文件夹中(如果存在,则覆盖该文件夹),然后删除存档。
但是我从下面的代码中得到的是提取父目录中的所有 ZIP 文件,这不是我想要的。
这是我的代码:
foreach ($file in (dir -recurse *.zip)) { & "c:\Program Files\7-zip\7z" x "$file" -aoa }; rm dir -recurse *.zip
有人可以帮我吗?
我刚刚找到了我的问题的完美答案,我愿意将其发布在这里,供将来遇到此线程的任何人使用。
我有几个问题。其中之一是在 PowerShell (v5.1 Windows 10) 中处理长路径。为了规避的是,用户应安装并加载PSAlphaFS
在PowerShell的模块,并使用Get-LongChildItem
替代Get-ChildItem
。为此,首先您需要运行以下命令来更新系统上的 PowerShell 执行策略:
Set-ExecutionPolicy RemoteSigned
接下来,您需要通过这个来安装模块:
Install-Module -Name PSAlphaFS
最后用这个加载它:
import-module PSAlphaFS
现在我们准备好摇滚和扮演角色了。只需将以下代码粘贴到 PowerShell 中,它就可以完成这项工作。(记得修改第20行的路径)
Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
param([string]$zipfile, [string]$outpath)
try {
[IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
echo "Done with unzip of file :) "
$true
}
catch {
echo "Oops....Can't unzip the file"
$false
}
}
$flag = $true
while($flag)
{
$zipFiles = Get-LongChildItem -Path "c:\Downloads\New Folder\FI" -Recurse | Where-Object {$_.Name -like "*.zip"}
if($zipFiles.count -eq 0)
{
$flag = $false
}
elseif($zipFiles.count -gt 0)
{
foreach($zipFile in $zipFiles)
{
#create the new name without .zip
$newName = $zipFile.FullName.Replace(".zip", "")
if(Unzip $zipFile.FullName $newName){
Remove-Item $zipFile.FullName
}
}
}
Clear-Variable zipFiles
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句