China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-07 12:51
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] Batch rename "*.txt" files in the same directory with a batch file View 3,959 Replies 17
Original Poster Posted 2007-09-04 13:39 ·  中国 福建 三明 电信
中级用户
★★
Credits 458
Posts 211
Joined 2006-07-26 19:42
20-year member
UID 59307
Status Offline
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 ]
Floor 2 Posted 2007-09-04 13:43 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Floor 3 Posted 2007-09-04 13:49 ·  中国 福建 三明 电信
中级用户
★★
Credits 458
Posts 211
Joined 2006-07-26 19:42
20-year member
UID 59307
Status Offline
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 ]
Floor 4 Posted 2007-09-04 14:05 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Floor 5 Posted 2007-09-04 14:10 ·  中国 福建 三明 电信
中级用户
★★
Credits 458
Posts 211
Joined 2006-07-26 19:42
20-year member
UID 59307
Status Offline
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 ]
Floor 6 Posted 2007-09-04 16:03 ·  中国 上海 华为云
中级用户
★★
Credits 458
Posts 211
Joined 2006-07-26 19:42
20-year member
UID 59307
Status Offline
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 ]
Floor 7 Posted 2007-09-04 16:12 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
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...

Floor 8 Posted 2007-09-04 16:21 ·  中国 上海 华为云
中级用户
★★
Credits 458
Posts 211
Joined 2006-07-26 19:42
20-year member
UID 59307
Status Offline
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 ]
Floor 9 Posted 2007-09-04 16:55 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
@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! -
Floor 10 Posted 2007-09-05 12:06 ·  中国 上海 华为云
中级用户
★★
Credits 458
Posts 211
Joined 2006-07-26 19:42
20-year member
UID 59307
Status Offline
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
Floor 11 Posted 2008-02-12 05:21 ·  中国 福建 福州 电信
中级用户
★★
Credits 458
Posts 211
Joined 2006-07-26 19:42
20-year member
UID 59307
Status Offline
!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.

@echo off
setlocal enabledelayedexpansion
set T=---
set a=3
for /f "delims=" %%i in ('dir /b *.txt') do (
for /f "tokens=1,2* delims=-" %%d in ('dir /b "%%i"') do (
set /a n=0
set oldn="%%i"
set newn0=%%~nd
set newn1="%%~nd!T!!a!.txt"
if not !oldn!==!newn1! (
if exist !newn1! (call :anum) else (
ren !oldn! !newn1!
)
)
)
)
goto :end
:anum
set /a n+=1
set newn2="!newn0!!n!!T!!a!.txt"
if exist !newn2! (goto :anum) else (
ren !oldn! !newn2!
)
goto :eof
:end
goto :eof


[ Last edited by wxcute on 2008-2-12 at 05:22 AM ]
Floor 12 Posted 2008-02-12 05:23 ·  中国 福建 福州 电信
中级用户
★★
Credits 458
Posts 211
Joined 2006-07-26 19:42
20-year member
UID 59307
Status Offline
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
Floor 13 Posted 2008-02-12 07:11 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
2007-9-5 12:06……

It's been a long time~
Floor 14 Posted 2008-02-12 08:13 ·  中国 福建 福州 电信
中级用户
★★
Credits 458
Posts 211
Joined 2006-07-26 19:42
20-year member
UID 59307
Status Offline
Yeah, I've been working hard to learn all along. I ran into this problem again today, so I started tackling it.
Floor 15 Posted 2008-02-12 12:15 ·  中国 吉林 吉林市 联通
中级用户
★★
Credits 307
Posts 130
Joined 2008-02-01 21:29
18-year member
UID 109981
Gender Male
Status Offline
OP, what are you doing this for? Could you explain?
Forum Jump: