Variable won't expand

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MrKnowItAllxx
Posts: 43
Joined: 20 Mar 2012 20:53

Variable won't expand

#1 Post by MrKnowItAllxx » 10 Apr 2012 18:19

So I have created my own left trim algorithm, (follows the same principles of the rTrim function in the library) but it seems that the variable
!str:~1,%len%! never expands properly. If I replace the variable with a hard-coded value, the function works just fine, but I would like to have it to ensure I do not cut off my string. Any help is appreciated

Code: Select all

@echo off
set "string1=          string1"
set "string2=     string2  "
set "string3=              string3"

echo "%string1%"
echo "%string2%"
echo "%string3%"
call :lTrim string1 "%string1%" " "
call :lTrim string2 "%string2%" " "
call :lTrim string3 "%string3%" " "
echo "%string1%"
echo "%string2%"
echo "%string3%"
pause
goto :eof

:lTrim
rem Trims a character from the left side of a string.
rem -Requires length function
rem
rem    Syntax: rTrim [rVar] "string" "character"
setlocal enabledelayedexpansion
set "str=%~2"
set "char=%~3"
call :length len "%str%"
for /l %%a in (0,1,%len%) do if "!str:~0,1!"=="!char!" set "str=!str:~1,%len%!"
endlocal & if not "%~1"=="" set "%~1=%str%" & exit /b

:length
rem Returns the length of a string.
rem
rem    Syntax: length [rVar] "string"
setlocal enabledelayedexpansion
set "str=%~2"
set "len=0"
set "count=0"
:length_loop
if "!str:~%len%,1!"=="" (
endlocal & if not "%~1"=="" set %~1=%len% & exit /b
) else set /a len=len+1
goto :length_loop


Output (nothing I have tried except hard-coding the %len% variable has changed this)

Code: Select all

"          string1"
"     string2  "
"              string3"
"str:~1,17 "
"str:~1,14 "
"str:~1,21 "
Press any key to continue . . .


Also: the lTrim algorithm in the library just doesn't seem to work for me(?) which is why I am creating my own. I'll test it again just to be sure, but for now I would still like to have this function working.
Last edited by MrKnowItAllxx on 10 Apr 2012 20:11, edited 1 time in total.

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Variable won't expand

#2 Post by Fawers » 10 Apr 2012 19:16

Personally, I see no reason for your program to not work.
It seems that your only problem is that variable one.

Mate, I'd change this line

Code: Select all

for /l %%a in (0,1,%len%) do if "!str:~0,1!"=="!char!" set "str=!str:~1,%len%!"


to this

Code: Select all

for /l %%a in (0,1,%len%) do if "%str:~0,1%"=="%char%" set "str=%str:~1,!len!%"


Whenever I have to make a "VARception" - use a variable inside another -, I write %var!var2!% instead of !var%var2%!.

If it works not either, then you might want to make a new :SECTION qit h the FOR command in order to CALL it from another point.

MrKnowItAllxx
Posts: 43
Joined: 20 Mar 2012 20:53

Re: Variable won't expand

#3 Post by MrKnowItAllxx » 10 Apr 2012 20:04

I guess I'll have to try your suggestion of making a new section, the same output was generated

Output:

Code: Select all

"          string1"
"     string2  "
"              string3"
"str:~1,17 "
"str:~1,14 "
"str:~1,21 "
Press any key to continue . . .


Though now that I think about it, the outer variable is what is failing to expand, not the inner variable - I need to correct my original post

MrKnowItAllxx
Posts: 43
Joined: 20 Mar 2012 20:53

Re: Variable won't expand

#4 Post by MrKnowItAllxx » 10 Apr 2012 20:05

I will try using a section marker and another call statement though

MrKnowItAllxx
Posts: 43
Joined: 20 Mar 2012 20:53

Re: Variable won't expand

#5 Post by MrKnowItAllxx » 10 Apr 2012 20:09

This also fails, with exact same output

Code: Select all

@echo off
set "string1=          string1"
set "string2=     string2  "
set "string3=              string3"

echo "%string1%"
echo "%string2%"
echo "%string3%"
call :lTrim string1 "%string1%" " "
call :lTrim string2 "%string2%" " "
call :lTrim string3 "%string3%" " "
echo "%string1%"
echo "%string2%"
echo "%string3%"
pause
goto :eof

:lTrim
rem Trims a character from the left side of a string.
rem -Requires length function
rem
rem    Syntax: rTrim [rVar] "string" "character"
setlocal enabledelayedexpansion
set "str=%~2"
set "char=%~3"
call :length len "%str%"
for /l %%a in (0,1,%len%) do if "%str:~0,1%"=="%char%" call :lTrim_process
endlocal & if not "%~1"=="" set "%~1=%str%" & exit /b

:lTrim_process
set "str=%str:~1,!len!%"
goto :eof

:length
rem Returns the length of a string.
rem
rem    Syntax: length [rVar] "string"
setlocal enabledelayedexpansion
set "str=%~2"
set "len=0"
set "count=0"
:length_loop
if "!str:~%len%,1!"=="" (
endlocal & if not "%~1"=="" set %~1=%len% & exit /b
) else set /a len=len+1
goto :length_loop

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Variable won't expand

#6 Post by Fawers » 10 Apr 2012 20:13

MrKnowItAllxx wrote:Though now that I think about it, the outer variable is what is failing to expand, not the inner variable - I need to correct my original post


Did you try to check whether the parameters on the CALL command are working?

Remember you can always do this check with

Code: Select all

if [%N] == [] goto :ERROR

where N is the parameter number (and assuming there is an :ERROR section to return you an error message).

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Variable won't expand

#7 Post by Liviu » 10 Apr 2012 22:29

Not sure what library you refer to, and what did not work there, but your :length is returning an extra space (which is quite obvious if you look at the output you posted). Just replace

Code: Select all

endlocal & if not "%~1"=="" set %~1=%len% & exit /b
with

Code: Select all

endlocal & if not "%~1"=="" set "%~1=%len%" & exit /b

Liviu

MrKnowItAllxx
Posts: 43
Joined: 20 Mar 2012 20:53

Re: Variable won't expand

#8 Post by MrKnowItAllxx » 11 Apr 2012 16:00

I found and fixed this error last night, before having read liviu's post, but I suppose that is irrelevant

Thank you for your time :)

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

Re: Variable won't expand

#9 Post by Aacini » 11 Apr 2012 17:33

If you are using LTrim to just trim spaces, may I suggest you to use the simpler method that appear in this post?

Code: Select all

@echo off
set "string1=          string1"
set "string2=     string2  "
set "string3=              string3"

echo "%string1%"
echo "%string2%"
echo "%string3%"
call :lTrim string1
call :lTrim string2
call :lTrim string3
echo "%string1%"
echo "%string2%"
echo "%string3%"
pause
goto :eof


:LTrim strVar -- LeftTrim spaces from strVar
if not defined %1 exit /b
setlocal EnableDelayedExpansion
set "var=!%1!"
call :getFirstChar !var:%%=%%%%!
set "var=!var:*%firstChar%=%firstChar%!"
endlocal & set "%1=%var%"
exit /B

:getFirstChar
setlocal DisableDelayedExpansion
set "firstParam=%1"
endlocal & set "firstChar=%firstParam:~0,1%"
exit /B

Result:

Code: Select all

"          string1"
"     string2  "
"              string3"
"string1"
"string2  "
"string3"
Press any key to continue . . .

Post Reply