timonsku
timonsku

Reputation: 1259

How to use a wildcard in a batch script with CLI tools that need a specific file name?

I have a cli application that doesn't like the usage of wildcards. In this example the usage of *.dat. I just get an error that the file *.dat is not valid.

I have a folder with thousands of files that need to be processed by this tool. So doing it manually is a no go. I encountered quite a few application where I had this problem but this time it's rather important. A general solution how to deal with those application would be very nice.

Can I maybe make a file list of all *.dat files and feed it to the application? It's not necessary that I use batch script but it seemed like the most simple solution so far.

Upvotes: 4

Views: 2427

Answers (1)

Joey
Joey

Reputation: 354466

You can use a for loop:

for %%x in (*.dat) do mycommand "%%x"

That would start the command once for each file. If you want to aggregate them you have to do a little more work:

setlocal enabledelayedexpansion
set Count=0
set List=
for %%x in (*.dat) do (
  set List=!List! "%%x"
  set /a Count+=1
  if !Count! GEQ 50 (
    mycommand !List!
    set List=
    set Count=0
  )
)

This would pass 50 files at a time to the command. You can tweak that number if desired. The problem is if you have thousands of files in the folder, then you cannot simply list them all in a single command line (because there is a maximum command line length limit), so you have to process them in chunks.

Upvotes: 5

Related Questions