使用PowerShell创建Zip文件

使用PowerShell创建Zip文件,powershell,scripting,powershell-2.0,Powershell,Scripting,Powershell 2.0,我在C:\Desktop\Mobile的位置有以下文件 Apple_iphone6.dat Apple_iphone7.dat Samsung_edge7.dat Samsung_galaxy.dat Sony_experia.dat Sony_M2.dat 我需要创建一个脚本,将类似的文件写入一个zip。因此,Apple_iphone6.dat和Apple_iphone7.dat文件必须放在一个zip中。 因此,最终创建的zip文件将是: Ap

我在C:\Desktop\Mobile的位置有以下文件

    Apple_iphone6.dat
    Apple_iphone7.dat
    Samsung_edge7.dat
    Samsung_galaxy.dat
    Sony_experia.dat
    Sony_M2.dat
我需要创建一个脚本,将类似的文件写入一个zip。因此,Apple_iphone6.dat和Apple_iphone7.dat文件必须放在一个zip中。 因此,最终创建的zip文件将是:

Apple_Files_Timestamp.zip
Samsung_Files_Timestamp.zip
Sony_Files_Timestamp.zip
我试过这个

Get-ChildItem C:\Desktop\Mobile -Recurse -File -Include *.dat | Where-Object { $_.LastWriteTime -lt $date } | Compress-Archive -DestinationPath C:\Desktop\Mobile
但它给我的错误是“压缩存档”未被识别为cmdlet的名称


如何才能使此代码正常工作?

在Powershell 2.0中,您不能使用压缩存档,您需要下载原始终端可执行文件以压缩和解压文件

您可以使用:

zip <path> <zip_name> -i <pattern_files>

如果您需要使用临时压缩选项,请访问。

对于Powershell 2.0,您不能使用压缩存档,您需要下载原始终端可执行文件以压缩和解压缩文件

您可以使用:

zip <path> <zip_name> -i <pattern_files>

如果您需要使用临时zip选项,请访问。

Pre Powershell v5您可以使用此选项。无需额外下载

$FullName = "Path\FileName"
$Name = CompressedFileName
$ZipFile = "Path\ZipFileName"
$Zip = [System.IO.Compression.ZipFile]::Open($ZipFile,'Update')
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($Zip,$FullName,$Name,"optimal")
$Zip.Dispose()

在Powershell v5之前,您可以使用此功能。无需额外下载

$FullName = "Path\FileName"
$Name = CompressedFileName
$ZipFile = "Path\ZipFileName"
$Zip = [System.IO.Compression.ZipFile]::Open($ZipFile,'Update')
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($Zip,$FullName,$Name,"optimal")
$Zip.Dispose()

您有两个问题,我将尝试总结这两个问题

1。压缩文件

为了使用Compress-Archive命令,您需要使用@LotPings已经注释过的PowerShell 5。你可以:

  • 在v5附带的Windows 10计算机或Server 2016上运行脚本
  • 下载并安装PoSh 5,请参阅
如果你不能做到这两个,你可以

  • 从PowerShell gallery安装一些模块,通过7-zip工具提供类似功能。搜索结果为。使用前下载并检查这些模块
  • 使用.NET4.5类,在这里检查堆栈溢出
2。组文件

一旦您对文件进行分组,就可以很容易地将它们通过管道传输到压缩命令,就像您已经尝试过的那样。通过以下方式可以实现适当的分组:

$Files = Get-ChildItem 'C:\Desktop\Mobile'
$Groups = $Files | ForEach-Object {($_.Name).split('_')[0]} | Select-Object -Unique

foreach ($Group in $Groups) {
    $Files | where Name -Match "^$Group" | Compress-Archive "C:\Desktop\Mobile\$Group.7z"
}

您有两个问题,我将尝试总结这两个问题

1。压缩文件

为了使用Compress-Archive命令,您需要使用@LotPings已经注释过的PowerShell 5。你可以:

  • 在v5附带的Windows 10计算机或Server 2016上运行脚本
  • 下载并安装PoSh 5,请参阅
如果你不能做到这两个,你可以

  • 从PowerShell gallery安装一些模块,通过7-zip工具提供类似功能。搜索结果为。使用前下载并检查这些模块
  • 使用.NET4.5类,在这里检查堆栈溢出
