Solved

Batch File: Skip number 5

Posted on 2010-08-25
41
554 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
  • 12
  • 12
  • 6
  • +5
41 Comments
 
LVL 10

Expert Comment

by:honestman31
Comment Utility
@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 20 total points
Comment Utility
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
Comment Utility
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
 
LVL 10

Expert Comment

by:honestman31
Comment Utility
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 20 total points
Comment Utility
@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
Comment Utility
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 20 total points
Comment Utility
@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 11

Expert Comment

by:Ben Personick
Comment Utility
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
Comment Utility
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 11

Expert Comment

by:Ben Personick
Comment Utility
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
Comment Utility
@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 11

Expert Comment

by:Ben Personick
Comment Utility
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 11

Expert Comment

by:Ben Personick
Comment Utility
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
Comment Utility
==>QCubed

Still not it, but very cool script though!!

Thanks,
Rene
0
 
LVL 11

Expert Comment

by:Ben Personick
Comment Utility
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 11

Expert Comment

by:Ben Personick
Comment Utility
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
Comment Utility
==>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
Comment Utility
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 40 total points
Comment Utility
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
Comment Utility
==>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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 11

Assisted Solution

by:Ben Personick
Ben Personick earned 200 total points
Comment Utility
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 11

Expert Comment

by:Ben Personick
Comment Utility
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
Comment Utility
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
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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 11

Expert Comment

by:Ben Personick
Comment Utility
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 51

Accepted Solution

by:
Bill Prew earned 120 total points
Comment Utility
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 11

Expert Comment

by:Ben Personick
Comment Utility
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 51

Expert Comment

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

~bp
0
 
LVL 11

Expert Comment

by:Ben Personick
Comment Utility
dude, it's all good ^^  glad to have you aboard the Q. =)
0
 
LVL 8

Assisted Solution

by:SylvainDrapeau
SylvainDrapeau earned 80 total points
Comment Utility
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
Comment Utility
(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 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
Nice!

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

Thanks bp
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
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
Comment Utility
Like I sed, ... I feel like an idiot... I need some rest!

Cheers,
Rene
0
 
LVL 10

Author Comment

by:ReneGe
Comment Utility
But your help realy helped me.

Thanks to you all.

Cheers again,
Rene
0
 
LVL 11

Expert Comment

by:Ben Personick
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This is about my first experience with programming Arduino.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now