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-11 23:54
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to batch rename files View 1,091 Replies 13
Original Poster Posted 2010-08-03 22:07 ·  中国 广东 深圳 联通
新手上路
Credits 5
Posts 7
Joined 2010-07-31 23:39
16-year member
UID 171565
Gender Male
Status Offline
I took photos with a digital camera, and all the photo names are SAM_00**.JPG. How can I make a batch file to remove the SAM_ from all the photo names and turn them into 00**.JPG
Floor 2 Posted 2010-08-03 22:34 ·  中国 广东 深圳 联通
新手上路
Credits 5
Posts 7
Joined 2010-07-31 23:39
16-year member
UID 171565
Gender Male
Status Offline
Has everyone gone to sleep?
Floor 3 Posted 2010-08-03 23:42 ·  中国 广东 汕头 电信
初级用户
★★
Credits 131
Posts 72
Joined 2008-02-24 14:31
18-year member
UID 111317
Gender Male
Status Offline
@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%i in ('dir /b /a-d *.jpg') do (
ren %%i !%%i:SAM_=!
)
pause


Give it a try, not guaranteed to work...
Floor 4 Posted 2010-08-04 00:11 ·  中国 广东 深圳 联通
新手上路
Credits 5
Posts 7
Joined 2010-07-31 23:39
16-year member
UID 171565
Gender Male
Status Offline
It doesn't work. After running it, it created a file called SAM_, and the batch file window shows "A duplicate file exists, or the file cannot be found"
Floor 5 Posted 2010-08-04 00:35 ·  中国 重庆 电信
初级用户
★★
Credits 198
Posts 89
Joined 2007-09-06 23:22
18-year member
UID 96746
Gender Male
From 重庆
Status Offline
Floor 6 Posted 2010-08-04 07:45 ·  中国 广东 深圳 联通
新手上路
Credits 5
Posts 7
Joined 2010-07-31 23:39
16-year member
UID 171565
Gender Male
Status Offline
OK.
set "str=%%i"
ren "%%i" "!str:SAM_=!"
)
Can you explain this piece of code in detail? I've been learning batch files for a while, but I mainly can't figure out FOR and SET
Floor 7 Posted 2010-08-04 08:31 ·  中国 湖北 黄石 电信
中级用户
★★
Credits 330
Posts 244
Joined 2006-04-14 14:07
20-year member
UID 53823
Gender Male
From 湖北
Status Offline
Originally posted by chinesechrist at 2010-8-3 22:07:
I took photos with a digital camera, and all the photo names are SAM_00**.JPG. How can I make a batch file to remove the SAM_ from all the photo names and turn them into 00**.JPG


This is very simple

ren sam_*.jpg " *.jpg"


Tested and works on Windows XP sp3
Floor 8 Posted 2010-08-04 08:40 ·  中国 湖北 黄石 电信
中级用户
★★
Credits 330
Posts 244
Joined 2006-04-14 14:07
20-year member
UID 53823
Gender Male
From 湖北
Status Offline
Found that the poster above still didn't pass either , it actually created files whose names begin with half-width spaces.
Floor 9 Posted 2010-08-04 09:19 ·  中国 重庆 电信
初级用户
★★
Credits 198
Posts 89
Joined 2007-09-06 23:22
18-year member
UID 96746
Gender Male
From 重庆
Status Offline
Ignore this post!

[ Last edited by WANKOILZ on 2010-8-4 at 09:32 ]
Floor 10 Posted 2010-08-04 17:59 ·  中国 广东 深圳 联通
新手上路
Credits 5
Posts 7
Joined 2010-07-31 23:39
16-year member
UID 171565
Gender Male
Status Offline
ren sam_*.jpg " *.jpg"
Tested and works on Windows XP sp3
---------but the filename has four extra spaces
Floor 11 Posted 2010-08-04 20:40 ·  中国 湖北 黄石 电信
中级用户
★★
Credits 330
Posts 244
Joined 2006-04-14 14:07
20-year member
UID 53823
Gender Male
From 湖北
Status Offline
Originally posted by chinesechrist at 2010-8-4 17:59:
ren sam_*.jpg " *.jpg"
Tested and works on Windows XP sp3
---------but the filename has four extra spaces


Mm, yes, reply #8 already pointed that out. Inspired by this thread, I wrote a small tool for renaming files. I've temporarily named it xren.exe, using Jscript regex.

   Usage: 
      xren file type filename-matching regex  replacement filename

For example: 
1. Replace all half-width spaces in the names of all jpg files in the current directory with _


xren jpg "/ /gi" "_"




2. Add www.cn-dos.net to the beginning of the filenames of all TXT files in the current directory


xren txt "/^/" www.cn-dos.net_




3. Remove the website address from all filenames of the form Story_www.example.com.txt


xren txt "/_www\.example\.com\.txt$/" ".txt"


...

That's enough examples for now. By the way, xren jpg "/^SAM_/" can solve the OP's problem ;)

[ Last edited by freeants001 on 2010-8-4 at 21:56 ]
Floor 12 Posted 2010-08-05 06:55 ·  中国 广东 深圳 联通
新手上路
Credits 5
Posts 7
Joined 2010-07-31 23:39
16-year member
UID 171565
Gender Male
Status Offline
Thanks for your XREN, it's very good software
Floor 13 Posted 2010-08-05 07:02 ·  中国 广东 深圳 联通
新手上路
Credits 5
Posts 7
Joined 2010-07-31 23:39
16-year member
UID 171565
Gender Male
Status Offline
I still don't understand the specific usage rules, and xren /? doesn't show any help information,
Floor 14 Posted 2010-08-05 08:37 ·  中国 湖北 黄石 电信
中级用户
★★
Credits 330
Posts 244
Joined 2006-04-14 14:07
20-year member
UID 53823
Gender Male
From 湖北
Status Offline
Forum Jump: