Batch process all files in directory

2019-01-23 17:06发布

Right now i have a batch job I wrote that calls another file and passes in the variables that executable needs to run (password and filename).

Ex:

> cd f:\test\utils 
> admin import-xml -Dimport.file=f:\DB\file1.xml  -Dadmin.db.password=test123

I wrote a job that does this, but found out that there would be multiple files.

The username and password never change but the filename differs for like 15 different xml files--with maybe more coming soon.

The files will always be located in the same folder. Instead of ending up with like 15-20 jobs (one for each file), can I write something that will process each file located in this directory. And either wait till one is completed before the next or I can add a 3 min sleep before it starts the next file.

3条回答
放荡不羁爱自由
2楼-- · 2019-01-23 17:28

You can use the for command. Something like this in a batch file:

for %%f in (*.xml) do call myotherbatch.bat %%f

Assuming that the admin command you are running doesn't return until the job is finished, the above loop would process them sequentially.

If you run the command at the prompt (as opposed to in a batch file), only use a single %.

查看更多
干净又极端
3楼-- · 2019-01-23 17:37
for file in f:\DB\*
do
   admin import-xml -Dimport.file="$file" -Dadmin.db.password=test123
done
查看更多
萌系小妹纸
4楼-- · 2019-01-23 17:41
pushd C:\test\utils
for %%F in (F:\DB\*.xml) do (
   admin import-xml "-Dimport.file=%%~dpnxF" -Dadmin.db.password=test123
)
popd

The %%~dpnxF expands to d‎rive, p‎ath, base‎n‎ame and e‎x‎tension of the current file.

If you intend to set and use environment variables (%foo%) in that loop, read help set first before you get into trouble.

查看更多
登录 后发表回答