Windows 7 batch file help

crollinjones

New member
Local time
9:59 AM
Messages
3
I use a program from the command line with two inputs:
prog file1.exA file2.exB
Where .exA is an input file for prog
and .exB is the second file required for the file.

I would like a to create a batch file to call the program that loops through all the *.exA files using all the .exB files. For example, if I have 10 .exA files and 10 .exB files, that would be 100 runs of the program.

Can I use nexted for loops in a batch file?

Thanks for your assistance!
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Dell
OS
Windows 7 64 bit

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Lenovo IdeaCenter 450
OS
Windows 10 Pro X64
CPU
Intel Quad Core i7-4770 @ 3.4Ghz
Memory
16.0GB PC3-12800 DDR3 SDRAM 1600 MHz
Graphics Card(s)
Intel Integrated HD Graphics
Sound Card
Realtek HD Audio
Monitor(s) Displays
HP 22" LCD
Screen Resolution
1680 x 1050
Hard Drives
250GB Samsung EVO SATA-3 SSD
2TB Seagate ST2000DM001 SATA-2
1.5TB Seagate ST3150041AS SATA
Keyboard
Dell USB
Mouse
Lenovo USB
Internet Speed
Cable via Road Runner 3MB Upload, 30MB Download
Antivirus
Windows Defender, MBAM Pro, MBAE
Browser
Seamonkey
Other Info
UEFI/GPT
PLDS DVD-RW DH16AERSH
Hi Crollinjones,

Can I use nexted for loops in a batch file?
For loops can be nest in batch files.

Consider the following batch program,
Code:
for %%A in (file1.exA file2.exA file3.exA file4.exA) do @(
	for %%B in (file1.exB file2.exB file3.exB file4.exB) do @(
		echo prog %%A %%B
	)
	echo.
)

The output looks like,
Code:
prog file1.exA file1.exB
prog file1.exA file2.exB
prog file1.exA file3.exB
prog file1.exA file4.exB

prog file2.exA file1.exB
prog file2.exA file2.exB
prog file2.exA file3.exB
prog file2.exA file4.exB

prog file3.exA file1.exB
prog file3.exA file2.exB
prog file3.exA file3.exB
prog file3.exA file4.exB

prog file4.exA file1.exB
prog file4.exA file2.exB
prog file4.exA file3.exB
prog file4.exA file4.exB
 

My Computer

Computer type
PC/Desktop
OS
Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
Windows 7 Batch file help

Thank you Pyprohly,
That your method is perfect for the specific case where there are a few files of each type. Is there a way to acheive the same result for the general case where I want the batch file to search the directory for all the files of each type to create the two lists? In my particular case, file*.exA is in one directory and file*.exB is in a second directory.

Thanks for your assistance!

Hi Crollinjones,

Can I use nexted for loops in a batch file?
For loops can be nest in batch files.

Consider the following batch program,
Code:
for %%A in (file1.exA file2.exA file3.exA file4.exA) do @(
    for %%B in (file1.exB file2.exB file3.exB file4.exB) do @(
        echo prog %%A %%B
    )
    echo.
)

The output looks like,
Code:
prog file1.exA file1.exB
prog file1.exA file2.exB
prog file1.exA file3.exB
prog file1.exA file4.exB
 
prog file2.exA file1.exB
prog file2.exA file2.exB
prog file2.exA file3.exB
prog file2.exA file4.exB
 
prog file3.exA file1.exB
prog file3.exA file2.exB
prog file3.exA file3.exB
prog file3.exA file4.exB
 
prog file4.exA file1.exB
prog file4.exA file2.exB
prog file4.exA file3.exB
prog file4.exA file4.exB
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Dell
OS
Windows 7 64 bit
Windows 7 Batch file help

I did some more testing and found that the following works:
@echo off
setlocal EnableDelayedExpansion
for %%a in (*.exA) do (
for %%b in ("dirb\*.exB") do (
echo "prog %%a %%b"

)
)
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Dell
OS
Windows 7 64 bit
Well here's some extra batch if you need it.

Code:
@echo off

REM For each file in %path_one% execute '%prog_name% A B' where A 
REM represents a file in %path_one% and B represents each file in %path_two%.

::
set prog_name="prog"
set path_and_filemask_one="C:\My\Path\*.exA"
set path_and_filemask_two="C:\My\Other\Path\*.exB"
::

for %%I in ( "%PATH_AND_FILEMASK_ONE:"=%" ) do ( set path_one="%%~dpI")
for %%I in ( "%PATH_AND_FILEMASK_TWO:"=%" ) do ( set path_two="%%~dpI")

pushd %PATH_ONE%
for /f "delims=" %%A in (' dir /a:-d /b "%PATH_AND_FILEMASK_ONE:"=%" ') do (
	for /f "delims=" %%B in (' dir /a:-d /b "%PATH_AND_FILEMASK_TWO:"=%" ') do (
		echo "%PROG_NAME:"=%" "%PATH_ONE:"=%\%%~nxA" "%PATH_TWO:"=%\%%~nxB"
	)
	echo.
)
popd

Remove the echo on line 18 to run the batch file for real.
 

My Computer

Computer type
PC/Desktop
OS
Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
Back
Top