[英]unzip zip files into multiple folders with powershell
I have 5 zip files and 5 folders respectively. 我分别有5个zip文件和5个文件夹。 Files are: FranceData.zip, USAdata.zip, GermanData.zip, Italydata.zip and DanemarkData.zip. 文件为:FranceData.zip,USAdata.zip,GermanData.zip,Italydata.zip和DanemarkData.zip。 Folders are: FranceFolder, USAFolder, GermanFolder, ItalyFolder and DanemarkFolder. 文件夹为:France文件夹,USA文件夹,German文件夹,Italy文件夹和Danemark文件夹。 I would like to know how would I unzip these files into their respective folders using Array in Powershell. 我想知道如何使用Powershell中的Array将这些文件解压缩到各自的文件夹中。 I am knew to Powershell and need help. 我对Powershell很了解,需要帮助。 Thank you 谢谢
This can be done using the Expand-Archive
cmdlet, which is available in PowerShell 5 and above. 可以使用Expand-Archive
cmdlet(在PowerShell 5及更高版本中可用)完成此操作。 (PowerShell's version can be checked via the $PSVersionTable
variable.) (可以通过$PSVersionTable
变量检查PowerShell的版本。)
To extract a zip file to a specific folder the following syntax is used: 要将zip文件提取到特定文件夹,请使用以下语法:
Expand-Archive -Path 'ZipFile.zip' -DestinationPath 'ZipFolder'
or 要么
Expand-Archive -LiteralPath 'ZipFile.zip' -DestinationPath 'ZipFolder'
If the -Path
parameter is used, PowerShell will recognise characters such as *
, ?
如果使用-Path
参数,PowerShell将识别*
-Path
等字符?
, [
, ]
, as wildcards, which can cause unexpected behaviour for file-paths that contain square-brackets. , [
, ]
作为通配符,可能导致包含方括号的文件路径出现意外行为。
If the -LiteralPath
parameter is used, PowerShell will not treat any characters as wildcard characters. 如果使用-LiteralPath
参数,PowerShell将不会将任何字符视为通配符。
Assuming all your zip files and folders follow the same naming pattern you could use an array like so: 假设所有zip文件和文件夹都遵循相同的命名模式,则可以使用如下数组:
$Countries = @(
'France',
'USA',
'German'
'Italy',
'Danemark'
)
foreach ($Country in $Countries)
{
$ZipFilePath = $Country + 'Data.zip'
$DestinationPath = $Country + 'Folder'
Expand-Archive -LiteralPath $ZipFilePath -DestinationPath $DestinationPath
}
If your files and folders don't follow the same naming pattern you could use a dictionary (or a collection of KeyValuePair
s), like so: 如果文件和文件夹的命名方式不同,则可以使用字典(或KeyValuePair
的集合),如下所示:
$ZipFilesAndFolders = @{
'FranceData.zip' = 'FranceFolder'
'USAData.zip' = 'USAFolder'
'GermanData.zip' = 'GermanFolder'
'ItalyData.zip' = 'ItalyFolder'
'DanemarkData.zip' = 'DanemarkFolder'
}
foreach ($KeyAndValue in $ZipFilesAndFolders.GetEnumerator())
{
$ZipFilePath = $KeyAndValue.Key
$DestinationPath = $KeyAndValue.Value
Expand-Archive -LiteralPath $ZipFilePath -DestinationPath $DestinationPath
}
With PowerShell 4 (and 3) 使用PowerShell 4(和3)
If you have .Net Framework 4.5 installed, you can use Microsoft.PowerShell.Archive
which was created by the PowerShell team. 如果安装了.Net Framework 4.5,则可以使用由PowerShell团队创建的Microsoft.PowerShell.Archive
。
PowerShell 4 requires .Net Framework 4.5 so this should work without any system changes needed. PowerShell 4需要.Net Framework 4.5,因此无需任何系统更改即可正常运行。
The module can be found at https://github.com/PowerShell/Microsoft.PowerShell.Archive 可以在https://github.com/PowerShell/Microsoft.PowerShell.Archive中找到该模块
The syntax used is identical, the function is Expand-Archive
and the parameters are -Path
, -LiteralPath
, and -DestinationPath
. 所使用的语法是相同的,该函数是Expand-Archive
和参数是-Path
, -LiteralPath
,和-DestinationPath
。
Just ensure that you have imported the module before using it, this can be done using the Import-Module
cmdlet like so: 只需确保在使用之前导入了模块,就可以使用Import-Module
cmdlet来完成此操作,如下所示:
Import-Module -Name 'Microsoft.PowerShell.Archive' -Force
PowerShell 2 PowerShell 2
A solution for PowerShell 2 can be found here: https://stackoverflow.com/a/37814462/9447234 可在以下位置找到适用于PowerShell 2的解决方案: https : //stackoverflow.com/a/37814462/9447234
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.