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-06-25 09:36
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Batch rename files, keeping only the first 6 characters View 4,450 Replies 31
Floor 16 Posted 2007-09-18 18:14 ·  中国 北京 雅虎中国
中级用户
★★
菜鸟总动员
Credits 259
Posts 112
Joined 2006-08-28 15:53
19-year member
UID 61454
Status Offline
Is there a way to take all the content before the last _ as one token and the content after as another token? Please teach...
★①②③④⑤⑥⑦⑧⑨⑩㈠㈡㈢㈣㈤㈥㈦㈧㈨㈩ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ【●】→←↑↓▲
Floor 17 Posted 2007-09-18 18:59 ·  中国 香港 腾讯云
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
```
@echo off
for /r %%i in (*.mp3) do (
set nm=%%~ni
call :js
call ren "%%i" "%%nm%%.mp3"
)
pause

:js
set nm=%nm:*_=%
echo %nm%|find "_">nul 2>nul&&goto js||goto :eof
```
Floor 18 Posted 2007-09-18 19:22 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Try the following one. If the string after the last _ character in the file name contains spaces, replace the spaces with "#":
@echo off & setlocal EnableDelayedExpansion
for %%a in (*.mp3) do (
set file_name=%%a
set file_rname=!file_name: =#!
set file_rname=!file_rname:_=;!
for %%b in (!file_rname!) do set new_file_name=%%b
rename "!file_name!" "!new_file_name!"
)
Floor 19 Posted 2007-09-18 19:27 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Improve it or you can not replace spaces:
@echo off & setlocal EnableDelayedExpansion
for %%a in (*.mp3) do (
set file_name=%%a
set file_rname=!file_name: =#!
set file_rname=!file_rname:_=;!
for %%b in (!file_rname!) do set new_file_name=%%b
set new_file_name=!new_file_name:#= !
rename "!file_name!" "!new_file_name!"
)
Floor 20 Posted 2007-09-19 10:09 ·  中国 北京 雅虎中国
中级用户
★★
菜鸟总动员
Credits 259
Posts 112
Joined 2006-08-28 15:53
19-year member
UID 61454
Status Offline
Hehe, thank you two, after testing, both achieved the effect!

To wudixin96: I am shallow, brother's move is too advanced, I don't quite understand the echo %nm%|find "_">nul 2>nul&&goto js.

To lxmxn: You can come up with such an idea, you are a talent, PF!
★①②③④⑤⑥⑦⑧⑨⑩㈠㈡㈢㈣㈤㈥㈦㈧㈨㈩ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ【●】→←↑↓▲
Floor 21 Posted 2007-09-19 10:38 ·  中国 香港 腾讯云
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
Brother's efficiency is very high, much stronger than mine. However, if the characters after the last _ contain #, they will be replaced with spaces.
Floor 22 Posted 2007-09-19 11:25 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Originally posted by wudixin96 at 2007-9-19 10:38:
lxmxn
Your efficiency is very high, much stronger than mine. However, if the characters after the last _ contain #,
it will be replaced with a space.

Improve it further:

@echo off & setlocal EnableDelayedExpansion
for %%a in (*.mp3) do (
set file_name=%%a
set file_rname=!file_name: =\!
set file_rname=!file_rname:_=;!
for %%b in (!file_rname!) do set new_file_name=%%b
set new_file_name=!new_file_name:\= !
echo rename "!file_name!" "!new_file_name!"
)
Floor 23 Posted 2007-09-19 11:48 ·  中国 香港 腾讯云
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
Then if there are also ; characters in the string, isn't the semicolon used as a separator again?
Floor 24 Posted 2007-09-19 12:02 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Well, there are always some bugs in the program. We can only eliminate as many special cases as possible and modify the code according to the situation.
Floor 25 Posted 2007-09-19 12:05 ·  中国 香港 腾讯云
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
o(∩_∩)o...Haha, I didn't see you on QQ.
Floor 26 Posted 2007-09-19 12:10 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Or rely on external tools.

@echo off
for %%a in (*.mp3) do (
for /f "delims=" %%b in ('echo\%%a^|sed "s/.*_\(.*\.mp3\)/\1/"') do echo rename "%%a" "%%b"
)
pause


ps: QQ has been invisible for a long time.

[ Last edited by lxmxn on 2007-9-19 at 12:11 PM ]
Floor 27 Posted 2007-09-20 10:10 ·  中国 辽宁 本溪 联通
银牌会员
★★★
Credits 1,212
Posts 464
Joined 2006-12-13 21:11
19-year member
UID 73417
Gender Male
Status Offline
```
set fso=createobject("scripting.filesystemobject")
set folder=fso.getfolder(".")
for each file in folder.files
if UCASE(RIGHT(file,3))="MP3" and INSTR(file,"_")<>0 then
set wj=fso.getfile(file)
wj.name=cl(file) & ".mp3"
end if
next
msgbox "Operation successful!",4096,"Prompt"
function cl(filename)
filename=fso.getbasename(filename)
n=INSTRREV(filename,"_")
cl=MID(filename,n+1)
end function
```
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
lxmxn +4 2007-09-20 12:33
Floor 28 Posted 2007-12-29 23:59 ·  中国 重庆 电信
银牌会员
★★★
Credits 2,165
Posts 730
Joined 2004-04-21 00:00
22-year member
UID 22966
Gender Male
Status Offline
It's hard to say if there are duplicate names.
How about being more straightforward:
ren ??????*.jpg ??????.jpg
It's still okay
Floor 29 Posted 2007-12-30 00:05 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
ren *.txt ??????.txt

The text "ren *.txt ??????.txt" is in English already in terms of the command itself. If we consider the context of DOS commands, it's a renaming command. But as per the requirement, if the text is already in English, return it unchanged. So the translated result is:

ren *.txt ??????.txt
Floor 30 Posted 2007-12-30 00:25 ·  中国 重庆 渝中区 电信
银牌会员
★★★
Credits 2,165
Posts 730
Joined 2004-04-21 00:00
22-year member
UID 22966
Gender Male
Status Offline
Brother's words are exactly right!
I focused all my attention on six-digit numbers ^-^
Forum Jump: