Solved

Batch File: Skip number 5

Posted on 2010-08-25
41
557 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
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 20 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 20 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 20 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 11

Expert Comment

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

Expert Comment

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

Expert Comment

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

Expert Comment

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

Expert Comment

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

Expert Comment

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

Assisted Solution

by:Ben Personick
Ben Personick earned 200 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 11

Expert Comment

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

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 11

Expert Comment

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

Accepted Solution

by:
Bill Prew earned 120 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 11

Expert Comment

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

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 11

Expert Comment

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

Assisted Solution

by:SylvainDrapeau
SylvainDrapeau earned 80 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 53

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 53

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 53

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 11

Expert Comment

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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 …

831 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