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 11:22
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Batch rename files, keeping only the first 6 characters View 4,456 Replies 31
Original Poster Posted 2007-05-04 13:44 ·  中国 湖南 湘潭 电信
新手上路
Credits 8
Posts 3
Joined 2007-05-04 13:19
19-year member
UID 87437
Gender Male
Status Offline
I have a folder with thousands of images named as follows:

006025_9485.jpg
006078_9fv5d.jpg
200032_9cv5f.jpg
006076_9143.jpg
000043_2350.jpg
... ...

Here, before the _ is a 6-digit number, and there is no inevitable fixed pattern; the number of digits after the _ is not fixed, and there is no pattern.

I want to achieve the following renaming effect:

006025.jpg
006078.jpg
200032.jpg
006076.jpg
000043.jpg
... ...

Simply put, it is to keep only the first six digits of all file names. I wonder what the batch processing command is?

(Note: I have too many basic knowledge, come here, hope you all give detailed pointers, thank you!)
I have any issues with the above situation. Here is the batch processing command to achieve the above renaming effect you want. You can use the following batch command to achieve the renaming effect you expect. The specific batch command is as follows. For the situation you want, the specific batch command is the following. To achieve the renaming effect you want, the specific batch command is the above. Here is the batch command to achieve the renaming effect you expect. For the situation you want, the specific batch command is as follows. Please use the following batch command to achieve the renaming effect you expect. The specific batch command is as follows. To achieve the renaming effect you want, the specific batch command is the above. Here is the batch command to achieve the renaming effect you expect. For the situation you want, the specific batch command is the following. Please use the following batch command to achieve the renaming effect you expect. The specific batch command is as follows. To achieve the renaming effect you want, the specific batch command is the above. Here is the batch command to achieve the renaming effect you expect. For the situation you want, the specific batch command is as follows. Please use the following batch command to achieve the renaming effect you expect. The specific batch command is as follows.

