我只是試圖在PowerShell中打開一個zip壓縮文件,並將其中的文件移動到特定位置。但它總是隻移動zip文件夾。我究竟做錯了什麼 ?如何解壓文件並將其複製到7zip的特定位置?
這是我現在有:
Get-ChildItem C:\zipplayground\*.zip | % {"C:\Program Files (x86)\7-Zip\7zG.exe";
Move-Item $_ C:\unzipplayground\}
我只是試圖在PowerShell中打開一個zip壓縮文件,並將其中的文件移動到特定位置。但它總是隻移動zip文件夾。我究竟做錯了什麼 ?如何解壓文件並將其複製到7zip的特定位置?
這是我現在有:
Get-ChildItem C:\zipplayground\*.zip | % {"C:\Program Files (x86)\7-Zip\7zG.exe";
Move-Item $_ C:\unzipplayground\}
我認爲正確的答案應該是類似的東西:
Get-ChildItem C:\zipplayground\*.zip | % {& "C:\Program Files\7-Zip\7z.exe" "x" $_.fullname "-oC:\unzipplayground"}
Alroc幾乎是正確的,但是$_.fullname
之間的引號不起作用,並且他缺少-o
參數7z。我使用7z.exe
而不是7zg.exe
,這種方式很好。
作爲參考,命令行幫助可以在這裏找到:http://sevenzip.sourceforge.jp/chm/cmdline/ 基本上,x
代表「提取物」和-o
關於「輸出目錄」
是的,這是謝謝:)唯一的區別是我的7zip路徑。 – RayofCommand
「-o」在路徑之前做了什麼? – RayofCommand
這是輸出目錄的7z參數。它必須「附加」到路徑上,不允許有空間。 – Poorkenny
我沒有7Zip的測試,但我認爲這是失敗,因爲你沒有告訴7Zip的東西進行操作,而你移動你ZIP文件到目的地自己。試試這個:
Get-ChildItem C:\zipplayground\*.zip | % {invoke-expression "C:\Program Files (x86)\7-Zip\7zG.exe x $_.FullName c:\unzipplayground\";}
表達式或語句中的意外標記'x'。要放什麼而不是x?對不起,我是新的。謝謝 – RayofCommand
'x'是正確的,你需要提取zipfile。從Powershell調用該程序是一個語法問題,目前我沒有任何要測試的東西。我認爲你需要移動報價。試試我的編輯。 – alroc
正如我在下面的回答中所提到的,引號之間的「$ _。fullname」無法工作,解析器無法解析變量成員。 – Poorkenny
如果你想避免開放源代碼的EXE/DLL像7zip的然後安裝PSCX模塊PowerShell和使用擴大的存檔。需要注意的是PSCX要求至少.NET 4中(我用4.5)和PowerShell 3.0
函數來獲取的7z.exe
function Get-7ZipExecutable
{
$7zipExecutable = "C:\Program Files\7-Zip\7z.exe"
return $7zipExecutable
}
路徑
函數來壓縮文件夾,其中目的地設置爲
function 7Zip-ZipDirectories
{
param
(
[CmdletBinding()]
[Parameter(Mandatory=$true)]
[System.IO.DirectoryInfo[]]$include,
[Parameter(Mandatory=$true)]
[System.IO.FileInfo]$destination
)
$7zipExecutable = Get-7ZipExecutable
# All folders in the destination path will be zipped in .7z format
foreach ($directory in $include)
{
$arguments = "a","$($destination.FullName)","$($directory.FullName)"
(& $7zipExecutable $arguments)
$7ZipExitCode = $LASTEXITCODE
if ($7ZipExitCode -ne 0)
{
$destination.Delete()
throw "An error occurred while zipping [$directory]. 7Zip Exit Code was [$7ZipExitCode]."
}
}
return $destination
}
函數解壓文件
function 7Zip-Unzip
{
param
(
[CmdletBinding()]
[Parameter(Mandatory=$true)]
[System.IO.FileInfo]$archive,
[Parameter(Mandatory=$true)]
[System.IO.DirectoryInfo]$destinationDirectory
)
$7zipExecutable = Get-7ZipExecutable
$archivePath = $archive.FullName
$destinationDirectoryPath = $destinationDirectory.FullName
(& $7zipExecutable x "$archivePath" -o"$destinationDirectoryPath" -aoa -r)
$7zipExitCode = $LASTEXITCODE
if ($7zipExitCode -ne 0)
{
throw "An error occurred while unzipping [$archivePath] to [$destinationDirectoryPath]. 7Zip Exit Code was [$7zipExitCode]."
}
return $destinationDirectory
}
謝謝Nithin K Anil組織它 –
爲什麼你的主題引用不使用開源DLL,如果你的問題沒有提到它? – alroc
對不起,改變了:) – RayofCommand