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-04 05:44
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » (Help) How to write a .bat file to implement file renaming batch processing View 2,857 Replies 18
Original Poster Posted 2007-03-31 18:11 ·  德国 德国电信(DTAG)
初级用户
Credits 36
Posts 14
Joined 2007-03-31 17:31
19-year member
UID 83522
Gender Male
Status Offline
I encountered a problem. For example, I have hundreds of.pdf files, such as:
central aaa 01.pdf
akurss bbs 99.pdf
sskpk p12 980 ok.pdf
airchina lufthansa uk mu500.pdf
92 years anniver.pdf
.........
What I want to do is to compile a batch file xxx.cmd. Running it once can automatically change the "spaces" in all the above.pdf files to "-". After running the xxx.cmd file, the above PDF files will be renamed as follows and saved:
central-aaa-01.pdf
akurss-bbs-99.pdf
sskpk-p12-980-ok.pdf
airchina-lufthansa-uk-mu500.pdf
92-years-anniver.pdf
.........
How to write it? Can you give an example? Thank you.

[ Last edited by tommy725 on 2007-4-2 at 06:55 AM ]
Floor 2 Posted 2007-03-31 18:18 ·  中国 陕西 西安 电信
铂金会员
★★★★
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-03-31 22:44 ·  中国 浙江 杭州 华数宽带
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
This is the original content in the quote section:



  1. @echo off
  2. for /f "delims=" %%a in ('dir /b /a *.pdf') do set "old_name=%%a" &call :re_name
  3. goto exit
  4. :re_name
  5. set "new_name=%old_name: =-%"
  6. ren "%old_name%" "%new_name%"
  7. :exit
BJSH发表于: 2007-03-31 09:37


[ Last edited by bjsh on 2007-3-31 at 09:47 AM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
huzixuan +4 2007-04-01 09:02
Floor 4 Posted 2007-03-31 23:12 ·  中国 四川 遂宁 电信
中级用户
★★
Credits 278
Posts 103
Joined 2006-10-21 21:08
19-year member
UID 67562
Gender Male
Status Offline
Floor 5 Posted 2007-04-01 00:35 ·  中国 浙江 杭州 华数宽带
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
Why is there no problem when I try it myself?? Please tell me the error message.
Floor 6 Posted 2007-04-01 05:59 ·  德国
初级用户
Credits 36
Posts 14
Joined 2007-03-31 17:31
19-year member
UID 83522
Gender Male
Status Offline
Thanks a lot, bjsh. I'll give it a try. But I still don't understand. If you can explain the meaning or function of each sentence, I would be even more grateful.
Floor 7 Posted 2007-04-01 07:49 ·  中国 广东 广州 荔湾区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
对于这一句话?想问个问题?

('dir /b /a *.pdf')里面的参数是不是只会执行一次?然后把把所有变量放内存里?等程序一个一个的读取?
当里面读取的内容非常多的时候。会不会造成机器假死?

For the sentence? Want to ask a question?

Are the parameters inside ('dir /b /a *.pdf') only executed once? Then put all variables into memory? Wait for the program to read one by one?
When there are a lot of content read inside. Will it cause the machine to freeze?
知,不觉多。不知,乃求知
Floor 8 Posted 2007-04-01 08:50 ·  中国 安徽 芜湖 电信
高级用户
★★
Credits 537
Posts 219
Joined 2006-10-31 21:08
19-year member
UID 69036
Gender Male
From 芜湖
Status Offline
Just now I made a mistake.

@echo off
setlocal enabledelayedexpansion
for %%h in (*.pdf) do (
set str=%%h
set str=!str: =-!
ren "%%h" "!str!")

[ Last edited by huzixuan on 2007-4-1 at 08:54 AM ]
江湖远
碧空长
路茫茫

一个人漫无目的的奔跑,风,刺骨的冷....
Floor 9 Posted 2007-04-01 17:43 ·  德国
初级用户
Credits 36
Posts 14
Joined 2007-03-31 17:31
19-year member
UID 83522
Gender Male
Status Offline
Thank you everyone, can I test like this? Edit file.cmd, then write the code in and save it, then run this file in the current folder. There are three test.pdf documents in the current folder. But why does this result appear:
a duplicate file name exists,or the file can not be found
a duplicate file name exists,or the file can not be found
a duplicate file name exists,or the file can not be found
Floor 10 Posted 2007-04-01 22:49 ·  中国 浙江 杭州 华数宽带
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
Does directly running the one I wrote also have this kind of error???
Floor 11 Posted 2007-04-01 23:05 ·  中国 广东 汕头 电信
初级用户
Credits 37
Posts 18
Joined 2007-04-01 08:57
19-year member
UID 83600
Gender Male
Status Offline
It's good, but I can't understand it. It's really frustrating~~~~
Floor 12 Posted 2007-04-02 19:54 ·  德国
初级用户
Credits 36
Posts 14
Joined 2007-03-31 17:31
19-year member
UID 83522
Gender Male
Status Offline
I ran
@echo off
for /f "delims= " %%a in ('dir /b/a *.pdf') do
set "old_name=%%a" &call :re_name
goto exit
:re_name
set "new_name=%old_name:=-%"
ren "%old_name%" "%new_name%"
:exit
After running, it said the syntax of the command is incorrect.
Floor 13 Posted 2007-04-02 21:56 ·  中国 四川 绵阳 江油市 电信
高级用户
★★★
Credits 502
Posts 327
Joined 2006-12-30 06:01
19-year member
UID 74981
Gender Male
Status Offline
What I wrote just now, which can be renamed after testing

@echo off&setlocal enabledelayedexpansion
for /f "delims=." %%a in ('dir /a-d /b *.pdf') do (
set str=%%a
set str=!str: =-!
ren "%%a".pdf !str!.pdf
)
pause


But your sskpk p12 980 ok.pdf has two spaces, so the renamed one will be sskpk--p12-980-ok.pdf. Is it okay to use it like this
Floor 14 Posted 2007-04-02 22:35 ·  德国
初级用户
Credits 36
Posts 14
Joined 2007-03-31 17:31
19-year member
UID 83522
Gender Male
Status Offline
Two spaces are my input error. If spaces can be changed to "-", it's okay.
Floor 15 Posted 2007-04-02 22:42 ·  中国 北京 鹏博士BGP
中级用户
★★
Credits 404
Posts 179
Joined 2006-03-30 14:44
20-year member
UID 53056
Status Offline
ren "* *.pdf" "*-*.pdf"
Forum Jump: