简体   繁体   中英

Want to rename a file through windows batch file scripting

Here is my file MY_APP_CON_123_yyyymmdd.hhmmss.txt where "yyyymmdd.hhmmss" is variable data. I want to rename it to MY_APP_CON_0123_yyyymmdd.hhmmss.txt (Zero infront of 123). When I use ren command as shown below, the first y disappears from the file name.

REN MY_APP_CON_123_* MY_APP_CON_0123_*

Any solutions ?

Here is a pure batch solution.

@echo off
for %%F in (MY_APP_CON_123_*) do @set "name=%%F"&call ren "%%name%%" "%%name:*MY_APP_CON_=MY_APP_CON_0%%"

Or, with delayed expansion:

@echo off
setlocal enableDelayedExpansion
for %%F in (MY_APP_CON_123_*) do @set "name=%%F"&ren "!name!" "!name:*MY_APP_CON_=MY_APP_CON_0!"

But I would use my JREN.BAT regular expression renaming utility - a hybrid JScript/batch script that runs natively on any Windows machine from XP onward.

call jren "^MY_APP_CON_123_" "MY_APP_CON_0123_" /i /fm *.txt

Not answer, but explaining why your rename isn't working:

cmd.exe/command.com's wildcard renaming is position based, eg

filename: abc123.txt
command: ren abc*.txt abd1*.txt

will do:

abc123.txt    - original name
||||**.txt
abd1**.txt    - rename pattern
----------
abcd23.txt    - new filename

https://superuser.com/questions/475874/how-does-the-windows-rename-command-interpret-wildcards

Wildcard renaming where you want to change parts of the filename and the new part has a different length from the original part gets very hairy.

for %%a in (MY_APP_CON_123_**) do set "name=%%a" & ren %name% %name:123=0123%

这对我有用

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM