DISTRICT 37

なにか

Powershellでzipファイルを作る

WindowsエクスプローラとではファイルをZIPするには任意のファイル(またはフォルダ)をクリックして選択して、右クリののちにZIPしていると思われる。でもそういったGUIでの操作よりもCUIでできないものかと思った。

ZIPをCUIで作る

Windowsではcmd.exeではなく、powershellでできる。

  • 構文
Compress-Archive -Path <target file> -Destination <file name>
  • 基本形
Compress-Archive -Path a.txt -Destination archive.zip
  • フォルダを指定する
Compress-Archive -Path my_folder -Destination archive.zip
  • 拡張子を指定する場合
Compress-Archive -Path *.log -Destination archive.zip
  • 複数指定する場合
Compress-Archive -Path a.txt, my_folder, *.log -Destination archive.zip
  • cmd.exeから実行する場合
powershell Compress-Archive -Path *.txt -Destination archive.zip

この通りcmd.exeからもコマンドの前にpowershellをつけるだけで実行できるのでファイルをアーカイブするバッチが作れる。そもそもpowershellスクリプトを作ればいいのだが、それはそれ。あとは「zip」とかでエイリアスを作っておくのもいい。

Set-Alias zip Compress-Archive

このCompress-ArchiveコマンドレットはPowerShellV5からの導入なので、バージョンが低い場合は新しいのを入れる必要がある。