How to compare two substrings or measure their length

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

How to compare two substrings or measure their length

#1 Post by alan_b » 07 Jan 2012 06:43

I am reading a file of registry keys and or folder paths into variables.
Each variable has a value such as
A=%LocalAppData%\Temp\Comodo\Firewall Pro\Data\Logs\TempFiles\
or alternatively without the trailing '\'
A=%LocalAppData%\Temp\Comodo\Firewall Pro\Data\Logs\TempFiles

I wish to know if variable %A% and %B% are a perfect match throughout the length of %B%

e.g. These are perfect matches for which I must do extra processing
B=%LocalAppData%\Temp\Comodo\Firewall Pro\Data
B=%LocalAppData%\Temp\Comodo\Firewall Pro\Data\Logs\TempFiles

This is a perfect match but %B% is too long and thus is of no interest
B=%LocalAppData%\Temp\Comodo\Firewall Pro\Data\Logs\TempFiles\Firewall\

I think FINDSTR could help, but I do not know how to use it to best effect.

I would also be interested in how to rapidly measure the length of a variable.

Regards
Alan

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to compare two substrings or measure their length

#2 Post by dbenham » 07 Jan 2012 08:40

Hi alan_b.

I thought you knew about the DOSTIPS function library. :?:

All you need is the :strLen function, coupled with some simple substring manipulation. No need for FINDSTR.

Code: Select all

@echo off
setlocal enableDelayedExpansion
set "A=%LocalAppData%\Temp\Comodo\Firewall Pro\Data\Logs\TempFiles"
set "B=%LocalAppData%\Temp\Comodo\Firewall Pro\Data"
call :strlen B len
if defined A if "!A:~0,%len%!"=="!B!" echo More processing needed
exit /b

:strLen string len -- returns the length of a string
::                 -- string [in]  - variable name containing the string being measured for length
::                 -- len    [out] - variable to be used to return the string length
:: Many thanks to 'sowgtsoi', but also 'jeb' and 'amel27' dostips forum users helped making this short and efficient
:$created 20081122 :$changed 20101116 :$categories StringOperation
:$source http://www.dostips.com
(   SETLOCAL ENABLEDELAYEDEXPANSION
    set "str=A!%~1!"&rem keep the A up front to ensure we get the length and not the upper bound
                     rem it also avoids trouble in case of empty string
    set "len=0"
    for /L %%A in (12,-1,0) do (
        set /a "len|=1<<%%A"
        for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
    )
)
( ENDLOCAL & REM RETURN VALUES
    IF "%~2" NEQ "" SET /a %~2=%len%
)
EXIT /b

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: How to compare two substrings or measure their length

#3 Post by alan_b » 07 Jan 2012 10:38

Thanks Dave

I was not aware of http://www.dostips.com/DtCodeCmdLib.php#
I see a lot of good reading there - and a good chance of getting a migraine :roll:

I use LastPass and this forum is one of my click-n-go shortcuts to get logged in
If I ever feel the need for the parent "Home" there is unfortunately no HOME button,
so although I can go into the browser address bar and edit all the way back to
http://www.dostips.com/
I now rarely do so because unfortunately Firefox is not as useable as it was.

Firefox has gone crazy with its insane race toward version 999.999
and is even no longer functional with extensions that it still considers as compatible.
e.g. "British English Dictionary R1.19 for Firefox" is still installed, but it does not work any more.

I have now gone to Palemoon which is based on proven stable Firefox code,
and "British English Dictionary R1.19 for Firefox" again gives me red squiggles - even when my spelling is correct :roll: .
I now find editing the address bar is feasible again.

On the top of every page I can select 6 things, including Logout.

I am disappointed that FAQ only explains things such as
"What is BBCode?"

It would be a very nice improvement if between FAQ and Search we could have one more item
DosTips - The DOS Batch Guide

Again, thanks for the code and your patience.

Regards
Alan

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: How to compare two substrings or measure their length

#4 Post by alan_b » 09 Jan 2012 09:17

I have a habit of closing a script, and also a code section that was called via a label, with
GOTO :EOF

You seem to have a different habit, twice in your solution above, of
EXIT /B

Is there any difference between the consequences, other than my habit wastes two bytes more ?
e.g. Does it have any effect upon the %errorlevel% that is returned ?

N.B.
I am aware of, and have used, the technique of deliberately returning a chosen number as errorlevel via
EXIT /B Number.

Regards
Alan

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to compare two substrings or measure their length

#5 Post by dbenham » 09 Jan 2012 10:07

I'm pretty sure there is no difference (other than the fact exit /b supports the errorlevel option, which you already know).

I think jeb posted an example somewhere that shows somehow that EXIT /B is translated into GOTO :EOF within the internals of CMD.EXE.

Going way back to true DOS, I believe the GOTO :EOF syntax predates the introduction of EXIT /B. I think GOTO :EOF became entrenched in peoples minds before EXIT /B became available, and so it seems to be the more prevelent form even today. Young developers who have never worked with true DOS see mostly the GOTO :EOF form in examples, so the trend continues.

I happen to prefer EXIT /B just because it makes more sense to me. In my mind, GOTO :EOF implies that CMD.EXE looks for the end of the file, whereas EXIT /B implies that the batch is exited immediately. I think the latter is closer to the truth.

