1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Windows】複数ファイルを個別ZIP圧縮するドラッグ&ドロップ対応バッチファイル

Posted at

複数のファイル・フォルダーを一括で個別のZIPファイルに圧縮するバッチファイルです。
ドラッグ&ドロップするだけで、各ファイルが同じディレクトリに個別ZIPとして作成されます。

コード

@echo off
setlocal EnableExtensions

:next
if "%~1"=="" goto :done

rem フルパス化(D&D/相対パス両対応)
set "P=%~f1"

rem 引数は環境変数経由($env:P)で PowerShell へ渡す
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
  "$ErrorActionPreference='Stop';" ^
  "$p = $env:P;" ^
  "$item = Get-Item -LiteralPath $p;" ^
  "if ($item.Extension -ieq '.zip') { return };" ^
  "$base = if ($item.PSIsContainer) { $item.Name } else { ('{0}_{1}' -f $item.BaseName, $item.Extension.Trim('.')) };" ^
  "$dir  = if ($item.DirectoryName) { $item.DirectoryName } else { (Get-Location).Path };" ^
  "$zip  = Join-Path $dir ($base + '.zip');" ^
  "$n    = 1;" ^
  "while (Test-Path -LiteralPath $zip) { $zip = Join-Path $dir ('{0}({1}).zip' -f $base, $n); $n++ };" ^
  "Compress-Archive -LiteralPath $item.FullName -DestinationPath $zip -Force;"

shift
goto :next

:done
pause

主な特徴

✅ ドラッグ&ドロップ対応
✅ 重複名自動回避(連番付与)
✅ Windows標準機能のみ使用
✅ ファイル・フォルダー両対応

使用方法

  1. 上記コードを.batファイルとして保存
  2. 圧縮したいファイル/フォルダーをドラッグ&ドロップ
  3. 完了!

詳細解説・応用例・トラブルシューティング

このバッチファイルの詳細な動作原理カスタマイズ方法PowerShellコマンド版よくあるトラブルと解決策については、以下の記事で詳しく解説しています。

📖 Windows11 - 複数のファイル・フォルダーを個別にZip圧縮する方法|追加ソフト不要

上記記事では以下の内容を扱っています:

  • PowerShellワンライナー版
  • GUIでのドラッグ&ドロップ版
  • 詳細なコード解説
  • カスタマイズ例(圧縮レベル変更、保存先指定など)
  • トラブルシューティング
  • 実行ポリシーの設定方法

まとめ

Windows環境での繰り返し作業の自動化に最適なツールです。
開発環境でのファイル管理や、業務でのドキュメント整理にぜひご活用ください!

#Windows #バッチファイル #PowerShell #自動化 #ZIP圧縮

1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?