【仕事効率化】PowerShellでフォルダ圧縮を自動化する方法




圧縮の基本操作


PowerShellでフォルダ圧縮をすれば、ルーチンワーク効率化を図ることが出来ます。

最終的にダブルクリックだけで圧縮が完了するので、かなり楽になりました。

PowerShellを使ったフォルダ圧縮の自動化について、簡単に書いていきます。

圧縮コマンド

Compress-Archive -Path 「圧縮したいファイル(フォルダ)」 -DestinationPath 「出力先」

 

 絶対パスの場合

Compress-Archive -Path C:\Users\Ponta\Documents\BatFileTest\Power-Shell\press\test01 -DestinationPath C:\Users\Ponta\Documents\BatFileTest\Power-Shell\press\圧縮済み

 相対パスの場合

Compress-Archive -Path .\test01 -DestinationPath .\圧縮済み

上のコマンドを実行すれば、-DestinationPathで指定したパスにzipが作成されます。
ただし-Pathのフォルダが空だと何も作成されないので気をつけてください。

注意点:フォルダパスは各自で変更しないと動作しないです。

 

よくわからないよ!って人はとりあえず絶対パスで指定しておきましょう。

 ・絶対パスの探し方

赤枠の中をクリックすればフォルダの絶対パスが分かります。

自分が使用したいフォルダパスを「-Path」「-DestinationPath」の後ろにコピーして置き換えてください。

フォルダパスを変数で指定

PowerShellでも他の言語と同じように変数が使えます
$ + 変数名 で型宣言はせずに使用可能

#圧縮対象ファイルのパス
$targetFIlePath = 'C:\Users\Ponta\Documents\BatFileTest\Power-Shell\press\test01'

#生成するzipのファイル名
$ouFileName = "圧縮済み"

Compress-Archive $targetFIlePath -DestinationPath (".\" + $ouFileName)

 

・フォルダのバックアップ等で出力ファイル名に日付を入れたい場合

#圧縮対象ファイルのパス
$targetFIlePath = 'C:\Users\Ponta\Documents\BatFileTest\Power-Shell\press\test01'

#生成するzipのファイル名
$zipFileName = "圧縮済み"

#現在日付を取得
$date = Get-Date -Format "yyyyMMdd"

$joindFileName = $date + $zipFileName

Compress-Archive $targetFIlePath -DestinationPath (".\" + $joindFileName)



複数ファイルの圧縮

-updateオプションをつけることで、既に存在するzipに追加できる

#圧縮対象ファイルのパス
$targetFIlePaths = @(
'C:\Users\Ponta\Documents\BatFileTest\Power-Shell\press\test01'
'C:\Users\Ponta\Documents\BatFileTest\Power-Shell\press\test02'
'C:\Users\Ponta\Documents\BatFileTest\Power-Shell\press\test03'
)
#出力先フォルダのパス
$DestinationPath = ".\"

#生成するzipのファイル名
$zipFileName = "圧縮済み"

$date = Get-Date -Format "yyyyMMdd"
$joindFileName = $date + $zipFileName

$DestinationPath = $DestinationPath + $joindFileName

#最初のファイルのみzipを新規生成
Compress-Archive $targetFIlePaths[0] -DestinationPath $DestinationPath

# $targetFIlePathsの要素をzipに追加する
for($i = 1; $i -lt $targetFIlePaths.Count; $i++){
Compress-Archive $targetFIlePaths[$i] -DestinationPath $DestinationPath -Update
}

power-shellコマンドを保存して簡単に呼び出す方法

簡単に言えばバッチからpower-shellのファイルをを呼び出します。
よく分からなくても①、②をすればきっと動くので安心してください!

①PowerShellのコードを「.ps1」(PowerShellスクリプトの拡張子)で保存しておく
②バッチファイル(.bat)からps1を呼び出す

 

①お好みのフォルダにmain.batを作成し、以下のコードをコピペします。

powershell -NoProfile -ExecutionPolicy Unrestricted .\pressScript.ps1
pause

 

②main.batと同じフォルダにpressScript.ps1を作成し、以下のコード(実行したいPowerShellのコード)を貼り付けます。

#圧縮対象ファイルのパス

$targetFIlePaths = @(
'C:\Users\Ponta\Documents\BatFileTest\Power-Shell\press\test01'
'C:\Users\Ponta\Documents\BatFileTest\Power-Shell\press\test02'
'C:\Users\Ponta\Documents\BatFileTest\Power-Shell\press\test03'
)
#出力先フォルダのパス
$DestinationPath = ".\"

#生成するzipのファイル名
$zipFileName = "圧縮済み"

$date = Get-Date -Format "yyyyMMdd"
$joindFileName = $date + $zipFileName

$DestinationPath = $DestinationPath + $joindFileName

#最初のファイルのみzipを新規生成
Compress-Archive $targetFIlePaths[0] -DestinationPath $DestinationPath

# $targetFIlePathsの要素をzipに追加する
for($i = 1; $i -lt $targetFIlePaths.Count; $i++){
Compress-Archive $targetFIlePaths[$i] -DestinationPath $DestinationPath -Update
}

main.batをダブルクリックすればPowerShellコードが実行されるはずです。




コメント

タイトルとURLをコピーしました