Solved

How to get right value in loop in DOS shell script

Posted on 2012-03-30
16
427 Views
Last Modified: 2012-04-03
Would like to get the right value in this script:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set N=10
set var=A
for /l %%i in (1,1,%N% ) do (
  @echo %var%
  if %%i lss 3 (
    set %var%=B
  ) else (
   if %%i lss 6 (
     set %var%=C
   ) else (
     set %var%=D
   )
  )
)

Expecting the output as follows
A
B
C
C
C
D
D
D
D
D
Actually not. Can any gurus help me out?
0
Comment
Question by:jl66
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 2
  • 2
  • +1
16 Comments
 
LVL 11

Expert Comment

by:paultomasi
ID: 37790033
Providing you do have to set a variable to you values, A, B, C, D, then this will do it quite nicely.
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set N=10

for /l %%i in (1,1,%N%) do (
  if %%i lss 6 (
    if %%i lss 3 (
      if %%i lss 2 (
        set var=A
      ) else (
        set var=B
      )
    ) else (
      set var=C
    )
  ) else (
    set var=D
  )
  echo !var!
)

Open in new window

0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37790047
If you want to pre-set var to A and place ECHO at the start of the loop then you will need this:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set N=10
set var=A

for /l %%i in (1,1,%N%) do (
  echo !var!
  if %%i lss 5 (
    if %%i lss 2 (
      set var=B
    ) else (
      set var=C
    )
  ) else (
    set var=D
  )
)
exit /b

Open in new window

0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37790053
Are you sure you want:

    A B CCC DDDDD

instead of:

    A BB CCC DDDD

?
0
On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

 
LVL 11

Expert Comment

by:paultomasi
ID: 37790060
Similarly, you could place the ECHO at the end of the loop (as in the first program above) and pre-set var to A like this:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set N=10
set var=A

for /l %%i in (1,1,%N%) do (
  if %%i lss 6 (
    if %%i lss 3 (
      if %%i gtr 1 (
        set var=B
      )
    ) else (
      set var=C
    )
  ) else (
    set var=D
  )
  echo !var!
)

Open in new window

0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37790073
Here are two other methods (pre-processing and post-processing var):
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set N=10
set var=A

for /l %%i in (1,1,%N%) do (
  echo !var!
  if %%i equ 1 set var=B
  if %%i equ 2 set var=C
  if %%i equ 5 set var=D
)

Open in new window

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set N=10
set var=A

for /l %%i in (1,1,%N%) do (
  if %%i equ 2 set var=B
  if %%i equ 3 set var=C
  if %%i equ 6 set var=D
  echo !var!
)

Open in new window

0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37790077
This is another way you can do it:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set N=9
set var=ABCCCDDDDD

for /l %%i in (0,1,%N%) do (
  echo !var:~%%i,1!
)

Open in new window

0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37790079
Finally, here's the shortest way of doing it:
@for %%i in (A B C C C D D D D D) do @echo %%i

Open in new window

Yep! Just a one-liner!

Or like this:
@echo off
set var=A B C C C D D D D D

for %%i in (%var%) do echo %%i

Open in new window

0
 
LVL 11

Accepted Solution

by:
paultomasi earned 360 total points
ID: 37790227
YOUR ORIGINAL CODE. See the necessary changes below:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set N=10
set var=A

for /l %%i in (1,1,%N% ) do (
  echo !var!
  if %%i lss 2 (
    set var=B
  ) else (
   if %%i lss 5 (
     set var=C
   ) else (
     set var=D
   )
  )
)

Open in new window

0
 
LVL 70

Assisted Solution

by:Qlemo
Qlemo earned 100 total points
ID: 37791111
There is an obvious difference between
   set %var%=b
and
   set var=b

What Paul showed in all length above appears to be what you were really after, because the original way of setting would set the var named like the contents of %var% to be set. Example:
@echo on
setlocal EnableDelayedExpansion
set var=A
set A=1
set %var%=2
echo %var%, !%var%!

Open in new window

0
 
LVL 38

Assisted Solution

by:Gerwin Jansen, EE MVE
Gerwin Jansen, EE MVE earned 40 total points
ID: 37791786
Hi, I would just correct your script like this:

echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set N=10
set var=A
for /l %%i in (1,1,%N% ) do (
  @echo !var!
  if %%i lss 3 (
    set var=B
  ) else (
   if %%i lss 6 (
     set var=C
   ) else (
     set var=D
   )
  )
)

Open in new window


Output is:

A
B
B
C
C
C
D
D
D
D
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37791883
gerwinjansen

I think it was more challenging doing "A B CCC DDDD" which now leads me to think this may have in fact been a classroom exercise.

jl66 specified the following output:

    A
    B
    C
    C
    C
    D
    D
    D
    D
    D

I queried this in http:#37790053 however, there has been no feedback suggesting otherwise.
0
 

Author Closing Comment

by:jl66
ID: 37792342
Thanks a lot for everyone. Paul really made a big effort. Qlemo points out the key: I forgot the expression of the variable in loop: !xxx!.
No, this is not a classroom homework!
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37792496
jl66

It wouldn't have mattered even if it were a classroom exercise because you had done nearly all the work yourself anyway.

Qlemo did well to point out your wrong use of 'set %var%=value'.

Of course, the other problem relates to using '%var%' instead of '!var!' however, this was not explicitly pointed out to you - you likely gathered this looking at solutions. Apologies for not pointing out these two obvious issues from the onset.
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 37793045
@paul, when looking at the original code, the output specified by OP is wrong, imho, my output is right :-) Anyway, OP learned some here...
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37793197
gerwinjansen
You're probably right. Maybe I was reading more into the question than there really was.

jl66
Perhapss you would like to confirm which output you intended; column 1 or column 2:
           Column 1       Column 2

               A             A
               B             B
               C             B
               C             C
               C             C
               D             C
               D             D
               D             D
               D             D
               D             D

Open in new window

0
 

Author Comment

by:jl66
ID: 37801503
Not compromise anything. Both are right. The point is that as mentioned before I forgot !var! in loop. Thank all of you so much.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Run MS Access Module from Macro / Bat File 9 113
how to run this deletion request 4 45
SSRS - Powershell 7 75
Wild card search and replace using Notepad 7 67
The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question