Batch process all files in directory

49,035

Solution 1

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.

Solution 2

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 %.

Share:
49,035

Related videos on Youtube

Gabriel
Author by

Gabriel

Updated on March 14, 2020

Comments

  • Gabriel
    Gabriel over 3 years

    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.

  • Gabriel
    Gabriel over 12 years
    Awesome. Able to use this to achieve what I needed. Thanks.
  • Snekse
    Snekse over 11 years
    Can you explain how to avoid the trouble of using set inside the for loop?
  • Rich
    Rich over 11 years
    Snekse, the magic word there is delayed variable expansion. It's something I have to explain in every second batch file answer I give. It's well explained in help set too.
  • Rich
    Rich over 10 years
    Thanks for the edit, tastybento. It was indeed correct (but I took the liberty of changing the variable name to make it easier to understand); alas there was a number of reviewers with no idea what they were reviewing who rejected it. To those reviewers: If you don't understand an edit, you can skip reviewing it and leave it to those who do. None of you have any visible batch file experience (at least on SO).

Related