Batch Functions: goto:eof, exit /b and structure.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
shadeclan
Posts: 61
Joined: 02 Jun 2011 11:29
Location: USA - Somewhere between Albany NY and Bennington VT

Re: Batch Functions: goto:eof, exit /b and structure.

#16 Post by shadeclan » 06 Jun 2011 11:05

I know what you mean. I program for New York State, which is even farther behind the technological curve than the private sector. We've got legacy systems stretching the gamut from mainframe COBOL and M204 to web programming and everything in between. Of course, our bosses want everyone to know how to do everything with everything which is clearly impossible for this humble programmer.

Speaking of code, this might interest you if you use or need something like CMDOW:

Code: Select all

@echo off
setlocal enabledelayedexpansion
rem set _NO_DEBUG_HEAP=1

set vYr=%date:~10,4%
set vMon=%date:~4,2%
set vDay=%date:~7,2%
set vHr=%time:~0,2%
set vMn=%time:~3,2%
set vSec=%time:~6,2%%time:~9,2%
set vFN=%vYr%%vMon%%vDay%%vHr%%vMn%%vSec%

rem @echo Checking to see if IDiary is already started
cd /d "%dCMDOW%"

if exist IDD.txt del /q IDD.txt

for /f "tokens=1-8,*" %%a in ('cmdow /f /b') do (
   set hndl=%%a
   set ttl=%%i

   if "!hndl:~0,2!"=="0x" if "%%b"=="1" if "%%h"=="iDD" (
      if "!ttl:~0,11!"=="iDailyDiary" (
         @echo %%a %%b %%c %%d %%e %%f %%g %%h %%i>>IDD.txt
      )
      if "!ttl:~0,5!"=="Diary" (
         @echo %%a %%b %%c %%d %%e %%f %%g %%h %%i>>IDD.txt
      )
   )
)

if exist IDD.txt (
   for /f "tokens=1-8,*" %%a in (IDD.txt) do (
      start cmdow.exe %%a /ACT /RES
   )
)else (
   if exist "E:\SyncEXP\Individual\SyncEXP.exe" (
      @echo syncing diaries before start.
rem      start /wait /d"E:\My Apps\Dependency Walker\" depends.exe /cpb /ot:"logs\SyncExpIDD_%vFN%.log" "E:\SyncEXP\Individual\SyncEXP.exe" -Diary
      start /wait /d"E:\SyncEXP\Individual\" SyncEXP.exe -Diary
   )
   
   if exist "%PROGRAMFILES%\My Apps\iDailyDiary\iDD.exe" (
      start /b /d"%PROGRAMFILES%\My Apps\iDailyDiary" iDD.exe
   )
)

rem pause

endlocal
exit /b 0
exit

In this example CMDOW is used to check for the existence of IDailyDiary windows (an application I cannot live without) EnableDelayedExpansion is envoked so that the variables "hndl" and "ttl" can be checked in the IF statement. If IDailyDiary is already running, CMDOW is used to activate the correct windows. If not, the application is started after running a synchronizing application (that keeps my home and work diaries synchronized).

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Batch Functions: goto:eof, exit /b and structure.

#17 Post by Ed Dyreen » 06 Jun 2011 11:12


shadeclan
Posts: 61
Joined: 02 Jun 2011 11:29
Location: USA - Somewhere between Albany NY and Bennington VT

Re: Batch Functions: goto:eof, exit /b and structure.

#18 Post by shadeclan » 06 Jun 2011 11:49

Ed Dyreen wrote:CMDOW u say ?, I've heard about it :o
http://www.dostips.com/forum/viewtopic.php?f=3&t=365&start=0&hilit=cmdow
Well, you obviously have been using CMDOW for a long time, but I don't know what language this is nor have I ever seen CMDOW used as an object in code. It's an EXE - what, did you find the sourcecode and create a DLL out of it?!?

Next Day:
Well, I am definitely a retard! Duh! The language is DOS of course - the escape characters and dot notation threw me off because I have never had to use any (until yesterday when I had to echo some pipes while creating a SQL file). I definitely have to examine this code though.

A Little Later:
I'm afraid this is pretty advanced stuff - far over my head. You appear to be loading a string of DOS commands into a loop variable. Call me stupid but I can't figure out how it's all supposed to work. You want to explain it to me?

