Running two For loops Simultaneously (not one after another)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ganon64
Posts: 3
Joined: 09 Feb 2012 16:59

Running two For loops Simultaneously (not one after another)

#1 Post by ganon64 » 09 Feb 2012 17:09

Hi,

I currently have the following code that I'm using for testing a proof of concept. Basically at the moment whilst the %w variable will cycle through it's possible values per loop, the %f value will not and always shows only the first possible value. Hence the output from this script is just Hi <w1>, nice to meet <f1> and Hi <w2> nice to meet <f1> and Hi <w3>, nice to meet <f1>. I'm looking for a way to get that %f variable to remember where it got to per %w loop. Please can anyone help?

Code: Select all

set def="c:\temp\doc1.txt"
set def2="c:\temp\doc2.txt"

setlocal enabledelayedexpansion
for /f "delims=*" %%w in ('Type %def%') do (call :Fix %%w
)
GOTO :eof

:Fix
for /f %%f in ('dir /b "C:\temp\test space"') do (
set g=%%f
set h=!g:.txt=!
echo Hi %%w, nice to meet !h!
GOTO :eof
)

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Running two For loops Simultaneously (not one after anot

#2 Post by Squashman » 09 Feb 2012 17:19

I don't know why you aren't just using nested for loops in this instance.

But when you pass a variable with a CALL you then reference that variable just like you do with any other batch file. You would use %1.

You can get rid of the SET statements and just use the FOR loop modifiers to remove the file extension. Then you don't need to use delayed expansion at all.

Code: Select all

echo Hi %1, nice to meet %%~nf

ganon64
Posts: 3
Joined: 09 Feb 2012 16:59

Re: Running two For loops Simultaneously (not one after anot

#3 Post by ganon64 » 09 Feb 2012 17:46

Thank you for your help Squashman. I've amended it in regards to your suggestions but it still produces the same output =(. I also tried nested For loops but I am not hugely familiar with them and so the output resulted in it running the 2nd 'For loop' multiple times per %w iteration. Is there anyway to get only one output of %f assigned to only one output of %w and not lots of results that are all mix matched? Here is my code:

Amended (without nested loops)

Code: Select all

set def="c:\temp\doc1.txt"
set def2="c:\temp\doc2.txt"

for /f "delims=*" %%w in ('Type %def%') do (call :Fix %%w
)
GOTO :eof

:Fix
for /f %%f in ('dir /b "C:\temp\test space"') do (
echo Hi %1, nice to meet %%~nf
GOTO :eof
)


My attempt at using a nested For loop

Code: Select all

set def="c:\temp\doc1.txt"
set def2="c:\temp\doc2.txt"

for /f "delims=*" %%w in ('Type %def%') do (
for /f %%f in ('dir /b "C:\temp\test space"') do (
echo Hi %%w, nice to meet %%~nf )
)

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Running two For loops Simultaneously (not one after anot

#4 Post by dbenham » 09 Feb 2012 18:47

Your posted code and question still doesn't make any sense. The 2nd loop extracts the name from the file name of a single file that has no apparent relationship to the data in the file from the first loop. How do you know which file name matches up with data contained in another file :? :?:

Dave Benham

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Running two For loops Simultaneously (not one after anot

#5 Post by Squashman » 09 Feb 2012 18:53

I guess I am misunderstanding what you are trying to do.

ganon64
Posts: 3
Joined: 09 Feb 2012 16:59

Re: Running two For loops Simultaneously (not one after anot

#6 Post by ganon64 » 09 Feb 2012 19:19

I managed to get the desired output through a very tedious script (see below). It involved kind of cheating and using the Skip parameter of the 2nd For loop to remember where it got to previously. My original plans for this proof of concept were that I was going to use it to change registry keys that required looking up a list of user SIDs in one text file and a list of usernames in another text file. Ideally though I suppose for that situation I guess I'd need to generate a CSV file somehow and read it back using delimters.

Code: Select all

set def="c:\temp\doc1.txt"
set def2="c:\temp\doc2.txt"

@echo off
setlocal enabledelayedexpansion
set /a n=-1
for /f "delims=*" %%w in ('Type %def%') do (
set /a n=!n!+1
call :Fix %%w
)
GOTO :eof

:Fix
if !n! GTR 0 (
set SKIP="skip=!n!"
) else (
  set SKIP=
)
for /f %SKIP% %%f in ('dir /b "C:\temp\test space"') do (
@echo on
echo Hi %1, nice to meet %%~nf
@echo off
GOTO :eof
)

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Running two For loops Simultaneously (not one after anot

#7 Post by Squashman » 09 Feb 2012 20:12

So you basically wanted a 1 to 1 line by line combination of two input files.

Aacini
Expert
Posts: 1930
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Running two For loops Simultaneously (not one after anot

#8 Post by Aacini » 09 Feb 2012 20:41

ganon64 wrote:Is there anyway to get only one output of %f assigned to only one output of %w ...?
Perhaps is this what you are looking for?

Code: Select all

set def="c:\temp\doc1.txt"
set def2="c:\temp\doc2.txt"

@echo off
setlocal enabledelayedexpansion
dir /b "C:\temp\test space" > dirOutput.txt
< dirOutput.txt ( for /f "delims=*" %%w in ('Type %def%') do (
   set /P name=
   echo Hi %%w, nice to meet !name:.txt=!
   )
)
Previous code matches each line in doc1.txt file with each file shown by dir comand, that is, first line with first file, second line with second file, etc...

Post Reply