PowerShellでZIP圧縮する。(その2)
IE7に対応。
置換確認ダイアログを出さずに置換します。
MakeExZIP.ps1 ZIPファイル ファイル...
if($args.length -lt 2){
write-output "Arguments Missing.";
write-output "Usage: MakeExZIP.PS1 ZIPfile files...";
return;
}
if([System.IO.Path]::GetExtension($args[0]) -ne ".zip"){
write-output ("Invalid Extension Name - " + $args[0]);
return;
}
$ie = $null;
if([System.IO.File]::Exists($args[0])){
[void][reflection.assembly]::LoadWithPartialName("'Microsoft.VisualBasic");
# IE7以降も可
$Shell=new-object -com Shell.Application;
$ZIPfile=$Shell.NameSpace([System.IO.Path]::GetFullPath($args[0])).Self.Path;
$Shell.ShellExecute("explorer.exe",$ZIPfile,$null,$null,0);
while($true){
foreach($ie in $Shell.Windows()){
if($ie.Visible){}
elseif([Microsoft.VisualBasic.Information]::TypeName($ie.Document) -like "IShellFolderViewDual*"){
if($ie.Document.Folder.Self.Path -eq $ZIPfile){break;}
}
$ie=$null;
}
if($ie){break;}
Start-sleep -milliseconds 100;
}
# $ie=new-object -com InternetExplorer.Application; #IE7以降ダメ
# $ie=[Microsoft.VisualBasic.Interaction]::GetObject("new:{C08AFD90-F2A1-11D1-8455-00A0C91F3880}"); #IE7以降も可
# $ie.Navigate([System.IO.Path]::GetFullPath($args[0]));
while($ie.Busy -or ($ie.ReadyState -ne 4)){
Start-sleep -milliseconds 100;
}
$Shell=$ie.Document.Application;
$zFolder=$ie.Document.Folder;
}else{
set-content $args[0] ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18));
$shell=new-object -com shell.application;
$zFolder = $Shell.NameSpace([System.IO.Path]::GetFullPath($args[0]));
}
for($k=1;$k -lt $args.length;$k++){
$File=[System.IO.Path]::GetFileName($args[$k]);
$sFolder=$Shell.NameSpace([System.IO.Path]::GetFullPath($args[$k]+"\.."));
$sFolderItem=$sFolder.ParseName($File);
if(-not $sFolderItem){
write-output ("File Not Found. - " + $File);
break;
}
$zFolderItem=$zFolder.ParseName($File);
if($zFolderItem){
if($tFolderName -eq $null){
$tFolderName = [System.IO.Path]::GetTempFileName();
[System.IO.File]::Delete($tFolderName);
[void][System.IO.Directory]::CreateDirectory($tFolderName);
$tFolder = $Shell.NameSpace(($tFolderName));
}
$Count = $zFolder.Items().Count;
$zFolderItem.InvokeVerb("cut");
$tFolder.Self.InvokeVerb("paste");
while($zFolder.Items().Count -ne $Count - 1){
Start-sleep -milliseconds 1000;
}
$zFolderItem = $null;
}
$Count = $zFolder.Items().Count;
$zFolder.CopyHere($sFolderItem);
while($zFolder.Items().Count -eq $Count + 1){
Start-sleep -milliseconds 1000;
}
$sFolderItem = $null;
}
$tFolder = $null;
$zFolder = $null;
$Shell = $null;
if($ie){$ie.Quit();}
[void][Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic")
while([Microsoft.VisualBasic.Information]::TypeName($ie) -eq "IWebBrowser2"){
Start-sleep -milliseconds 1000;
}
if($tFolderName){
[System.IO.Directory]::Delete($tFolderName,$true);
}