中国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-04 04:39
48,038 topics / 350,123 posts / today 0 new / 48,251 members
DOS批处理 & 脚本技术(批处理室) » (Help) How to write a .bat file to implement file renaming batch processing
Printable Version  2,854 / 18
Floor1 tommy725 Posted 2007-03-31 18:11
初级用户 Posts 14 Credits 36
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 ]
Floor2 slore Posted 2007-03-31 18:18
铂金会员 Posts 2,478 Credits 5,212
Floor3 bjsh Posted 2007-03-31 22:44
银牌会员 Posts 621 Credits 2,000
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 ]
Floor4 zzhh612 Posted 2007-03-31 23:12
中级用户 Posts 103 Credits 278
Floor5 bjsh Posted 2007-04-01 00:35
银牌会员 Posts 621 Credits 2,000
Why is there no problem when I try it myself?? Please tell me the error message.
Floor6 tommy725 Posted 2007-04-01 05:59
初级用户 Posts 14 Credits 36
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.
Floor7 flyinspace Posted 2007-04-01 07:49
银牌会员 Posts 517 Credits 1,206
对于这一句话?想问个问题?

('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?
Floor8 huzixuan Posted 2007-04-01 08:50
高级用户 Posts 219 Credits 537 From 芜湖
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 ]
Floor9 tommy725 Posted 2007-04-01 17:43
初级用户 Posts 14 Credits 36
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
Floor10 bjsh Posted 2007-04-01 22:49
银牌会员 Posts 621 Credits 2,000
Does directly running the one I wrote also have this kind of error???
Floor11 wuwuwuming Posted 2007-04-01 23:05
初级用户 Posts 18 Credits 37
It's good, but I can't understand it. It's really frustrating~~~~
Floor12 tommy725 Posted 2007-04-02 19:54
初级用户 Posts 14 Credits 36
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.
Floor13 qingfushuan Posted 2007-04-02 21:56
高级用户 Posts 327 Credits 502
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
Floor14 tommy725 Posted 2007-04-02 22:35
初级用户 Posts 14 Credits 36
Two spaces are my input error. If spaces can be changed to "-", it's okay.
Floor15 kcdsw Posted 2007-04-02 22:42
中级用户 Posts 179 Credits 404
ren "* *.pdf" "*-*.pdf"
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023