中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-07 13:37
48,040 topics / 350,125 posts / today 0 new / 48,252 members
DOS批处理 & 脚本技术(批处理室) » [Help] Batch rename "*.txt" files in the same directory with a batch file
Printable Version  3,964 / 17
Floor1 wxcute Posted 2007-09-04 13:39
中级用户 Posts 211 Credits 458
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 ]
Floor2 slore Posted 2007-09-04 13:43
铂金会员 Posts 2,478 Credits 5,212
Floor3 wxcute Posted 2007-09-04 13:49
中级用户 Posts 211 Credits 458
Thanks, I'll take a look first

==========
I looked at it
Some of them say the specified file cannot be found, and repeated runs also will

[ Last edited by wxcute on 2007-9-4 at 01:53 PM ]
Floor4 slore Posted 2007-09-04 14:05
铂金会员 Posts 2,478 Credits 5,212
Floor5 wxcute Posted 2007-09-04 14:10
中级用户 Posts 211 Credits 458
I referred to the works of the veterans and improved it a little, now it can prompt for the number to input
==========
set /p a=Enter the number N to rewrite, format (for example: ---3):
for /f "delims=" %%i in ('dir /b *.txt') do ren "%%i" "%%~ni%a%.txt"
pause
==========

[ Last edited by wxcute on 2007-9-4 at 04:44 PM ]
Floor6 wxcute Posted 2007-09-04 16:03
中级用户 Posts 211 Credits 458
slore
Hello, using the method in your post #4, among these
=====
d.txt
e.txt
f---2.txt
=====
they can't be changed

[ Last edited by wxcute on 2007-9-4 at 04:19 PM ]
Floor7 slore Posted 2007-09-04 16:12
铂金会员 Posts 2,478 Credits 5,212
f--2.txt, what do you want to change it to? Didn't you say that if it already is that way then it shouldn't be changed? And repeated runs shouldn't append more, right?

I tested it...

Floor8 wxcute Posted 2007-09-04 16:21
中级用户 Posts 211 Credits 458
What I mean is, when I set the number to 3, I want to change the 2 to 3

Also thanks for even testing it for me, you're really a good person

My question is a bit complicated, it may be troublesome. Please take another careful look at the topic in post #1

[ Last edited by wxcute on 2007-9-4 at 04:24 PM ]
Floor9 slore Posted 2007-09-04 16:55
铂金会员 Posts 2,478 Credits 5,212
@echo off
Set a=--5
for /f "tokens=1,2* delims=-" %%i in ('dir /b *.txt') do (
if "%%j"=="" (ren "%%i" "%%~ni%a%.txt") else (ren "%%i--%%j" "%%~ni%a%.txt")
)
pause


Only now did I notice that your j---2 has 3 of them! -
Floor10 wxcute Posted 2007-09-05 12:06
中级用户 Posts 211 Credits 458
There are still problems, because the minimum number of " - " symbols has to be 3,

If there are more than 3 " - ", only change the number, for example:
c-----3, has five -, the number is correct, no change
g----2, has four -, the number is wrong, should become: g----3

If there is only one " - ", the name does not change directly; just add "---3" at the end, for example:
a-1, becomes a-1---3
b-3, becomes b-3---3

If there are two " - ", then add one more " - " symbol, for example:
e--3, becomes e---3
f--2, becomes f---3

If any of the above causes a duplicate name, then a number also needs to be added. For example:
d--2, should become d1---3
d---3, no change
Floor11 wxcute Posted 2008-02-12 05:21
中级用户 Posts 211 Credits 458
!T! is the connector
!a! is the set quantity
Put it in the txt file directory and run it; repeated runs will not append more.
The number of connectors cannot be determined exactly, but it can determine more than three and less than three; basically it counts as meeting the requirements.



[ Last edited by wxcute on 2008-2-12 at 05:22 AM ]
Floor12 wxcute Posted 2008-02-12 05:23
中级用户 Posts 211 Credits 458
Test files
-------------------
Original file|Target file
========
a.txt|a---3.txt
b.txt|b---3.txt
c-----3.txt|c---3.txt
a a.txt|a a---3.txt

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
g--3.txt|g---3.txt
h--2.txt|h---3.txt
i----4.txt|i---3.txt

j---.txt|j---3.txt
k--.txt|k---3.txt
l----.txt|l---3.txt
Floor13 slore Posted 2008-02-12 07:11
铂金会员 Posts 2,478 Credits 5,212
2007-9-5 12:06……

It's been a long time~
Floor14 wxcute Posted 2008-02-12 08:13
中级用户 Posts 211 Credits 458
Yeah, I've been working hard to learn all along. I ran into this problem again today, so I started tackling it.
Floor15 pinghu Posted 2008-02-12 12:15
中级用户 Posts 130 Credits 307
OP, what are you doing this for? Could you explain?
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023