Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PowerShell从zip文件中提取17个文件夹_Powershell_Powershell 4.0_Powershell 5.0 - Fatal编程技术网

PowerShell从zip文件中提取17个文件夹

PowerShell从zip文件中提取17个文件夹,powershell,powershell-4.0,powershell-5.0,Powershell,Powershell 4.0,Powershell 5.0,我有一个自动创建的zip文件,无法更改其中的文件夹数 我试图从zip文件中17个文件夹深处的文件夹中提取所有内容。问题是文件夹的名称可能会更改 我开始使用7Zip提取另一个zip文件夹,效果很好: $zipExe = join-path ${env:ProgramFiles(x86)} '7-zip\7z.exe' if (-not (test-path $zipExe)) { $zipExe = join-path ${env:ProgramW6432} '7-zip\7z.exe'

我有一个自动创建的zip文件,无法更改其中的文件夹数

我试图从zip文件中17个文件夹深处的文件夹中提取所有内容。问题是文件夹的名称可能会更改

我开始使用7Zip提取另一个zip文件夹,效果很好:

$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 

有没有办法提取zip文件中17个文件夹深处的文件夹中的内容

您可以使用7Zip的listing函数来获取文件的内容。然后,您可以解析该输出,查找具有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,我找不到只提取文件夹的方法。因此,这是清理结束。但请注意,父文件夹不包含任何其他文件或文件夹。 此外,我必须在末尾使用“启动进程”,否则7Zip将中断变量输入


您可能需要根据ZIP文件结构对其进行一些更改,但这应该会让您继续。

我忽略了问题或您面临的问题:您能否编辑您的问题,以便它清楚地说明您面临的问题?@Bluf done。添加了,所以我的问题是:是否有方法提取zip文件中17个文件夹深处的文件夹中的内容。@Bluf我的答案是:有。请澄清你的问题。具体问题是什么?你自己试过ot吗?请看帮助中心中的。我想您在使用Powershell本机函数的Windows上遇到了最大路径长度问题?@jessehouwing不,我只是不知道如何在Powershell中编写它。我也会做一些类似StartFolder****等的事情吗?这一直到它是移动项(连接路径$extractPath$folderPath)-Destination$extractPath我得到了一个无效的路径错误,但是路径是正确的。我在本地做了一个人工测试,它对我有效,但您的环境中绝对可能存在一些差异。您是否记得更新顶部的路径?你能提供准确的错误信息吗?是的,我已经更改了路径,我应该添加这些是网络路径。错误:无效路径:无效路径:'\\MachineName\shared\GDistribute\Test\QA\WebsiteName\CodeName.Web\Content\R\u C\GO47\u 1\DATA01\2\CodeName\BuildName\Sources\src\DIR\31x\Source\CodeName.Web\obj\Release\PackageTmp'。在第22行,char:1+移动项(加入路径$extractPath$folderPath)-目标$extractPath+类别信息:invalidooperation:(:)[],ArgumentException+FullyQualifiedErrorId:MoveItemDynamicParametersProviderException我看不出不起作用的原因,但这可能是提供程序的问题。尝试将移动项目行更改为:移动项目“文件系统::$(连接路径$extractPath$folderPath)”-目标“文件系统::$extractPath”