2。组文件

一旦您对文件进行分组,就可以很容易地将它们通过管道传输到压缩命令,就像您已经尝试过的那样。通过以下方式可以实现适当的分组:

$Files = Get-ChildItem 'C:\Desktop\Mobile'
$Groups = $Files | ForEach-Object {($_.Name).split('_')[0]} | Select-Object -Unique

foreach ($Group in $Groups) {
    $Files | where Name -Match "^$Group" | Compress-Archive "C:\Desktop\Mobile\$Group.7z"
}
  • 下面的脚本执行分组操作
  • 拉链命令取决于您选择的拉链

样本输出:

> .\SO_44030884.ps1
   Samsung_edge7.dat --> Samsung_Files_20170517081753.zip
  Samsung_galaxy.dat --> Samsung_Files_20170517081753.zip
   Apple_iphone6.dat --> Apple_Files_20170517081753.zip
   Apple_iphone7.dat --> Apple_Files_20170517081753.zip
         Sony_M2.dat --> Sony_Files_20170517081753.zip
    Sony_experia.dat --> Sony_Files_20170517081753.zip
此链接可能会有所帮助
  • 下面的脚本执行分组操作
  • 拉链命令取决于您选择的拉链

  • 样本输出:

    > .\SO_44030884.ps1
       Samsung_edge7.dat --> Samsung_Files_20170517081753.zip
      Samsung_galaxy.dat --> Samsung_Files_20170517081753.zip
       Apple_iphone6.dat --> Apple_Files_20170517081753.zip
       Apple_iphone7.dat --> Apple_Files_20170517081753.zip
             Sony_M2.dat --> Sony_Files_20170517081753.zip
        Sony_experia.dat --> Sony_Files_20170517081753.zip
    

    此链接可能会有所帮助

    需要PowerShell版本5+,我会首先使用make name
    gci*.dat | group{($\uName).split(“”“)[0]}
    ,然后使用第三方工具(如7zip)将这些组迭代到zip。我刚刚意识到我使用的是较低版本的Power Shell。此外,我的代码将在一个单一的zip压缩一切。但我需要把它分组。一些代码示例将真正帮助我。需要PowerShell版本5+,我首先按make name
    gci*.dat | group{($\u.name).split('''u')[0]}
    对组进行分组,然后使用第三方工具(如7zip)将组迭代到zip。我刚刚意识到我使用的是较低版本的Power Shell。此外,我的代码将在一个单一的zip压缩一切。但我需要把它分组。一些代码示例对我真的很有帮助。$destination='C:\Temp'$Files=Get ChildItem'C:\Desktop\Mobile.,*.dat'$Groups=$Files | ForEach对象{($..Name).split('.'''''.[0]}}|选择对象-唯一的ForEach($Groups中的组){$Files |其中Name-Match“^$Group”|添加类型-汇编“system.io.compression.Files”[io.compression.zipfile::CreateFromDirectory($Files,$destination)}这不会创建任何zip文件。这里缺少什么?用xxx.zip指定-DestinationPath,如下所示:
    | Compress Archive-DestinationPath xxx.zip
    将抑制7z不支持错误和请求目标路径$destination='C:\Temp'$Files=Get ChildItem'C:\Desktop\Mobile.*.dat'$Groups=$Files | ForEach对象{($\uu.Name).split('''[0]}|选择Object-Unique foreach($Groups中的Group){$Files |其中Name-Match“^$Group”| Add Type-assembly“system.io.compression.filesystem”[io.compression.zipfile]::CreateFromDirectory($Files,$destination)}这不会创建任何zip文件。这里缺少什么?用xxx.zip指定-DestinationPath,如下所示:
    |压缩存档-DestinationPath xxx.zip
    将抑制7z不支持错误,并且请求目标路径v5.1.16299.251的提示会抱怨TypeNotFound``主要次要生成修订------------5 1 16299 251```v5.1.16299.251抱怨TypeNotFound“主要-次要构建版本--------------5 1 16299 251”`真正的问题是,为什么仍然使用v2?更新它。真正的问题是,为什么你还在使用v2?更新它。