Last active
November 8, 2020 07:24
-
-
Save gologius/2b3d1ae6b1140d50f077f34a370f9434 to your computer and use it in GitHub Desktop.
コマンドライン引数で指定されたファイルを、ファイル名変換してからPW付で圧縮するバッチ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#コマンドライン引数で指定されたファイルを、ファイル名変換してからPW付で圧縮するバッチ | |
#ファイルを選択→バッチの上に持ってきてドロップすると、簡単に実行できる | |
#定数 | |
$now_yyyymmdd = (Get-Date).ToString("yyyyMMdd") | |
$sevenzip_path = "C:\Program Files\7-Zip\7z.exe" | |
$work_dir_name = "合体データ_" + $now_yyyymmdd | |
$result_file_name = $work_dir_name + ".zip" | |
$password = "114514" | |
# (存在していないなら)ワークディレクトリ作成 | |
if (-Not (Test-Path .\$work_dir_name)) { | |
mkdir .\$work_dir_name | |
} | |
# ワークディレクトリに、引数で指定されたファイルを格納 | |
for($i=0; $i -lt $args.Count; $i++) { | |
Move-Item $args[$i] .\$work_dir_name | |
} | |
# ファイルのリネーム | |
# ワークフォルダ内の全ファイルに対して、複数パターンのファイル名置換をかけ | |
Get-ChildItem .\$work_dir_name | Rename-Item -NewName { $_.Name -replace '00012345_','Aデータ_' } | |
Get-ChildItem .\$work_dir_name | Rename-Item -NewName { $_.Name -replace '00012345-2_','Bデータ_' } | |
# フォルダ圧縮(7zipのコマンドライン機能を使用) | |
# 7z.exe a <圧縮後のファイル名> <圧縮対象のフォルダまたはファイル> <※複数ファイルの場合スペース区切りで記載> -p<パスワード> | |
# ※-p と 指定するパスワードの間にはスペースを入れてはいけない(7zipの仕様らしい) | |
$tmp = "-p" + $password #↓の文に直接展開することができないので、一次変数を経由している | |
&$sevenzip_path a .\$result_file_name .\$work_dir_name $tmp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment