Page 1 of 1

1.Can a subroutine return a value? Yes. 2.Left/Right cut a s

Posted: 14 Apr 2008 16:29
by budhax
Hello,
Can a DOS subroutine return a value? Yes, sending to the subroutine the variable name to catch the returned value.
See those 2 examples.
Subroutine LeftCut returns the a string cut till the first left found character.
Subroutine RightCut returns the a string cut till the first right found character.


NOTES:
1. remove "::" in "::ECHO.!P2!" to see the subroutine process/steps.
2. remove /i in "IF /i NOT" to get subroutines case sensitive.
3. This "SET !P1!=!P2!" uses the interesting DOS ability that you can set a variable using an expression for its name.
So, in the LeftCut subroutine:

Code: Select all

SET !P1!=!P2!

is equivalent to

Code: Select all

SET ReturnedVar1=!P2!

4. This is a BELL char before "*** No".

Any comment?
Thanks.


Code: Select all

@ECHO OFF
@SETLOCAL ENABLEDELAYEDEXPANSION
::-------------------------------------------------------------- LEFT CUT STRING
Call:LeftCut "ReturnedVar1" "192.168.100.11\INETPUB\Web PROJECTS" "\"
ECHO.%ReturnedVar1%

ECHO.
ECHO.

::------------------------------------------------------------- RIGHT CUT STRING
Call:RightCut "ReturnedVar2" "192.168.100.11\INETPUB\Web PROJECTS" "\"
ECHO.%ReturnedVar2%

ECHO.
ECHO.

::------------------------------------------------------------- RIGHT CUT STRING
Call:RightCut "ReturnedVar3" "192.168.100.11\INETPUB\Web PROJECTS" "x"
ECHO.%ReturnedVar3%
Pause&EXIT


:LeftCut
SET P1=%~1&::Returned Variable Name
SET P2=%~2&::String To Cut
SET P3=%~3&::Character To Find
:ContinueLCut
SET P2=!P2:~1!
::ECHO.!P2!
IF /i "!P2!"=="" (COLOR 0C&ECHO.*** No [!P3!] In The STRING&GOTO:EOF)
IF /i NOT "!P2:~,1!"=="!P3!" (GOTO:ContinueLCut) ELSE (SET !P1!=!P2!&GOTO:EOF)


:RightCut
SET P1=%~1&::Returned Variable Name
SET P2=%~2&::String To Cut
SET P3=%~3&::Character To Find
:ContinueRCut
SET P2=!P2:~,-1!
::ECHO.!P2!
IF /i "!P2!"=="" (COLOR 0C&ECHO.*** No [!P3!] In The STRING&GOTO:EOF)
IF /i NOT "!P2:~-1!"=="!P3!" (GOTO:ContinueRCut) ELSE (SET !P1!=!P2!&GOTO:EOF)