0
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?

More than 1 year has passed since last update.

Windowsでファイルを連番付きで大量にコピーしたい

Posted at

やりたいこと

コマンドラインを一切使わずに(ここが重要)、スクリプトファイルをダブルクリックするとカレントディレクトリのファイルに連番をつけてコピーされるようにしたい。ユーザーはスクリプトファイル自体を(メモ帳などで)書き換えられることを想定する。環境はWindows。

使い方

インストールするものを極力減らしたいので、Windowsのバッチファイルを採用する。以下を書き込んだファイルをコピーしたいファイルがあるディレクトリにコピーして、ファイル名と番号を書き換えて保存し、ダブルクリックするだけ。

コード

copy_with_serial_number.bat
@echo off

setlocal enabledelayedexpansion

for /l %%i in (1, 1, 100) do (
    set formatted_num=0000%%i
    set formatted_num=!formatted_num:~-4!
    copy test.tif test_!formatted_num!.tif
)

endlocal

ひっかかったポイント

ほぼ初めてバッチファイルを作ったので、ありとあらゆる段階でひっかかった。この言語、ひいき目に見てく(以下、略)。ただし、フォーマット構文はシンプルで素敵。

for文のかっこ

for /l %%i in (1, 1, 100) do (
    rem 処理したい内容
)

のところで、「()」を「{}」にしてしまった。あと、コメント行の「rem」ってなんやねん。

for文の中でローカル変数を変更したい時には

setlocal enabledelayedexpansion

rem ここに本文

endlocal

のように記述しなければいけない。「enebledelayedexpansion」っておまじない、おまじないすぎるやろ!笑 あと、変数の参照は普段は「%変数名%」だけど、ローカル変数を参照するときには「!変数名!」としなければいけない。

0
0
0

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
0
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?