Batch processing is really simple and powerful for text/file operations! There is no need for any complicated code. Just a note pad plus concise code can handle most text/file operations. The following records some of the codes I have used, which are basically some of the codes I answered to friends on Baidu, which not only serves as reference for the Jikes, but also serves as a memorandum. . . .
Case 1.
I want to name all the files in a folder 001,002... In this way, it doesn't matter what order it is. As long as the name changes, it's fine. Who can help me write it?
The code I gave:
@echo off setlocal enabledelayedexpansion set var=1000 for /r "%~dp0" %%i in (*.*) do ( set /a var+=1 if not "%%~nxi"=="%~nx0" ren "%%i" !var:~-3!%%~xi ) pause>nul echo Processing is complete,Exit any key... exit
A brief explanation:
1. Use for /r to traverse all files in the current directory (%~dp0), which is more efficient than for /f + dir /s /b;
The not statement is used to exclude the bat file itself. The full path is %0, and it expands to the bat file name: %~nx0;
3. Use variable 1000 to start accumulating, and use!var:~-3! to obtain the last three digits, that is, 001, 002, 003, respectively...
Ps: var=1000, then this script renames up to 999 files. If there are more, there will be an overwrite error. How to modify it? Please think about it, it is best to leave your code in the comments, haha! (It's relatively simple, please do not slap the bricks with big prawns!)
Case II.
"For example, I have a folder with files all X1, X2...X51. I have a file name that becomes [Yousei-raws] Soul Eater 14 [BDrip 1280x720 x264 FLAC].ass code is as follows:
@echo off set a=0 setlocal EnableDelayedExpansion for %%n in (*.ass) do ( ren "%%n" "[Yousei-raws] Soul Eater !a! [BDrip 1280x720 x264 FLAC].ass" set /A a+=1 )
How to make the order correct? By the way, the last 51 becomes 1.
The code I gave:
@echo off set a=100 setlocal EnableDelayedExpansion for /f "delims=*" %%n in ('dir /b *.ass') do ( set /a a+=1 ren "%%n" "[Yousei-raws] Soul Eater !a:~-2! [BDrip 1280x720 x264 FLAC].ass" )
Simple explanation: Because the value of a is initialized to 0, the first file is 0, the second file is 1, and the 15th is 14. . . The code has changed the order, first give a+1, and let the value of a is 1 when renamed.
Ps: The efficiency of using for+dir here will be a little lower, but the error rate will also be reduced, which is easy to understand! Of course, you can use for /r.