shadeclan
Posts: 61
Joined: 02 Jun 2011 11:29
Location: USA - Somewhere between Albany NY and Bennington VT

Re: Batch Functions: goto:eof, exit /b and structure.

#19 Post by shadeclan » 07 Jun 2011 08:01

To return to the original subject, how's about a function library?
@echo off
setlocal enabledelayedexpansion
set p1=%1%

goto:BEGIN

:here
@echo Here function executed!

exit /b 0

:there
@echo There - function executed!

exit /b 0

:BEGIN
if defined p1 goto:LIBRARY
goto:END

:LIBRARY
if /i %p1%==here call:here&goto:END
if /i %p1%==there call:there&goto:END


:END
endlocal
exit /b 0
exit

Then, just call the "library" and use the label of the function as the first parameter:

Code: Select all

call MyLibrary.bat here


Ed, I have never accessed error codes (exit /b) but I assume they would come as some sort of output from the function or batch file. How do you redirect an error code into a variable for examination?

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Batch Functions: goto:eof, exit /b and structure.

#20 Post by Ed Dyreen » 07 Jun 2011 09:11

Maybe u should do some homework.

MS-DOS/MSDOS Batch Files: Batch File Tutorial and Reference :arrow:
http://www.allenware.com/icsw/icswidx.htm

shadeclan
Posts: 61
Joined: 02 Jun 2011 11:29
Location: USA - Somewhere between Albany NY and Bennington VT

Re: Batch Functions: goto:eof, exit /b and structure.

#21 Post by shadeclan » 07 Jun 2011 09:12

Thanks, Ed. I will.

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Batch Functions: goto:eof, exit /b and structure.

#22 Post by jeb » 07 Jun 2011 16:34

Ed Dyreen wrote:But an exit /b is indeed better than a goto :eof
It allows us to use return-codes and is noticably faster when used in larger scripts.

Why do you think it is faster :?:

exit /b and goto :eof seems to be nearly the same, I measured it and there is no relevant speed difference. :)

Btw. I assume exit /b is nearly the same as goto :Eof

Code: Select all

setlocal DisableExtensions
exit /b

Produces on my system the error message
Das Sprungziel - EOF wurde nicht gefunden.

The label - EOF can't be found

jeb

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Batch Functions: goto:eof, exit /b and structure.

#23 Post by Ed Dyreen » 07 Jun 2011 16:39


Wel jeb if you say so I believe you :mrgreen:
Ed Dyreen wrote:
But an exit /b is indeed better than a goto :eof
It allows us to use return-codes and is noticably faster when used in larger scripts.

Why do you think it is faster :?:

exit /b and goto :eof seems to be nearly the same, I measured it and there is no relevant speed difference. :)

goto :eof == go to the end of the file ... end of file reached exiting ...
exit /b == exiting ...

Was my thinking :roll:


Why did I say "noticably" ?, should have just said; 'Might be faster', cause I never tested the speed :oops:

shadeclan
Posts: 61
Joined: 02 Jun 2011 11:29
Location: USA - Somewhere between Albany NY and Bennington VT

Re: Batch Functions: goto:eof, exit /b and structure.

#24 Post by shadeclan » 09 Jun 2011 06:28

Ed Dyreen wrote:Why did I say "noticeably" ?, should have just said; 'Might be faster', cause I never tested the speed :oops:
Well, my first point was that it made the code more "sensible" and easier to read for people who might not be as acquainted with DOS script as is Super-Batcher Mr. Dyreen ;). One would expect to find something at the end of a function like "Exit" or "End" rather than "goto:eof" - and, as Ed points out, "Exit /b" allows you to return error codes for evaluation. Always nice to be able to add error trapping to your code.

prasad.gujar
Posts: 1
Joined: 01 Apr 2013 23:26

Re: Batch Functions: goto:eof, exit /b and structure.

#25 Post by prasad.gujar » 01 Apr 2013 23:29

Thanks for the post mate, this info solved a critical fault in my script. :D

shadeclan
Posts: 61
Joined: 02 Jun 2011 11:29
Location: USA - Somewhere between Albany NY and Bennington VT

Re: Batch Functions: goto:eof, exit /b and structure.

#26 Post by shadeclan » 02 Apr 2013 08:38

