How to rename and add incrementing number suffix on multiple files in Batch Script?

强颜欢笑 提交于 2019-12-20 03:40:51

问题


I have 500 files coming in and I need to first check if any file(s) exist then rename all of them regardless of what their filename is (the files are named in a different language).

No need to process them in any order.

Rename:

1.  “¦X¼d¬f-20110703-¦+¦dñHÑ-ª-¦=¬¦.xls” 
2.  “¦X¼d¬f-20110707-¦+¡¦-+¡8.xls”
3.  “¦X¼d¬f-20110707-¦+¡¦ñj¦«.xls”
4.  “¦X¼d¬f-20110708-¦+¡¦¬M¼n.xls”
5.  “¦X¼d¬f-20110713-¦d¼O¼n¦hÑP.xls”
.
.
.
500

To:

“TWN_CH_INV_VISIT_FORM_01.xls”
“TWN_CH_INV_VISIT_FORM_02.xls”
“TWN_CH_INV_VISIT_FORM_03.xls”
“TWN_CH_INV_VISIT_FORM_04.xls”
“TWN_CH_INV_VISIT_FORM_05.xls”
.
.
.
“TWN_CH_INV_VISIT_FORM_500.xls”

Hope you could help me on this one. I’ve been trying to do this for weeks.


回答1:


a simple FOR with a count (SET /A) should do what you need.

setlocal enabledelayedexpansion
SET /A COUNT=0
FOR %%A IN (*.xls) DO (
  SET /A COUNT+=1
  REN "%%A" "TWN_CH_INV_VIST_FORM_!COUNT!.xls"
)

See HELP FOR and HELP SET




回答2:


This is a deceptively difficult question to solve.

The 5 year old PA answer has a few problems.

1) The FOR loop begins iterating without buffering the entire directory tree, so it has the potential to rename a file that has already been renamed. I believe that is why the 7 file is missing within r0mmel's comment.

2) Delayed expansion occurs after for variables are expanded, so the file name will be corrupted and the rename will fail if the name contains a ! character.

3) A rename can fail if there already exists a TWN_CH_INV_VIST_FORM_n.xls file with the same number.

At first I thought I could solve the problem using the following:

@echo off
for /f "delims=: tokens=1*" %%A in (
  'dir /b *.xls ^| findstr /n "^"'
) do ren "%%B" "TWN_CH_INV_VIST_FORM_%%A.xls.new"
ren *.txt.new *.

I use DIR /B to list the files, and pipe that result to FINDSTR to prefix each file name with a line number, followed by a colon.

I then use FOR /F to iterate and parse the results into the number and the file name. FOR /F buffers the entire result before iterating, so I don't need to worry about renaming the same file twice.

I first give the renamed files a .xls.new "extension", just in case your directory already has files that meet the TWN_CH_INV_VIST_FORM_n.xls pattern. You don't want any name collisions. The final REN command then simply removes the .new extension to leave the desired .xls.

BUT, I just noticed that the original file names have lots of weird characters that could involve unicode that is not in the current code page. FOR /F does not play well with unicode.

There is one other minor issue in that the above does not pad the number to a fixed width. (this could have been solved easily enough)

So at this point it is time to break out my JREN.BAT regular expression renaming utility. It is pure script (hybrid batch / JScript) that runs natively on any Windows machine from XP onward. It has a built in facility to incorporate a fixed width incrementing number in the new name, and it works fine with unicode. I still temporarily give the new name the ".xls.new" extension to avoid any name collisions.

@echo off
call jren "^.*" "'TWN_CH_INV_VIST_FORM_'+$n+'.xls.new'" /j /npad 3 /fm *.xls
ren *.xls.new *.

I chose to pad the incrementing number to 3 digits instead of 2 because the OP said there could be 500 files.

Full documentation for JREN.BAT is available from the command line via jren /?, or jren /?? if you want paged output.



来源:https://stackoverflow.com/questions/6691455/how-to-rename-and-add-incrementing-number-suffix-on-multiple-files-in-batch-scri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!