Windows 7 batch file help


  1. Posts : 3
    Windows 7 64 bit
       #1

    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 Computer


  2. Posts : 6,285
    Windows 10 Pro X64
       #2
      My Computer


  3. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #3

    Hi Crollinjones,

    crollinjones said:
    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


  4. Posts : 3
    Windows 7 64 bit
    Thread Starter
       #4

    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!

    Pyprohly said:
    Hi Crollinjones,

    crollinjones said:
    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


  5. Posts : 3
    Windows 7 64 bit
    Thread Starter
       #5

    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


  6. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #6

    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


 

  Related Discussions
Our Sites
Site Links
About 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:56.
Find Us