Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Windows 7 batch file help

23 Apr 2015   #1
crollinjones

Windows 7 64 bit
 
 
Windows 7 batch file help

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 System SpecsSystem Spec
.
23 Apr 2015   #2
Ztruker

Windows 10 Pro X64
 
 

My System SpecsSystem Spec
24 Apr 2015   #3
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

Hi Crollinjones,

Quote   Quote: Originally Posted by crollinjones View Post
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 System SpecsSystem Spec
.

24 Apr 2015   #4
crollinjones

Windows 7 64 bit
 
 
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!

Quote   Quote: Originally Posted by Pyprohly View Post
Hi Crollinjones,

Quote   Quote: Originally Posted by crollinjones View Post
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 System SpecsSystem Spec
24 Apr 2015   #5
crollinjones

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 System SpecsSystem Spec
24 Apr 2015   #6
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

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 System SpecsSystem Spec
Reply

 Windows 7 batch file help




Thread Tools Search this Thread
Search this Thread:

Advanced Search




Similar help and support threads
Thread Forum
reading file in windows batch
i have a file b.txt with entries like : explorer.exe pid: 3136 type: File A24: D:\ftproot\logs\tagswap notepad.exe pid: 7732 type: File 18: D:\ftproot\logs\tagswap I want to read each record and extract pid and kill it. The following is only giving...
General Discussion
Batch File: works under cmd-Windows , NOT under MS-DOS
I made these codes to backup files : set p=C:\Users\ for /f %%a in ('dir C:\Users\ /b') do ( if not "%%a"=="Public", ( set fileDest=C:\Backup-%%a_%date:~7,2%.%date:~4,2%.%date:~10,4%
Virtualization
How to run a batch file at logoff & some batch scripting help please
Hiya, I'd just like to run a simple batch file at logoff to backup some settings. I plan to use Robocopy, which is what i use to more thorough backups. Two questions: 1. How make it run by clicking Start > Shutdown? (i realise i could make a shutdown batch file and run it instead, but i'd...
Backup and Restore
Batch File that grabs file names from folder to insert into batch file
Ok I'm totally at a loss on how to word this so I'm just going to write it out the best that I can... :confused: First I have a batch file that I'm writing that will convert mp4 files to mp3 files (I have a lot) for a buddies band using ffmpeg. Here's the code that I'm using: ffmpeg -i...
General Discussion
Is it possible to create a windows batch file to do this?
I generally run windows aero theme across all four monitors which i am happy with. However when i run a music app called cubase 5, i hear all sorts of squeaks and glitches when i simply move my cursor around the screen. The problem resolves itself when i switch to the standard windows classic...
General Discussion
Batch File Error Windows 7 x64
Hey Guys, Tried writing a batch file to open a few programs I use together regularly, but which I don't want all the time on startup. I've tried: start “Maxivista” /d “C:\Program Files\MaxiVista Pro Server\MaxiVistaAll.exe" start “Netbeans” /d "R:\Program Files\NetBeans...
General Discussion


Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd

All times are GMT -5. The time now is 17:57.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App