Batch file to rename "*.txt" files in the same directory according to the name of the parent directory
This batch file is placed in the same directory as the *.txt files
For a folder name like "Materials each 3", the 3 can also be greater than 10; below I use 3 as an example
".txt" becomes "---3.txt", where 3 is the number in the folder name
If already contains "---3", for example: "a---3.txt", then it does not need to be renamed; if it is "---2", then change the 2 to 3
If there are files like "a.txt" and "a---3.txt", then change "a.txt" to "a1---3.txt"; if "a1---3.txt" also exists, then keep incrementing it to "a2---3.txt". In other words, if there is a duplicate name, just vary it with a number, but the "---3" must not change.
Finally count the number of *.txt files, multiply the count by 3 to get a number N, and rename the directory to "Materials each 3---N"
=======================
Example:
Original It should become like this
a.txt a---3.txt
b.txt b---3.txt
c-----3.txt c-----3.txt (if there are more than 3 "-" symbols, it also does not need to be changed)
a a.txt a a---3.txt (there may be spaces in the filename, and there may also be Chinese characters)
d.txt d2---3.txt
d---3.txt d---3.txt
d1---3.txt d1---3.txt
e.txt e1---3.txt
e---3.txt e---3.txt
e1.txt e11---3.txt
f---2.txt f---3.txt
Finally the folder name should be changed to "Materials each 3---33", and if the batch file is run again, only the 33 should change.
=======================
This is what I wrote myself (I don't really understand it very well, I just used it as-is; I referred to the "rename" posts found by searching this site):
---------------
set a=---2
for /f "delims=" %%i in ('dir /b *.txt') do ren "%%i" "%%~ni%a%.txt"
pause
---------------
I don't know how to write the conditional checks, so after running it repeatedly, names like "a---3---3.txt" appear. That's only natural, so I'm asking everyone here to help.
Maybe what I'm asking is a bit too much, sorry. I'm also modifying the batch file and thinking it over myself.
[ Last edited by wxcute on 2007-9-4 at 04:25 PM ]
This batch file is placed in the same directory as the *.txt files
For a folder name like "Materials each 3", the 3 can also be greater than 10; below I use 3 as an example
".txt" becomes "---3.txt", where 3 is the number in the folder name
If already contains "---3", for example: "a---3.txt", then it does not need to be renamed; if it is "---2", then change the 2 to 3
If there are files like "a.txt" and "a---3.txt", then change "a.txt" to "a1---3.txt"; if "a1---3.txt" also exists, then keep incrementing it to "a2---3.txt". In other words, if there is a duplicate name, just vary it with a number, but the "---3" must not change.
Finally count the number of *.txt files, multiply the count by 3 to get a number N, and rename the directory to "Materials each 3---N"
=======================
Example:
Original It should become like this
a.txt a---3.txt
b.txt b---3.txt
c-----3.txt c-----3.txt (if there are more than 3 "-" symbols, it also does not need to be changed)
a a.txt a a---3.txt (there may be spaces in the filename, and there may also be Chinese characters)
d.txt d2---3.txt
d---3.txt d---3.txt
d1---3.txt d1---3.txt
e.txt e1---3.txt
e---3.txt e---3.txt
e1.txt e11---3.txt
f---2.txt f---3.txt
Finally the folder name should be changed to "Materials each 3---33", and if the batch file is run again, only the 33 should change.
=======================
This is what I wrote myself (I don't really understand it very well, I just used it as-is; I referred to the "rename" posts found by searching this site):
---------------
set a=---2
for /f "delims=" %%i in ('dir /b *.txt') do ren "%%i" "%%~ni%a%.txt"
pause
---------------
I don't know how to write the conditional checks, so after running it repeatedly, names like "a---3---3.txt" appear. That's only natural, so I'm asking everyone here to help.
Maybe what I'm asking is a bit too much, sorry. I'm also modifying the batch file and thinking it over myself.
[ Last edited by wxcute on 2007-9-4 at 04:25 PM ]

