简体   繁体   中英

Use .bat file to loop through folders/subfolder and and provide the folder/subfolder as input to an exe

I have to run a tool(.exe) that is stored in a particular folder.(Say C:\\Temp)
The input parameter for this tool is the (.inf) files placed in various other folders/subfolders.(Say C:\\Test\\SampleInfFiles, C:\\Test\\SampleInfFiles\\Inf2)

I want to create a batch file where i can run this tool recursively on the various folders.

I am not sure how to provide the folder name of the inf as the input parameter to the tool.

@echo off 
for /r /d C:\Test\SampleInfFiles %%x in (dir /b *.inf) do(
pushd "%%x"
call C:\Test\ScanInf.exe
popd
)

I am new to all this. Could you please help create a batch file.

If I understand correctly, you need to specify the folder that contains one or more inf files to the executable, and not the inf files itself.

Apparently the order of parameters is important. for expects the part after /r to be a path. Furthermore, you can just specify * as a set.

The loop now browses all subfolders of the given folder. Inside the loop, it will check if that folder contains an inf file (on that level). If it does, the executable is called with the folder as parameter. Notice the use of ~ in the variable. This strips off any quotes, so you can safely add quotes without risking to have double quotes.

@echo off 
for /d /r C:\Test\SampleInfFiles\ %%x in (*) do (
  pushd "%%~x"
  if exist %%x\*.inf call C:\Test\ScanInf.exe "%%~x"
  popd
)

See: Using batch parameters

Here as a nice one-liner:

for /R "C:\Test\SampleInfFiles" /D %%X in (*.*) do if exist "%%~fX\*.inf" call C:\Test\ScanInf.exe "%%~fX"

If you want to run that in the command prompt directly, replace each %% by % .

I think the call command could be omitted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM