[英]PowerShell extract each zip file to own folder
我想将一些文件解压缩到与 zip 文件同名的文件夹中。 我一直在做这样笨重的事情,但由于这是 PowerShell,通常有更聪明的方法来实现目标。
是否有某种单行或双行方式可以对文件夹中的每个 zip 文件进行操作,并将其解压缩到与 zip 同名的子文件夹中(但没有扩展名)?
foreach ($i in $zipfiles) {
$src = $i.FullName
$name = $i.Name
$ext = $i.Extension
$name_noext = ($name -split $ext)[0]
$out = Split-Path $src
$dst = Join-Path $out $name_noext
$info += "`n`n$name`n==========`n"
if (!(Test-Path $dst)) {
New-Item -Type Directory $dst -EA Silent | Out-Null
Expand-Archive -LiteralPath $src -DestinationPath $dst -EA Silent | Out-Null
}
}
你可以用更少的变量来做。 当$zipfiles
集合包含出现时的 FileInfo 对象时,大多数变量可以使用对象已有的属性替换。
此外,尽量避免使用+=
连接到变量,因为这既消耗时间又消耗内存。
只需在变量中捕获循环中输出的任何结果。
像这样的东西:
# capture the stuff you want here as array
$info = foreach ($zip in $zipfiles) {
# output whatever you need to be collected in $info
$zip.Name
# construct the folderpath for the unzipped files
$dst = Join-Path -Path $zip.DirectoryName -ChildPath $zip.BaseName
if (!(Test-Path $dst -PathType Container)) {
$null = New-Item -ItemType Directory $dst -ErrorAction SilentlyContinue
$null = Expand-Archive -LiteralPath $zip.FullName -DestinationPath $dst -ErrorAction SilentlyContinue
}
}
# now you can create a multiline string from the $info array
$result = $info -join "`r`n==========`r`n"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.