BAT command to insert a char. in a file name

January 2017


I have a problem, one whose solution I stumbled onto a couple years ago but - alas - my storage recently crashed and I lost the batch file!

We start w/ this awkward file name:
The following BAT utility cleverly strips everything in front of the 'A' (first 23 chars.):

@echo off 
for %%i in (*.dat) do (set fName=%%i) & call :rename 
goto :eof 
:: Crops the 1st 23 characters of fName ('rank(all)_so2_1hr_conc_'): 
ren %fName% %fName:~23% 
goto :eof 

Great! This renders:


Now I need to convert this to:


Notice '...h10' needs to be '...h100'. Note that, while in this example, the part preceding the '.' is 'A_2p5h10', it could be any 8-character string (so we'll need a wildcard or something).

Can you please suggest a routine that will add the ' 0 ' in the middle of the file name (preceding the '.')? There are hundreds of file names in the directory that need to have this conversion.

As I recall, the critical command (I believe used REN) that used to do this was extremely simple (contained one of more %, as I recall).


You can use this script:
@echo off 
for %%i in (*.dat) do (set fName=%%i) & call :rename 
goto :eof 
:: Rename the file by discarding first 23 chars, and last 7 and adding H100.plt at the end 
ren %fName% %fName:~23,-7%H100.plt 
goto :eof 


Thanks to Tommy for this tip on the forum.


Published by aakai1056. Latest update on February 22, 2010 at 08:37 AM by aakai1056.
This document, titled "BAT command to insert a char. in a file name," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (