3

Use * with a batch file to get file names

 3 years ago
source link: https://www.codesd.com/item/use-with-a-batch-file-to-get-file-names.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Use * with a batch file to get file names

advertisements

I am using Windows XP and need to create a batch file to move tok.filename to filename.tok. This works for one file if I type in fix.bat tok.filename.

set filename=%1
set newname=%filename:~4,45%
ren %1 %newname%.tok

I need to type in fix.bat tok*, but this puts tok* in the filename.

How do I get it to read all the files into the filename one at a time?


Use a for statement.

setlocal enabledelayedexpansion

for %%i in (tok.*) do (
  set filename=%%i
  set newname=!filename:~4,45!
  ren %%i !newname!.tok
)

Enabling the delayed expansion makes it so that the variables are evaluated at the time they are used.

Alternatively, since you already have a batch file that works, you could write another batch file that uses the for statement, which calls your working batch file -- like this:

for %%i in (tok.*) do call fix.bat %%i

Or you could run it directly from the command line like this:

for %i in (tok.*) do call fix.bat %i


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK