中国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-10 14:57
47,811 topics / 349,897 posts / today 0 new / 48,255 members
DOS批处理 & 脚本技术(批处理室) » Batch rename files, keeping only the first 6 characters
Printable Version  4,755 / 31
Floor1 liuwang123 Posted 2007-05-04 13:44
新手上路 Posts 3 Credits 8
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"
)
Floor2 ccwan Posted 2007-05-04 14:18
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
```@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:



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```
Floor3 caiyiqun Posted 2007-05-04 14:23
初级用户 Posts 11 Credits 25
At first glance, I can tell you must be the one who downloads comics!! Hehe
Floor4 slore Posted 2007-05-04 14:37
铂金会员 Posts 2,478 Credits 5,212
Split by _, take the last part of %%i and then add .jpg
Floor5 liuwang123 Posted 2007-05-04 14:53
新手上路 Posts 3 Credits 8
Originally posted by ccwan at 2007-5-4 01:18 AM:

Try it out.
Floor6 liuwang123 Posted 2007-05-04 16:26
新手上路 Posts 3 Credits 8
Thanks to the help of the second floor, the execution efficiency is very high!
Floor7 lxmxn Posted 2007-05-04 16:56
版主 Posts 4,938 Credits 11,386
```
for /f "delims=_ tokens=1*" %%a in ('dir /b *.jpg') do ren %%a_%%b %%a.jpg
```
Floor8 yeke Posted 2007-05-04 20:54
初级用户 Posts 13 Credits 25
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 ]
Floor9 lpk130 Posted 2007-05-05 19:38
初级用户 Posts 30 Credits 80
```
@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
Floor10 foreverlrf Posted 2007-08-20 12:31
初级用户 Posts 11 Credits 24
The method provided by the moderator is good, and the code is short and has high execution efficiency!
Floor11 Billunique Posted 2007-09-18 16:50
中级用户 Posts 112 Credits 259
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 ]
Floor12 wudixin96 Posted 2007-09-18 17:42
银牌会员 Posts 931 Credits 1,928
adb.mp3
adc.mp3
adb.mp3

Are the file names not duplicate?
Floor13 jmz573515 Posted 2007-09-18 17:47
银牌会员 Posts 464 Credits 1,212
```
@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
```
Floor14 wudixin96 Posted 2007-09-18 17:51
银牌会员 Posts 931 Credits 1,928
The last part upstairs, isn't it not necessarily 3 digits?
Floor15 Billunique Posted 2007-09-18 18:04
中级用户 Posts 112 Credits 259
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 _.
1 2 3  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023