バッチ:ファイルの名前を一括変更するにはどうすればよいですか?
-
12-11-2019 - |
質問
Perl でこれを行っている人を見てきましたが、バッチで行う方法はあるのだろうかと疑問に思っています。これは Windows に組み込まれているため、バッチ スクリプトでこれを行う方法を知っておくとより役立つと思います。コンピューターに何もインストールする必要はありません。
入力名の例: myFile_55
出力パターンの例:変化 myFile
に picture
そして数値を 13 減らします。出力例: picture_42
.
これについてはどのようにアプローチしますか?名前を変更するバッチコマンドを知っています。
ren myFile_55 picture_42
.
したがって、という名前のファイルがある場合、 renamer.bat
, 以下を追加できます。
for /r %%x in (%1) do ren "%%x" %2
.
次に、次のコマンドを入力します。
renamer.bat myfile* picture*
.
でも、どうやって数字を減らしたらいいのかわかりません。
正しい解決策はありません
他のヒント
おそらく、元のファイル名を for ループに通し、名前と番号を抽出し、数値を計算して、新しい名前と番号を使用して元に戻すことができます。ファイル名の形式が次のとおりである限り、 name_number
これを使用できます:
REM Allow for numbers to be iterated within the for-loop i.e. the i - z
SETLOCAL ENABLEDELAYEDEXPANSION
SET i=0
SET z=13
SET newName=picture
SET workDir=C:\Path\To\Files
REM Given that filenames are in the format of 'Name_number', we're going to extract 'number
REM and set it to the i variables, then subtract it by 13, then rename the original file
REM to what the newName_x which if the filename was oldName_23 it would now be newName_10
FOR /r %%X in (%1) do (
FOR /F "tokens=1-2 delims=_" %%A IN ("%%X") DO (
SET i=%%B
SET /A x=!i! - %z%
REM ~dpX refers to the drive and path of the file
REN "%%~dpX\%%A_%%B" "%newName%_!x!"
)
)
編集:を編集しました REN
コマンドを使用して、元のファイルのドライブとパスを含めます。混乱を避けるために、小文字の x から大文字の X に変更されました。 %%~dpX
.
所属していません StackOverflow