Question

I want to run a command like this: foo.exe file1.c file 2.c file3.c ... and so on until I have listed all files under current directory

How can I do this in a script?

EDIT: Answers below do not work and it has to do with spaces in filenames. Here is what dir /b /a-d is giving me:

c:\Users\morpheus\temp>dir /b /a-d
ceiling GRF Model 24x36.pdf
conference room 02.pdf
conference room 03.pdf
conference room.pdf
conference room01.pdf
CONFERENCE TABLE  detail.pdf
CONFERENCE TABLE.pdf
door detail.pdf
door.pdf
ELEVATION GF24X36.pdf
flooring GRF Model 24x36.pdf
junction detail 01.pdf
junction detail.pdf
MODULAR GRF Model 24x36.pdf
RECEPTION PART PLAN GF.pdf
reception table edited.pdf
reception table.pdf

@cookiebutter, what I get with your solution is:

foo.exe  "ceiling"  "conference"  "conference"
 "conference"  "conference"  "CONFERENCE"  "CONFERENCE"  "door"  "door.pdf"  "EL
EVATION"  "flooring"  "junction"  "junction"  "MODULAR"  "RECEPTION"  "reception
"  "reception"

The solution that is able to handle spaces in filenames is like this:

echo off
setlocal ENABLEDELAYEDEXPANSION
set params=
for %%i in ('dir *.* /b') do (
if exist %%i set params=!params! "%%i"
)
echo on
foo.exe %params%

I will accept cookiebutter's answer as it set me off in right direction.

Was it helpful?

Solution

This should do it.

I don't recall what it is but there is a limit as to the number of characters you can include on a single line so if you have hundreds of files you want to include, this probably will not work for you.

But if you don't have too many, this will work.

echo off
setlocal ENABLEDELAYEDEXPANSION
set params=
for /f %%i in ('dir /b /a-d') do (
    set params=!params! "%%i"
)
echo on
foo.exe %params%

OTHER TIPS

1) If you want the foo.exe to be executed with each file seperately you can use

for /F "delims=" %%i in ('dir /b /a-d') do (foo.exe "%%i" )

2) If you want the foo.exe to take as input parameters all the files you can use

@set str4=

for /F "delims=" %%i in ('dir /b /a-d') DO call :append %%i
@echo %str4%
foo.exe %str4%

:append
@set str4=%str4% %1

Please let me know if it works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top