PowerShell从zip文件中提取17个文件夹深
我有一个自动创建的zip文件,我无法更改其中的文件夹数量。PowerShell从zip文件中提取17个文件夹深
我试图提取所有从深17个文件夹中的zip文件中的文件夹中的内容。问题是文件夹的名称可能会更改。
我开始使用7Zip解压缩其他压缩文件夹和正常工作:
$zipExe = join-path ${env:ProgramFiles(x86)} '7-zip\7z.exe'
if (-not (test-path $zipExe)) {
$zipExe = join-path ${env:ProgramW6432} '7-zip\7z.exe'
if (-not (test-path $zipExe)) {
'7-zip does not exist on this system.'
}
}
set-alias zip "C:\Program Files\7-Zip\7z.exe"
zip x $WebDeployFolder -o \$WebDeployTempFolder
有没有一种方法来提取深17个文件夹中的ZIP文件中的文件夹中的内容?
您可以使用7Zip的上市函数来获取文件的内容。然后,您可以解析该输出,查找具有17个级别的文件夹并使用该路径来提取内容。
下面是一段代码,它就是这样做的。
$7zip = "${env:ProgramFiles(x86)}\7-Zip\7z.exe"
$archiveFile = "C:\Temp\Archive.zip"
$extractPath = "C:\Temp"
$archiveLevel = 17
# Get contents list from zip file
$zipContents = & $7zip l $archiveFile
# Filter contents for only folders, described as "D" in Attr column
$contents = $zipContents | Where-Object { $_ -match "\sD(\.|[A-Z]){4}\s"}
# Get line where the folder level defined in $archiveLevel is present
$folderLine = $contents | Where-Object { ($_ -split "\\").Count -eq ($archiveLevel) }
# Get the folder path from line
$folderPath = $folderLine -split "\s" | Where-Object { $_ } | Select-Object -Last 1
# Extract the folder to the desired path. This includes the entire folder tree but only the contents of the desired folder level
Start-Process $7zip -ArgumentList "x $archiveFile","-o$extractPath","$folderPath" -Wait
# Move the contents of the desired level to the top of the path
Move-Item (Join-Path $extractPath $folderPath) -Destination $extractPath
# Remove the remaining empty folder tree
Remove-Item (Join-Path $extractPath ($folderPath -split "\\" | Select-Object -First 1)) -Recurse
代码中有几个注意事项。 我无法找到一种方法来提取文件夹没有完整的路径/ parensts。所以最后清理。但请注意,父文件夹不包含任何其他文件或文件夹。 另外,我不得不在最后使用“Start-Process”,否则7Zip会打破变量输入。
你可能会改变它取决于你的ZIP文件结构都有点,但它应该让你去。
这工作到它移动项目(加入路径$ extractPath $ folderPath)-Destination $ extractPath我得到一个无效路径错误,但路径是正确的。 –
我在本地做了一个人工测试,它对我很有用,但是在你的环境中可能会有一些差异。你是否记得更新顶部的路径?你能提供确切的错误信息吗? –
是的,我已经改变了路径,我应该添加这些是网络路径。 错误: 无效路径:无效路径:“\\计算机\共享\ GDistribute \测试\ QA \ WebsiteName \ CodeName.Web \内容\ R_C \ GO47_1 \ DATA01 \ 2 \代号\ BuildName \源头\ SRC \ DIR \ 31X \来源\ CodeName.Web \ OBJ \发布\包\ PackageTmp”。 在行:22字符:1 +移动项目(联接路径$ extractPath $ FOLDERPATH)-Destination $ extractPath + CategoryInfo:InvalidOperation:(:) [],ArgumentException的 + FullyQualifiedErrorId:MoveItemDynamicParametersProviderException –
我丢失的问题,或者你所面临的问题:您可以编辑您的问题,所以它明确规定,你所面临的问题? – bluuf
@bluuf完成。添加所以我的问题是:有没有办法提取zip文件中17个文件夹深处的文件夹中的内容。 –
@bluuf我的答案是:是的。请澄清你的问题。具体问题是什么?你是否尝试过自己?请在帮助中心查看[我如何提出一个好问题?](https://stackoverflow.com/help/how-to-ask)。 – Clijsters