@echo off
for /f "delims=" %%a in ('dir /b *.jpg') do (
set "fname=%%a"
for %%b in ("!fname:~0,6!.jpg"
echo !fname:~0,6!.jpg
ren "%%a" "!fname:~0,6!.jpg"
ren "%%a" "!fname:~0,6!.jpg"
)
Floor 2 Posted 2007-05-04 14:18 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
```@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%i in ('dir/b *.jpg') do (
set var=%%i
set var=!var:~0,6!
ren %%i !var!.jpg
)
pause

Wait, there's a mistake in the code. The `dir/b` command is correct, but in the `for /f` loop, it should be `dir/b *.jpg` instead of `dir/b *.jpg`? No, wait, the original code has `dir/b *.jpg`, but in the second code block, it's written as `dir/b *.jpg`. That's an error. But according to the translation requirements, we just translate the Chinese parts. Since the user's input is code with some Chinese? Wait, no, the user's input is a code snippet. Let's check again.

The original code is:

@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%i in ('dir/b *.jpg') do (
set var=%%i
set var=!var:~0,6!
ren %%i !var!.jpg
)
pause


There's no Chinese in the code. So the translated output is the same code. Wait, no, wait a second. The user provided a code snippet, and we need to translate it if there's Chinese, but here there's no Chinese. So we just return the code as is.

Wait, I made a mistake earlier. The code doesn't have Chinese. So the translated text is the same as the input code.

So the output should be:

```@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%i in ('dir/b *.jpg') do (
set var=%%i
set var=!var:~0,6!
ren %%i !var!.jpg
)
pause```
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 3 Posted 2007-05-04 14:23 ·  中国 福建 泉州 石狮市 电信
初级用户
Credits 25
Posts 11
Joined 2007-04-03 16:24
19-year member
UID 83843
Gender Male
Status Offline
At first glance, I can tell you must be the one who downloads comics!! Hehe
Floor 4 Posted 2007-05-04 14:37 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Split by _, take the last part of %%i and then add .jpg
Floor 5 Posted 2007-05-04 14:53 ·  中国 湖南 湘潭 电信
新手上路
Credits 8
Posts 3
Joined 2007-05-04 13:19
19-year member
UID 87437
Gender Male
Status Offline
Originally posted by ccwan at 2007-5-4 01:18 AM:
@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%i in ('dir/b *.jpg') do (
set var=%%i
set var=!var:~0,6!
ren %%i !var!.jpg
)
pause

Try it out.
Floor 6 Posted 2007-05-04 16:26 ·  中国 湖南 湘潭 电信
新手上路
Credits 8
Posts 3
Joined 2007-05-04 13:19
19-year member
UID 87437
Gender Male
Status Offline
Thanks to the help of the second floor, the execution efficiency is very high!
Floor 7 Posted 2007-05-04 16:56 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
```
for /f "delims=_ tokens=1*" %%a in ('dir /b *.jpg') do ren %%a_%%b %%a.jpg
```
Floor 8 Posted 2007-05-04 20:54 ·  中国 湖南 邵阳 电信
初级用户
Credits 25
Posts 13
Joined 2007-04-10 04:48
19-year member
UID 84568
Gender Male
Status Offline
It's really fun.

─────────────────── Moderation Record ────────────────────
Executor: bjsh
Description: Spamming
Punishment: Since it's a new member of the forum, no punishment is given for the time being; if found again, it will be severely punished
─────────────────── Moderation Record ────────────────────


[ Last edited by bjsh on 2007-5-4 at 12:04 PM ]
Floor 9 Posted 2007-05-05 19:38 ·  中国 四川 成都 联通
初级用户
Credits 80
Posts 30
Joined 2007-04-19 23:21
19-year member
UID 85763
Gender Male
Status Offline
```
@echo off
setlocal enabledelayedexpansion
for %%i in (*) do (
set name=%%~ni
set name=!name:~0,6!
ren %%i !name!%%~xi
)
```
Files with a filename shorter than 6 characters will not be renamed
Case-insensitive for extensions
Floor 10 Posted 2007-08-20 12:31 ·  中国 广西 柳州 电信
初级用户
Credits 24
Posts 11
Joined 2007-08-15 15:49
18-year member
UID 95192
Gender Male
Status Offline
The method provided by the moderator is good, and the code is short and has high execution efficiency!
Floor 11 Posted 2007-09-18 16:50 ·  中国 北京 雅虎中国
中级用户
★★
菜鸟总动员
Credits 259
Posts 112
Joined 2006-08-28 15:53
19-year member
UID 61454
Status Offline
Just along to ask:
If my prefix has no rules, and I just want the content after the last _ to rename, how to do? Suppose my files are like this:

Copy Copy Dreaming of You_fdsl_fds_111_adb.mp3
Copy Copy Dreaming of You_fdsl_fds_adc.mp3
Dreaming of You_fdsl_fds_ade.mp3
.....

And my purpose is to get:
adb.mp3
adc.mp3
ade.mp3
.....

[ Last edited by Billunique on 2007-9-18 at 06:00 PM ]
★①②③④⑤⑥⑦⑧⑨⑩㈠㈡㈢㈣㈤㈥㈦㈧㈨㈩ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ【●】→←↑↓▲
Floor 12 Posted 2007-09-18 17:42 ·  中国 香港 腾讯云
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
adb.mp3
adc.mp3
adb.mp3

Are the file names not duplicate?
Floor 13 Posted 2007-09-18 17:47 ·  中国 辽宁 本溪 联通
银牌会员
★★★
Credits 1,212
Posts 464
Joined 2006-12-13 21:11
19-year member
UID 73417
Gender Male
Status Offline
```
@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%i in ('dir /b *.mp3') do (
set name=%%~ni
set name=!name:~-3,3!
ren %%i !name!.mp3
)
pause
```
Floor 14 Posted 2007-09-18 17:51 ·  中国 香港 腾讯云
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
The last part upstairs, isn't it not necessarily 3 digits?
Floor 15 Posted 2007-09-18 18:04 ·  中国 北京 雅虎中国
中级用户
★★
菜鸟总动员
Credits 259
Posts 112
Joined 2006-08-28 15:53
19-year member
UID 61454
Status Offline
Well, thanks to the reminder from wudixin96 and the answer from jmz573515 : )
Sorry I didn't express it clearly.
1. The name after _ is uncertain, and it is 3 digits.
2. There will be no duplicate situations for the name after _.
★①②③④⑤⑥⑦⑧⑨⑩㈠㈡㈢㈣㈤㈥㈦㈧㈨㈩ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ【●】→←↑↓▲
Forum Jump: