简体   繁体   中英

windows batch regular expression

I have a bunch of files under the format : TOTO-A#7.wav and I would like to rename them TOTO_7_As.wav (s for sharp) but I am not very good at regular expressions. Do you know if its possible to do that easily? Cheers

From the prompt:

for /f "tokens=1*delims=#" %a in ('dir /b /a-d *#*') do ren "%a#%b" "%aS%b"

As a batch line:

for /f "tokens=1*delims=#" %%a in ('dir /b /a-d *#*') do ECHO(ren "%%a#%%b" "%%aS%%b"

Note that the ECHO( in the second example will simply show the required instructions. This is to allow the opportunity to ensure the correct instructions are generated. To execute the instructions, remove the ECHO( .

(Same trick can be used on the previous version, but it's a little tricky to read the result)


for /f "tokens=1,2,3*delims=#-." %%a in ('dir /b /a-d *-*#*.*') do ECHO(ren "%%a-%%b#%%c.%%d" "%%a_%%c_%%bs.%%d"

will rename TOTO-A#7.wav to TOTO_7_As.wav

No idea what you mean by "sharp" - or "before between" fo that matter.

@echo off
for /F "tokens=1-3 delims=-#." %%a in ('dir /A-D /B *.wav') do ren "%%a-%%b#%%c.wav" "%%a_%%c_%%bs.wav

If you want to execute previous Batch file in the command-line, just change each %% by % .

If you have other files with .wav extension and different format, change *.wav wild-card by a more precise one, for example: ????-?#?.wav

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