BAT command to insert a char. in a file name
Solved/Closed
Related:
- BAT command to insert a char. in a file name
- Windows 10 iso file download 64-bit - Download - Windows
- To display a text file in reverse order what command should be used - Guide
- Change computer name command line - Guide
- Cs 1.6 money command - Guide
- How to insert a checkmark in word - Guide
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
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
@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
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!
@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!
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.
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.
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Feb 21, 2010 at 08:37 AM
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)
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)