Dchaps
Dchaps

Reputation: 89

.bat loop through directory, pass file names to executable as arguments

The executable I am running simply wants a file name as a parameter

@echo off
FOR  %%i IN (C:\Files\*.*) DO (
echo %%~nxi
start "mass extract.." "C:\Files\extractor.exe" %%~nxi
)

despite only returning the file names themselves, its not passing it to my exe as a parameter correctly, what am I missing?

Upvotes: 1

Views: 644

Answers (2)

Paul
Paul

Reputation: 2710

You have to send the working directory to extractor or the full path of each files.

3 ways:

method #1

Use cd or pushd

cd /d "C:\Files"
FOR %%i IN (*.*) DO (
echo %%~nxi
start "mass extract.." "C:\Files\extractor.exe" %%~nxi
)

method #2

Send the full path of each files with %%i (see call /? or for /?)

FOR %%i IN (C:\Files\*.*) DO (
echo "%%~i"
start "mass extract.." "C:\Files\extractor.exe" "%%~i"
)

method #3

specify in start the working directory.

FOR  %%i IN (C:\Files\*.*) DO (
echo %%~nxi
start "mass extract.." /D "C:\Files" "C:\Files\extractor.exe" %%~nxi
)

Upvotes: 1

touchofevil
touchofevil

Reputation: 613

instead of

start "mass extract.." "C:\Files\extractor.exe" %%~nxi

try

start "mass extract.." "C:\Files\extractor.exe" %%i

This would pass full file path.

Also may I suggest adding the path's in quotes? In case your location has any spaces the above wouldn't work correctly.

FOR  %%i IN ("C:\Files\*.*")

Upvotes: 1

Related Questions