Jump to content

Setting Variables in nested FOR Loop


Recommended Posts

Hi Folks,

This is my first post so please forgive me if its too wordy or unclear.

I'm trying to create a nested FOR loop in batch (w2003 SE SP2). What I want to do is take all the values in 1 text file and run each of them against each of the values in another text file.

The first text file has 1 entry on each line and the second text file has 2 entries which are delimited by a ":" (colon not weird smiley face).

The code is below. The echo's I've placed in Italics are just for testing as I want to see if the value has been set as the variable. In both cases the variable isnt being echo'ed.

I know that I could just use the %a %i %j rather than Variables but I'd prefer to use the variables as i think its clearer and may need to add further variables.

SETLOCAL EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('type WWN.txt') do (

Set WWN=%%a

Echo WWN is %WWN%

for /f "tokens=1-2 delims=:" %%i in ('type DirPort.txt') do (

Set Dir=%%i

Set Port=%%j

Echo Dir is %Dir% Port is %Port%

echo - symmask -sid -wwn %%a -dir %%i -p %%j -noprompt >>WWNDirport.txt

)

Echo WWN Processed is %WWN% >>WWNDIRPORT.txt)

thanks for any feedback

Link to comment
Share on other sites


What happens if you change it slightly like this?

SETLOCAL EnableDelayedExpansion
for /f "tokens=*" %%a in ('type WWN.txt') do (
Set WWN=%%a
Echo WWN is !WWN!
for /f "tokens=1-2 delims=:" %%i in ('type DirPort.txt') do (
Set Dir=%%i
Set Port=%%j
Echo Dir is !Dir! Port is !Port!
echo - symmask -sid -wwn %%a -dir %%i -p %%j -noprompt >>WWNDirport.txt
)
Echo WWN Processed is !WWN! >>WWNDIRPORT.txt
)

Link to comment
Share on other sites

What happens if you change it slightly like this?

SETLOCAL EnableDelayedExpansion
for /f "tokens=*" %%a in ('type WWN.txt') do (
Set WWN=%%a
Echo WWN is !WWN!
for /f "tokens=1-2 delims=:" %%i in ('type DirPort.txt') do (
Set Dir=%%i
Set Port=%%j
Echo Dir is !Dir! Port is !Port!
echo - symmask -sid -wwn %%a -dir %%i -p %%j -noprompt >>WWNDirport.txt
)
Echo WWN Processed is !WWN! >>WWNDIRPORT.txt
)

Firstly thanks for the reply. I've tried using the exclamation marks too but that will just echo "Dir is !Dir! Port is !Port! "

I'm not sure when to use exclamation marks and when to use percentage signs in general.

any other ideas?

Tony

Link to comment
Share on other sites

Try turning echoing off!

Make this your first line and run the script again.

@Echo off

Then when you've finished kicking yourself at reading the wrong lines in the console output try changing the code slightly:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "TOKENS=*" %%# IN (WWN.TXT) DO (
SET WWN=%%#
ECHO=WWN IS !WWN!
FOR /F "TOKENS=1-2DELIMS=:" %%* IN (DIRPORT.TXT) DO (
SET DIR=%%*
SET PORT=%%+
ECHO=DIR IS !DIR! PORT IS !PORT!
>>WWNDirport.txt ECHO=- symmask -sid -wwn %%# -dir %%* -p %%+ -noprompt
)
>>WWNDirport.txt ECHO=WWN Processed IS !WWN!
)

Link to comment
Share on other sites

Try turning echoing off!

Make this your first line and run the script again.

@Echo off

Then when you've finished kicking yourself at reading the wrong lines in the console output try changing the code slightly:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "TOKENS=*" %%# IN (WWN.TXT) DO (
SET WWN=%%#
ECHO=WWN IS !WWN!
FOR /F "TOKENS=1-2DELIMS=:" %%* IN (DIRPORT.TXT) DO (
SET DIR=%%*
SET PORT=%%+
ECHO=DIR IS !DIR! PORT IS !PORT!
>>WWNDirport.txt ECHO=- symmask -sid -wwn %%# -dir %%* -p %%+ -noprompt
)
>>WWNDirport.txt ECHO=WWN Processed IS !WWN!
)

Yzöwl, thanks a million for your help. I actualy had echo off set originally but i wanted to see how the script was processing so i knocked it off. Forgot to reset it.

Script working great.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...