Monday, September 28, 2009

DOS BAT File Copy Rename with String Manipulation

I needed to rename a directory of files to truncate the file names to the first 5 characters (plus the file extension).  I found one example that showed how two write a FOR loop that broke out the filename and extension and passed it to a second BAT file:

for /f %%a IN (’dir /b *.txt’) do call runner.bat %%~na %%~xa

And using a second example that covered DOS string manipulation, I put together this batch file that accepts two parameters and does a substring to get the first 5 characters of the first parameter:

set file=%1
set file=%file:~0,5%
ren %1%2 %file%%2

 

No comments: