You are not logged in.
please help
it seems i can't get this code to work correctly
to successfully expand a var containing "!" char with delayed expansion
echo first test&echo.
call :cmdstring /string "qwer!y" /replace "!" "t"
echo.&echo.&echo second test&echo.
call :cmdstring /string "qwer!y" /replace "w" "t"
echo.&pause
goto :eof
:cmdstring
setlocal enabledelayedexpansion
set replacecounter=0
set "str2rep="
set "repstr="
for %%R IN (%*) do (
set "strparam=%%~R"
IF "!stringcounter!"=="1" set "string=%%~R" &set /a stringcounter+=1
IF "!replacecounter!"=="2" set "repstr=%%~R" &set /a replacecounter+=1
IF "!replacecounter!"=="1" set "str2rep=%%~R" &set /a replacecounter+=1
IF /I %%R==/replace set /a replacecounter+=1
IF /I "!strparam:~0,2!"=="/s" set /a stringcounter+=1
)
set old_string=%string%
set old_string
set str2rep
set repstr
set "new_string="
for /f "tokens=* delims=" %%b in ('
%comspec% /u /c echo:"%old_string%" ^|more ^|find /v """"') do (
if "%%b"=="!str2rep!" (
call set "new_string=%%new_string%%%%repstr%%"
) else (
call set "new_string=%%new_string%%%%b"
)
)
set new_string
goto :eof
pause
output:
first test
old_string=qwer
environnement variable str2rep is not defined.
environnement variable repstr is not defined.
new_string=qwer
second test
old_string=qwery
str2rep=w
repstr=t
new_string=qtery
in both test the exclamation mark will vanish
in the first test nothing will be replaced because the character to replace was an exclamation mark
and in the second test replacement is done but no exclamation mark is preserved
Last edited by allal (23 May 2011 18:32)
Offline
You loose the "!" in the line
for %%R IN (%*) do (
You can access it only without delayed expansion.
@echo off
call :cmdstring result "abc!ef"
setlocal EnableDelayedExpansion
echo !result!
goto :eof
:cmdstring
set "string=%~2"
setlocal EnableDelayedExpansion
set "newString=!string:~-3!-!string:~0,3!
(
endlocal
set "%~1=%newString%"
goto :eof
)
jeb
Offline
well done jeb
Offline
i think that is a mistake to make var expansion using ! or % or any character
sooner or later you will face a percent sign,exclamation,ampersand and the result is an error or a character vanishement.
actually a var don't need characters to be expanded, this method really make things complex
and it is not needed at all and it will be very great if this method was not implemented.
like any other language var expansion is just writing var without quotes
example:
REM declare and assign a value to a variable
SET var="st!r%in&g"
REM expand the variable
echo var
REM don't expand the variable
echo "var"
echo 'var'
echo ^var
output :
REM expand the variable
"st!r%in&g"
REM don't expand the variable
"var"
'var'
^var
they could use enabledelayedexpansion
then variable expansion will be delayed
using only the variable but without any extra useless character
believe it or not it will be very flexible and easy if this unwanted method doesn't exist
Offline
i think a character must instead be used to identify a variable
for you to know that is a variable
for a user to quickly spot a variable in huge line of code or huge text
not to be used for expansion
example :
!var=value
or
%var=125
or
like the most language
$var="string"
Offline