?
Solved

Batch File: Skip number 5

Posted on 2010-08-25
41
Medium Priority
?
564 Views
Last Modified: 2012-05-10
Hi there,

In the following script, I need to skip number 5.

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /L %%A in (1,1,10) do (
IF %%A == 5 GOTO :EOF
echo %%A
)
ECHO.
echo DONE
ECHO.
pause

EXPECTED OUTPUT:
-------------------------------
1
2
3
4
6
7
8
9
10

DONE


Thanks for your help,
Rene
0
Comment
Question by:ReneGe
[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
  • 12
  • 12
  • 6
  • +5
41 Comments
 
LVL 10

Expert Comment

by:honestman31
ID: 33524737
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /L %%A in (1,1,10) do (
IF %%A == 5 GOTO :TT

echo %%A

:TT
)
ECHO.
echo DONE
ECHO.
pause
0
 
LVL 3

Assisted Solution

by:robertodeacruz
robertodeacruz earned 80 total points
ID: 33524742
Change to:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /L %%A in (1,1,10) do (
 IF NOT %%A == 5 echo %%A
)
ECHO.
echo DONE
ECHO.
pause
0
 
LVL 8

Expert Comment

by:SylvainDrapeau
ID: 33524763
Hello !

Try this :

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /L %%A in (1,1,10) do (if not %%A == 5 (echo %%A))
ECHO.
echo DONE
ECHO.
pause

Syldra
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Expert Comment

by:honestman31
ID: 33524764
Sorry I think this the one ( remove the : after GOTO )
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /L %%A in (1,1,10) do (
IF %%A == 5 GOTO  TT

echo %%A

:TT
)
ECHO.
echo DONE
ECHO.
pause
0
 
LVL 3

Assisted Solution

by:NevTon
NevTon earned 80 total points
ID: 33524817
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /L %%A in (1,1,10) do (
IF NOT %%A == 5 echo %%A
)
ECHO.
echo DONE
ECHO.
pause
0
 
LVL 10

Author Comment

by:ReneGe
ID: 33525044
I need to conditionnaly skip a command line withine a FOR command, without exiting the loop.

I should have beed clearer in my initial post.

==> honestman31:
It does not work, but that is what I need to resolve.

Thanks guys for you help,
Rene
0
 
LVL 3

Assisted Solution

by:superiz
superiz earned 80 total points
ID: 33525210
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /L %%A in (1,1,10) do (
IF %%A neq 5 echo %%A
)
ECHO.
echo DONE
ECHO.
pause
0
 
LVL 14
ID: 33525225
ReneGe there is a lot to be questioned about the exact meaning of your request!

I THINK what you're saying is this (I am going to use a call instead of delayed expansion to explain because I thin kit's easier to follow  (See attached code for reference)

I believe you want to skip the 5th command if the item in the loop is #5 and the 6th if the item in the loop is #6.

Is this correct?

@ECHO OFF

FOR /L %%A IN () DO CALL :Start-Commands
ECHO.
ECHO DONE!
ECHO.
PAUSE
GOTO :EOF
:Start-Commands
Command 1
Command 2
Command 3
Command 4
Command 5
Command 6
Command 7
Command 8
Command 9
Command 10
GOTO :EOF
:End-Commands

Open in new window

0
 
LVL 10

Author Comment

by:ReneGe
ID: 33525241
The conditional ECHO was just an example of the command line I need to skip.

I need to conditionnaly skip a command line within a FOR command, without exiting the loop, not a conditional ECHO.

Thanks,
Rene
0
 
LVL 14
ID: 33525301
If that is the case would change your loop to work like this (here I will use Delayed Expansion again for you)


@ECHO OFF 
SETLOCAL ENABLEDELAYEDEXPANSION
SET CMDList="Command 1", "Command 2", "Command 3", "Command 4", "Command 5", "Command 6", "Command 7", "Command 8", "Command 9", "Command 10"

FOR %%A IN (%CMDList%) DO (
	SET "CurrentCMD=%%A"
	FOR %%a IN (%CMDList%) DO (
		IF !CurrentCMD! NEQ %%A ECHO %%A
		IF !CurrentCMD! NEQ %%A CALL %%A
	)
)
ECHO.
ECHO DONE
ECHO.
ENDLOCAL
PAUSE

Open in new window

0
 
LVL 10

Author Comment

by:ReneGe
ID: 33525310
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /L %%A in (1,1,10) do CALL :StartCMDs %%A

ECHO.
echo DONE
ECHO.

pause
EXIT


:StartCMDs
IF %~1 == 5 exit /b
echo %~1
exit /b


But without the CALL...

Is it possible?

Thanks,
Rene
0
 
LVL 14
ID: 33525315
whoops screwed up my capitalization!  My bad!  pleas use attached instead!

@ECHO OFF 
SETLOCAL ENABLEDELAYEDEXPANSION
SET CMDList="Command 1", "Command 2", "Command 3", "Command 4", "Command 5", "Command 6", "Command 7", "Command 8", "Command 9", "Command 10"

FOR %%A IN (%CMDList%) DO (
	SET "CurrentCMD=%%A"
	FOR %%a IN (%CMDList%) DO (
		IF !CurrentCMD! NEQ %%b ECHO %%b
		IF !CurrentCMD! NEQ %%b CALL %%b
	)
)
ECHO.
ECHO DONE
ECHO.
ENDLOCAL
PAUSE

Open in new window

0
 
LVL 14
ID: 33525334
that lowercase a also needs to be a lowercase b, yeesh, sorry I hate having to re-post so often! with such minor typo changed  Grr!

@ECHO OFF 
SETLOCAL ENABLEDELAYEDEXPANSION
SET CMDList="Command 1", "Command 2", "Command 3", "Command 4", "Command 5", "Command 6", "Command 7", "Command 8", "Command 9", "Command 10"

FOR %%A IN (%CMDList%) DO (
	SET "CurrentCMD=%%A"
	FOR %%b IN (%CMDList%) DO (
		IF !CurrentCMD! NEQ %%b ECHO %%b
		IF !CurrentCMD! NEQ %%b CALL %%b
	)
)
ECHO.
ECHO DONE
ECHO.
ENDLOCAL
PAUSE

Open in new window

0
 
LVL 10

Author Comment

by:ReneGe
ID: 33525347
==>QCubed

Still not it, but very cool script though!!

Thanks,
Rene
0
 
LVL 14
ID: 33525381
SIGH, I feel so ridiculous, I didn't add the /I switch to the IF statements!!



@ECHO OFF 
SETLOCAL ENABLEDELAYEDEXPANSION
SET CMDList="Command 1", "Command 2", "Command 3", "Command 4", "Command 5", "Command 6", "Command 7", "Command 8", "Command 9", "Command 10"

FOR %%A IN (%CMDList%) DO (
	SET "CurrentCMD=%%A"
	FOR %%b IN (%CMDList%) DO (
		IF /I !CurrentCMD! NEQ %%b ECHO %%b
		IF /I !CurrentCMD! NEQ %%b CALL %%b
	)
)
ECHO.
ECHO DONE
ECHO.
ENDLOCAL
PAUSE

Open in new window

0
 
LVL 14
ID: 33525416
Thanks RenGe!

If you are trying to skip ALL the commands ENTIRELY, and ONLY when the number is = to 5, it's even simpler than what I did try this.

PS what is your distaste for CALL?

@ECHO OFF
SET CMDList="Command 1", "Command 2", "Command 3", "Command 4", "Command 5", "Command 6", "Command 7", "Command 8", "Command 9", "Command 10"
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /L %%A in (1,1,10) DO (
	ECHO.
	ECHO Set %%A Begining
	ECHO.
	IF /I "%%A" NEQ "5" FOR %%b IN (%CMDList%) DO CALL %%b
	ECHO.
	ECHO Set %%A Completed.
	ECHO.
)
ECHO.
ECHO DONE
ECHO.
ENDLOCAL
PAUSE

Open in new window

0
 
LVL 10

Author Comment

by:ReneGe
ID: 33525497
==>QCubed:

Compared to a streignt command line, CALL add's an extra complication to a script. But if to avoid a CALL, the script must be more complicated, well I'll use a CALL.

What are your thoughts about this?

Thanks,
Rene
0
 
LVL 10

Expert Comment

by:honestman31
ID: 33525611
this will work 100 %  tested

@echo off
for /l %%A in (1,1,10) DO (
If   %%A NEQ 5 echo %%A

 )

0
 
LVL 10

Assisted Solution

by:honestman31
honestman31 earned 160 total points
ID: 33525644
don't forget the last )

@echo off
for /l %%A in (1,1,10) DO (
If   %%A NEQ 5 echo %%A
 )
@echo off
for /l %%A in (1,1,10) DO ( 
If   %%A NEQ 5 echo %%A 
)

Open in new window

0
 
LVL 10

Author Comment

by:ReneGe
ID: 33525713
==>honestman31

The focus here is not the result displayed. It's my need to to conditionnaly skip a command line within a FOR command, without exiting the loop.

Without using CALL, and within a single command line, like your the if %%A == 5 GOTO TT, but working.

Thanks,
Rene
0
 
LVL 14

Assisted Solution

by:Ben Personick (Previously QCubed)
Ben Personick (Previously QCubed) earned 800 total points
ID: 33526053
Renge, the "CALL" I use in the second command is to CALL your command which I assume is a windows NT command or external command, it is NOT necessary to use the CALL in the command, but since you are delaying I figured it would be a good Idea to CALL the commands instead.

CALL, in my mind, simplifies matters, but it's 6 one way half a dozen the other.

I tend to use CALL because I like to write subroutines which the script can call repeatedly instead of having to re-write the dame code over and over again, this keeps scripts with repeated actions shorter, and more concise.

but it's possible that if you add enough fore loops you can replicate that behavior inside the loops, however that gets very complex, and hard to follow, and easy to make a mistake.  Not only that but it it can add a lot of overhead too.

again 6 one way half a dozen the other as when I call the code pauses, and hunts through the script for my lable ,causing the process to take slightly longer. (in today's computer world, the time difference is negligible unless your script is huge and/or you call things tens of thousands of times, in which case you can get around that by sticking the sub functions at the beginning of the script instead of the end.)

Also I thought you were putting full path to the command so I wrapped it all in a double quote and CALLED it instead of just stripping the quotes and running it.
0
 
LVL 14
ID: 33526170
I have altered the code NOT to use the CALL, though the usage of CALL there wasn't going to cause any hunting..

@ECHO OFF
SET CMDList="Command 1", "Command 2", "Command 3", "Command 4", "Command 5", "Command 6", "Command 7", "Command 8", "Command 9", "Command 10"
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /L %%A in (1,1,10) DO (
	IF /I "%%A" NEQ "5" FOR %%b IN (%CMDList%) DO (
		SET "TmpCMD=%%b"
		!TmpCMD:"=!
	)
)
ECHO.
ECHO DONE
ECHO.
ENDLOCAL
PAUSE

Open in new window

0
 
LVL 8

Expert Comment

by:SylvainDrapeau
ID: 33527047
Hello again !

here's a golden shiny one-liner that skips number 5, with no CALL and without leaving the loop :

-----------
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
FOR /L %%A IN (1,1,10) DO (IF %%A == 5 (GOTO :EOF) ELSE (ECHO %%A))
ECHO.
ECHO DONE
ECHO.
PAUSE
-----------

I hope this is what you need.

Syldra
0
 
LVL 8

Expert Comment

by:SylvainDrapeau
ID: 33527112
Wait, wait, wait...

When I do this on the command line, it works fine :
FOR /L %A IN (1,1,10) DO (IF %A == 5 (GOTO :EOF) ELSE (ECHO %A))

but when I launch the code above, it stops at 4...

I'll look into it...

Syldra
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 33527163
Rene,

I'm confused why this doesn't meet your need:

@echo off
for /l %%A in (1,1,10) DO (
  If %%A NEQ 5 (
    echo %%A
  )
  REM rest of logic here...
)

In this case the echo line is "skipped" when the loop value is 5.  This sounded like what you wanted.  Then for all values the rest of the logic inside the for loop can be executed.

~bp
0
 
LVL 14
ID: 33527183
I think he wants to skip all commands if the value of the loop is 5.  I gave him that code, in essence others did similar code.
0
 
LVL 56

Accepted Solution

by:
Bill Prew earned 480 total points
ID: 33527200
So then, wouldn't that just be this?

@echo off
for /l %%A in (1,1,10) DO (
  If %%A NEQ 5 (
    echo %%A
    REM rest of logic here...
  )
 )

~bp
0
 
LVL 14
ID: 33527216
yes, again I think we've all posted variations on that code, but he doesn't seem happy with it and hasn't responded further yet..
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 33527233
Okay, sorry, bust day at the office, just checking in on what's going on...

~bp
0
 
LVL 14
ID: 33527242
dude, it's all good ^^  glad to have you aboard the Q. =)
0
 
LVL 8

Assisted Solution

by:SylvainDrapeau
SylvainDrapeau earned 320 total points
ID: 33527259
Ok, last comment on that one for me, here's a new batch, tested and working :

-------------
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
FOR /L %%A IN (1,1,10) DO (IF NOT %%A == 5 (ECHO %%A&ECHO TEST))
ECHO.
ECHO DONE
ECHO.
PAUSE
-------------

Note the use of a "&" to separate multiple commands... this allows you to specify multiple commands on a single line...

I guess this does what you asked... multiple commands, single line, skip 5 and does not exits loop.

Done... I guess.

Syldra
0
 
LVL 10

Author Comment

by:ReneGe
ID: 33527610
(hey bp!!)

Hi guys,

Sorry for taking so long to reply, I just arrived.

I just passed through all of your posts and I must admit, I feel like an idiot... Maybe 80h/week of work is ment to make you be one, but that's another thread ;-)

Ok now, after reviewing all of your posts, I had a glimps of a GRRRRR... realizing that all I needed is "ELSE".



@echo off
FOR /L %%A in (1,1,10) DO (
      If %%A NEQ 5 (
            ECHO %%A
      ) ELSE (
            REM DO NOTHING
      )
)
ECHO. && ECHO DONE && ECHO.
PAUSE
EXIT



I realy wish that it would be possible to give 500 points to each of you but it's not possible. So I'll split the points according to the number of posts or by the level of contribution.

I just hope you will feel that my appreciation is far greater than the points I can allow.

So again, a big thanks and a casual cheers,
René
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 33527622
If you aren't going to do anything in the ELSE block, then you don't need it.

~bp
0
 
LVL 10

Author Comment

by:ReneGe
ID: 33527652
Nice!

This Batch file will loop more than 30K times. Limiting code makes a difference.

Thanks bp
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 33527669
If you really do need to just loop over 1 to 10 except 5, then this would be even faster:

FOR %%A in (1,2,3,4,6,7,8,9,10) DO (
  ECHO %%A
)

~bp
0
 
LVL 10

Author Comment

by:ReneGe
ID: 33527695
Thanks bp,

What I realy need, is:

FOR /L %%A in (1,1,10) DO If %%A NEQ 5 (
  DO SOMETHING
  DO SOMETHING ELSE
)

Cheers,
Rene
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 33527708
So that is the same as this:

FOR %%A in (1,2,3,4,6,7,8,9,10) DO (
  DO SOMETHING
  DO SOMETHING ELSE
)

~bp
0
 
LVL 10

Author Comment

by:ReneGe
ID: 33527740
No because, in my real script, the missing number 5 is a condition that must be met.

Cheers,
Rene
0
 
LVL 10

Author Comment

by:ReneGe
ID: 33527759
Like I sed, ... I feel like an idiot... I need some rest!

Cheers,
Rene
0
 
LVL 10

Author Comment

by:ReneGe
ID: 33527768
But your help realy helped me.

Thanks to you all.

Cheers again,
Rene
0
 
LVL 14
ID: 33530025
Quite welcome ReneGe!  you're going to have to post us a copy of this script and a brief explanation of what it does one day, with the number of questions it's spun I really wonder what all these little pieces we've been working on with you add-up to in the big picture!  =)  Cheers to you as well, happy hunting.

~Q
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Starting up a Project
Suggested Courses

765 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