Tsar Tsar - 1 month ago 5
Bash Question

Execute all commands in current folder sequentially

Assuming I have a bunch of

commands in .cmd files, order alphabetically e.g.:

  1. 01.setup.cmd

  2. 02.version1.cmd

  3. 03.version2.cmd

  4. 04.version3.cmd

how could one sequentially execute these in correct order with another .cmd file?


On windows:

for /F "tokens=*" %a in ('dir /b *.cmd') do call "%a"

This just loops over the result of dir /b *.cmd calling each in turn.

explanation from the docs:

FOR /F processing of a text file consists of reading the file, one line of text at a time and then breaking the line up into individual items of data called 'tokens'. The DO command is then executed with the parameter(s) set to the token(s) found.

So my command says:

  • "tokens=*" don't give me individual tokens, give me the whole line as one hit
  • %a - name the line variable %a (note: it'll needs to be escaped as %%a if you're putting it in a batch file
  • ('dir /b *.cmd') This is the input that it'll loop over. A bare directory listing for all .cmd files
  • then what I want it to do. Call the command %a.

If I didn't add the tokens bit it would work fine until you find a space in the file names.