【问题标题】:How to extract multiple zips into one folder without creating separate folders with zip name? Powershell如何在不创建带有 zip 名称的单独文件夹的情况下将多个 zip 提取到一个文件夹中?电源外壳
【发布时间】:2018-10-11 06:49:23
【问题描述】:

这是我在 powershell 中的脚本

$today   = (Get-Date).ToString('dd_MM_yyyy')
$LocalPath = "C:\Builds\$today"
New-Item -ItemType directory -Path $LocalPath
$RemotePath = "C:\Builds\zip\$today"
$Max_hours = "-5"
#Max_mins = "-5"
$Curr_date = get-date

#Checking date and then copying file from LocalPath to RemotePath
Foreach($file in (Get-ChildItem $RemotePath))
{
    if($file.LastWriteTime -gt ($Curr_date).addhours($Max_hours))
    {

        Get-ChildItem "C:\Builds\zip\$today\*pc*.*" | % {& "C:\Program Files\7-Zip\7z.exe" "x" "-aoa" $_.fullname "-oC:\Builds\$today"}
    }
    ELSE
    {"not extracting $file"
    }

}

我有几个 *.zip 文件,我想将它们提取到特定文件夹中。问题是,7zip 使用 *.zip 名称创建子文件夹并将文件提取到此文件夹中。

例如。我有 a.zipb.zipc.zip 文件,我希望将它们准确地提取到 Builds 文件夹中。现在,在我的命令之后,它们被提取到:

构建/a/(此处为 a.zip 文件)

Builds/b/(这里是 b.zip 文件)

Builds/c/(这里是 c.zip 文件)

我希望它们都位于具有完整目录路径的 Builds/(此处为 a、b、c 文件)中。 shell中是否有任何7zip选项可以做到这一点?

'-e' 选项提取所有没有文件夹路径的文件,这就是我现在要寻找的。

【问题讨论】:

    标签: windows powershell


    【解决方案1】:

    如果您使用的是 PowerShell v5,则可以使用 Expand-Archive 而不是 7zip:

    Get-ChildItem "C:\Builds\zip\$today\*pc*.*" | % {Expand-Archive $_ -DestinationPath "C:\Builds\$today"}
    

    编辑:

    我在使用该命令时没有创建文件夹。

    包含文件的压缩文件:

    运行Expand-Archive C:\aaaa\nuget350.zip -DestinationPath C:\bbbb

    提取的内容:

    【讨论】:

    • 您确定 zip 文件中没有文件夹,因为文件夹不是由命令创建的。我已经编辑了我的帖子以显示一个示例。
    • 啊,这就是我困惑的问题,因为几天前一切都很好,但现在我看到在最新版本中,我的客户将所有东西都打包在了那个子文件夹中,所以我现在的问题毫无意义。谢谢你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    相关资源
    最近更新 更多