I also like the consistency that EXIT /B provides. The same command is used whether or not you want to specify the return ERRORLEVEL.

Dave Benham

aGerman
Expert
Posts: 4656
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to compare two substrings or measure their length

#6 Post by aGerman » 09 Jan 2012 12:10

Code: Select all

@echo off &setlocal EnableDelayedExpansion
for %%i in (1a 1b 2a 2b 3a 3b 4a 4b 5a 5b) do (
  cmd /c exit 0 &REM "reset" errorlevel
  call :sub_%%i
  echo %%i - !errorlevel!
)
pause
goto :eof


:sub_1a
echo a|find "b"
goto :eof

:sub_1b
echo a|find "b"
exit /b

:sub_2a
echo a|find "b" &goto :eof

:sub_2b
echo a|find "b" &exit /b

:sub_3a
setlocal
echo a|find "b"
endlocal
goto :eof

:sub_3b
setlocal
echo a|find "b"
endlocal
exit /b

:sub_4a
setlocal
echo a|find "b"
endlocal &goto :eof

:sub_4b
setlocal
echo a|find "b"
endlocal &exit /b

:sub_5a
goto :eof|find /c ""

:sub_5b
exit /b|find /c ""


Result:
1a - 1
1b - 1
2a - 1
2b - 1
3a - 1
3b - 1
4a - 1
4b - 1
0
0
5a - 1
0
5b - 1

Only the 5th gives me an idea that goto :eof and exit /b is eventually not the same. But I cannot explain the behaviour of find /c in this case :?

Regards
aGerman

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to compare two substrings or measure their length

#7 Post by dbenham » 09 Jan 2012 12:32

:sub_5a and :sub_5b are actually giving the same result :!: :D

They look different because 5a is falling through to 5b. Both sides of the pipe are executed in their own CMD.EXE process under command line context. So neither GOTO :EOF nor EXIT/B exit the function of the parent batch process.

Here is an extract of the relevent output when @ECHO OFF is removed from the top of the batch.

Code: Select all

D:\utils>(cmd /c exit 0   & REM "reset" errorlevel
 call :sub_5a
 echo 5a - !errorlevel! )

D:\utils>goto :eof  | find /c ""
0

D:\utils>exit /b  | find /c ""
0
5a - 1

D:\utils>(cmd /c exit 0   & REM "reset" errorlevel
 call :sub_5b
 echo 5b - !errorlevel! )

D:\utils>exit /b  | find /c ""
0
5b - 1


Dave Benham

aGerman
Expert
Posts: 4656
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to compare two substrings or measure their length

#8 Post by aGerman » 09 Jan 2012 12:44

You're absolutely right, Dave :oops:
Normally I repeat my tests with @prompt $g$s instead of @echo off to avoid those mistakes.

Regards
aGerman

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: How to compare two substrings or measure their length

#9 Post by alan_b » 09 Jan 2012 15:44

OOOOOOOPS

I had a tiny niggle of uncertainty.

I seem to have opened another can of worms to contemplate :lol:

Thanks

Regards
Alan

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

Re: How to compare two substrings or measure their length

#10 Post by jeb » 10 Jan 2012 11:57

dbenham wrote:I think jeb posted an example somewhere that shows somehow that EXIT /B is translated into GOTO :EOF within the internals of CMD.EXE.


Yes, you can see this with DisableExtensions

Code: Select all

@echo off
setlocal DisableExtensions
echo Start the test
exit /b

endlocal
echo ende
exit /b
:EOF
echo The Label EOF found
setlocal EnableExtensions
exit /b


Output wrote:Start the test
Das Sprungziel - EOF wurde nicht gefunden.


The error message can be translated to something like
The label EOF couldn't be found

jeb

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: How to compare two substrings or measure their length

#11 Post by alan_b » 10 Jan 2012 16:24

Thanks

Now I can see it.

Yet another "gotcha" that will trip me up when my memory has faded next year :cry:

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to compare two substrings or measure their length

#12 Post by dbenham » 10 Jan 2012 17:07

What is the "gotcha"? I don't see any potential issue.

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: How to compare two substrings or measure their length

#13 Post by alan_b » 11 Jan 2012 01:28

I admit I have not run Jeb's code,
but what I understood was that EXIT /B only does its job when extensions are enabled to translate this into GOTO :EOF
in which case I can normally use EXIT /B,
BUT if I should ever accept snippets of code from a forum (here or elsewhere) and try to use them
any EXIT /B code may fall apart if a snippet has disabled extensions,
and by then I will probably have forgotten what I have just learnt.

Regards
Alan

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to compare two substrings or measure their length

#14 Post by dbenham » 11 Jan 2012 06:37

Neither EXIT /B nor GOTO :EOF work if extensions are disabled :!:

The purpose of SETLOCAL DisableExtensions is to put CMD.EXE into a mode where it attempts to be compatible with the old COMMAND.COM, which did not support GOTO :EOF or EXIT /B. (Among other things)

The point of jeb's post was to provide evidence that EXIT /B is interpreted internally as GOTO :EOF within the internals of CMD.EXE. It provides more evidence that there is no functional difference between the two.


Dave Benham

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: How to compare two substrings or measure their length

#15 Post by alan_b » 11 Jan 2012 10:03

Thanks

I had not realized I was skating on such thin ice :roll: .

Regards
Alan

Post Reply