Solved Batch Script to pad .1 file ext to .0001

October 13, 2011 at 10:21:34
Specs: Windows XP
I am trying to setup a batch script to automate the following process: I have a directory with several files (could be 2, could be hundreds) all named *.1. *.2, *.3, *.11, *.123. I need to join these files together but not in 1, 11, 123, 2, 3, order. I need to rename the .1 to .0001 and the .11 to .0011 and the .123 to .0123. I have been working with iterations of the FOR command for the past day and a half and I just can't get it right. Can anyone point me in the right direction? Here is the code I currently have. It is expanded a bit to help me troubleshoot...

For /f "tokens=* delims=" %%I in ('dir /a-d /b *.*') do (set str=%%~xI & set str2=000%str% & set str3=%str2:.=0% & set str4=%str3:~-4% & rename "%%~I" "%%~nI.%str4%")

My thinking is grab the extension (.1), add zeros (000.1), replace . with 0 (00001), take right 4 digits, (0001), rename to original file + . + new extension (filename.0001). Is there an easier way (or a way that works)? I am familiar with DOS, but I haven't done a lot in years, and I have never used the FOR command to this extent before. Thanks for nay help you can provide!


See More: Batch Script to pad .1 file ext to .0001

Report •

#1
October 13, 2011 at 13:10:58
✔ Best Answer
I am familiar with DOS, but I haven't done a lot in years
Probably because it died out over 15 years ago. For the record, confusing DOS and the Command Prompt is kinda like confusing a pickup truck with a golf cart. They both have a steering wheel and pedals, obviously they're the same thing.

You need to use delayed expansion.

setlocal EnableDelayedExpansion
For /f "delims=" %%I in ('dir /a-d /b *.*') do (
  set str=%%~xI
  set str=0000!str:.=!
  set str=!str:~-4!
  rename "%%~I" "%%~nI.!str!"
)

How To Ask Questions The Smart Way


Report •

#2
October 13, 2011 at 14:11:48
All douchery aside, thanks for the help.

Report •

#3
October 13, 2011 at 14:24:18
Oy! You're the one confusing the Ford F150 with a Club Car Precedent i2L.

And now I want a golf cart. I have no practical use for it and I have no idea how I'd store it, but I still want one.

How To Ask Questions The Smart Way


Report •
Related Solutions


Ask Question