开发者

What is the DOS search pattern expression to find files named "Main" with any integer extension?

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

What is the DOS search pattern expression to find files named "Main" with any integer extension?

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 expressio开发者_JAVA百科n.

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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