3

I have a program that generated unknown numbers of files with integer extension as follows.

enter image description here

I want to append .eps to each. How to do this in a DOS batch file?

I cannot use the following because I don't know the search expression.

for %%x in (Main.<what>) do rename "%%x" "%%x.eps"

Note: Any files having the same name with non-integer extension must be left as is.

Second Person Shooter
  • 14,188
  • 21
  • 90
  • 165

2 Answers2

3
setlocal EnableDelayedExpansion
for %%x in (Main.*) do (
    set ext=%%~Xx
    set /a num=!ext:~1!
    if !num! gtr 0 rename "%%x" "%%x.eps"
)

First SET get just the extension of file name, including the dot (with ~X). Second SET /A try to convert the extension (without the dot with :~1) to number. If it is really a number (greater than zero) do the rename.

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • There might be issues with numbers that start with 0s. In batch scripting, such numbers are considered to be in octal notation, and thus values like `083`, `097` would cause errors in the `SET /A` command. Same goes about values that start with decimal digits but also contain some letters. The command shell would attempt to treat such a value as hexadecimal, and if contained letters outside the range from `A` to `F` (case-insensitive), an error would occur. – Andriy M Aug 01 '11 at 19:49
1

This should loop through the directory. Here is the explanation on how it works:

tokens = 1-2 means we only care about the first and second part of the file name.

delims = . means to split the tokens on the . in the file name.

dir /b means to only list the files, don't show any of the . or .. that is normally shown in the dir command. You need to put the directory in there or run from the same file directory.

LSS a char will always return GREATER THAN, that's how this works. I picked 9999, you can pick whatever.

Finally, it takes the first part of the file name (%%A) and then the extension (%%B) then renames to first part of the file name (%%A) with the eps extension.

for /f "tokens=1-2 delims=." %%A in ('dir /b') do if %%B LSS 9999 rename %%A.%%B %%A.%%B.eps

Put this in your batch file and run it.

Note: You can't rename to the same filename, so I used %%A.%%B.eps.

  • Assuming that works at all, how would it avoid renaming files with non-numeric extensions? – Carey Gregory Jul 30 '11 at 00:23
  • @Carey: The numerics have nothing to do with it. The extension is treated as a "token" because we specify the delimiter as the period, so it splits on the period. I just tried it here and it works. –  Jul 30 '11 at 00:29
  • Add an IF expression for the second (%%B) token like in the answer above it think it will work - at least it will work little better than in my answer - it will still rename star.00XXX.star (((( – Boris Treukhov Jul 30 '11 at 00:34
  • The OP said "Note: Any files having the same name with non-integer extension must be left as is." – Carey Gregory Jul 30 '11 at 00:36
  • 1
    @Carey: Fixed. Added IF statement. –  Jul 30 '11 at 00:54