BAT command to insert a char. in a file name

Solved/Closed
Tommy - Feb 19, 2010 at 04:06 PM
 Tommy - Feb 21, 2010 at 08:49 AM
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:
rank(all)_so2_1hr_conc_A_2p5h10.dat
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
:rename
:: Crops the 1st 23 characters of fName ('rank(all)_so2_1hr_conc_'):
ren %fName% %fName:~23%
goto :eof

Great! This renders:

A_2p5h10.dat

Now I need to convert this to:

A_2p5h100.dat

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).

3 responses

rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 766
Feb 20, 2010 at 05:49 PM
Try this


@echo off
for %%i in (*.dat) do (set fName=%%i) & call :rename
goto :eof
:rename
:: rename the file by discarding first 23 letter, and inserting 0 at the 4 position from end
ren %fName% %fName:~23,-4%0%fName:~-4%

goto :eof

Also this should do if .dat is always the case
@echo off
for %%i in (*.dat) do (set fName=%%i) & call :rename
goto :eof
:rename
:: rename the file by discarding first 23 letter, and last 4 and adding 0.dat at the end
ren %fName% %fName:~23,-4%0.dat

goto :eof
1
Thanks again, Risvisa1. I just took a bit of time to play around w/ the 2 scripts you originally provided, and I liked the 2nd one best. I transposed it a bit and now can say that the following script is EXACTLY equivalent to what I offered above (which uses the 2 commands using the '?' wildcards):

@echo off
for %%i in (*.dat) do (set fName=%%i) & call :rename
goto :eof
:rename
:: 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


This script - a modification of my original "cropping" script, is perfect. I thank you again for your clever contribution. Kudos!
1
Thank you, Rizvisa1. What you offered, while a bit "complicated", may work and is something I may play with when I have a little time. Since my post on 2/20/10, I rediscovered the method I had used previously to effect the changes I desired in the file names. It makes use of the '?' as a wildcard (not '%' as I had thought).

I begin with file names that are of these types (happen to be 8.3):

A_1.5h10.plt
A___1h10.plt

As I said, we want to render them *.H100.plt

The solution is SIMPLE; you need two commands that will work in tandem:

:: Change 'h10' to 'H100'
ren ???.?h10.plt ???.?H100.plt
ren ?????h10.plt ?????H100.plt

The first line will convert the file names w/ decimals in the "front part", e.g., the first name above.
The second line is necessary to convert the second type above, which has no decimal in the front part of the file name. Again, both are necessary to get the job done: they are complementary.

The result will look like this:

A_1.5H100.plt
A___1H100.plt

I hope this will benefit others who come to this forum w/ DOS script questions.
0
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 766
Feb 21, 2010 at 08:37 AM
Taking page from you in explaining what code does. I would explain, may be would help some one

ren %fName% %fName:~23,-4%0.dat

ren : is dos command for rename

%fname% is the variable passed and is the initial file name (rank(all)_so2_1hr_conc_A_2p5h10.dat)

%fName:~23,-4 says that starting from character 23 get all characters till end, but leave out last 4 (gives A_2p5h10)

"0.dat" is just a string that gets appended to the string evaluated by %fName:~23,-4. it adds that extra "0" and the file extension ".dat" (makes it A_2p5h100.dat)


ren %fName% %fName:~23,-4%0%fName:~-4%

%fName:~23,-4% , get all but last 4 characters starting from character 23 (gives A_2p5h10)

"0" , was the 0 that you wanted to add (makes A_2p5h100)

%fName:~-4% , is getting the last 4 character which would be ".dat" (forms A_2p5h100.dat)
0