Skip to content
View in the app

A better way to browse. Learn more.

MSFN

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Setting Variables in nested FOR Loop

Featured Replies

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

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
)

  • Author

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

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!
)

  • Author

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.

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.