prasad.gujar wrote:Thanks for the post mate, this info solved a critical fault in my script. :D

Was it one of my scripts or something that Ed said?

In any case, you're welcome.

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Batch Functions: goto:eof, exit /b and structure.

#27 Post by Aacini » 02 Apr 2013 09:29

I know this is a very old thread; however, I couldn't resist the temptation to insert a couple comments here.

Many, many years ago, when I was hired for the first time as computer programmer, my boss was worried about a critical section of a program that must generate a very important result. The key point of the program was an IF statement that must check a certain value and do a certain process. My boss wanted to take some precautions, so he forced me to write such important IF statement twice! in "case that the first IF may fail" as he explained me. I don't remember which language was used for such program (Fortran IV or Algol), but the equivalent Batch code would be something similar to this one:

Code: Select all

if %variable% equ 123 call :ImportantProcess
if %variable% equ 123 call :ImportantProcess
Yes! The final production program had such line written twice. The ImportantProcess just generate a certain number that was recorded in a disk file later. I sincerely apologize, but your code makes me to remember such story...

I get used to use GOTO :EOF to end the Main Program, and EXIT /B to end a subroutine. From this point of view, a Batch file that return an ERRORLEVEL value is a subroutine even if it is called from the command line. However, if a Main Program ends at the end of the Batch file, I used to insert no ending command at all.

Antonio

shadeclan
Posts: 61
Joined: 02 Jun 2011 11:29
Location: USA - Somewhere between Albany NY and Bennington VT

Re: Batch Functions: goto:eof, exit /b and structure.

#28 Post by shadeclan » 02 Apr 2013 09:40

Aacini wrote:I know this is a very old thread; however, I couldn't resist the temptation to insert a couple comments here.

Many, many years ago, when I was hired for the first time as computer programmer, my boss was worried about a critical section of a program that must generate a very important result. The key point of the program was an IF statement that must check a certain value and do a certain process. My boss wanted to take some precautions, so he forced me to write such important IF statement twice! in "case that the first IF may fail" as he explained me. I don't remember which language was used for such program (Fortran IV or Algol), but the equivalent Batch code would be something similar to this one:

Code: Select all

if %variable% equ 123 call :ImportantProcess
if %variable% equ 123 call :ImportantProcess
Yes! The final production program had such line written twice. The ImportantProcess just generate a certain number that was recorded in a disk file later. I sincerely apologize, but your code makes me to remember such story...

I get used to use GOTO :EOF to end the Main Program, and EXIT /B to end a subroutine. From this point of view, a Batch file that return an ERRORLEVEL value is a subroutine even if it is called from the command line. However, if a Main Program ends at the end of the Batch file, I used to insert no ending command at all.

Antonio

DOS is a very old language so it lends itself to very old threads ... :D

Wow - that's crazy what your boss had you do. Call me crazy but I think that if it doesn't work the first time, it won't work the second time, either.

The double Exit was just something I stumbled on in a frustrating moment. As Ed said, it should not be necessary and he was never able to reproduce the problem that necessitated it.

I'm one of those programmers that work in multiple languages and have a hard time keeping them all straight. Putting an Exit on the end of subroutines or main routines is a simple thing, easy to remember, that helps me avoid any unpleasantness.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Batch Functions: goto:eof, exit /b and structure.

#29 Post by Squashman » 02 Apr 2013 10:41

shadeclan wrote:DOS is a very old language so it lends itself to very old threads ... :D

DOS is an old operating system. DISK OPERATING SYSTEM. What you mean is batch is old but in reality as far as scripting languages and shells go it is not even as old as CSH or TCSH or Korn for that matter.

shadeclan
Posts: 61
Joined: 02 Jun 2011 11:29
Location: USA - Somewhere between Albany NY and Bennington VT

Re: Batch Functions: goto:eof, exit /b and structure.

#30 Post by shadeclan » 02 Apr 2013 10:44

Squashman wrote:
shadeclan wrote:DOS is a very old language so it lends itself to very old threads ... :D

DOS is an old operating system. DISK OPERATING SYSTEM. What you mean is batch is old but in reality as far as scripting languages and shells go it is not even as old as CSH or TCSH or Korn for that matter.

It's old to me! :D

Post Reply