setting variable within for loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
glenm
Posts: 2
Joined: 15 Mar 2011 16:26

setting variable within for loop

#1 Post by glenm » 18 Mar 2011 04:38

Hi,
My problem is as follows.

I have 2 directories software_test_R1.1 and software_test_R1.2 contained with a parent directory called test

My code is as follows

@echo off
for /f "tokens=* delims=" %%X in ('dir /ad /b') do (
for /f "tokens=3 delims=_" %%A IN ("%%X") do (
echo %%A
set version=%%A
echo %version%
)
)

I expected that within second for loop that echo %%A would give me the same value as echo %version%

What i get is

software_test_R1.1
R1.1
R1.2
software_test_R1.2
R1.2
R1.2

Does anyone know the fix for this ?

Thanks

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: setting variable within for loop

#2 Post by ChickenSoup » 18 Mar 2011 10:31

Didn't really look too closely but, try this:

Code: Select all

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "tokens=* delims=" %%X in ('dir /ad /b') do (
for /f "tokens=3 delims=_" %%A IN ("%%X") do (
echo %%A
set version=%%A
echo !version!
)

Post Reply