I have a script that has a lot of use of the SETLOCAL ENABLEDELAYEDEXPANSION command, so I start the script off that way (less headaches). However, it does not allow you to use the ! character without escaping each instance of it (and I want to create a long line of !s for an error logging section =D ) and I don't want to escape every one of them.
Is there a way to temporarily break out of SETLOCAL, then reenter it keeping all previously created variables within the original SETLOCAL?
For example:
SETLOCAL ENABLEDELAYEDEXPANSION
set var=HELLO
ECHO %var%
ENDLOCAL
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO %var%
The 2nd ECHO will not give you the previous value of var
EDIT: ^ will not allow you to escape the ! inside SETLOCAL ENABLEDELAYEDEXPANSION
setlocal DisableDelayedExpansion
set var=Value! with! many! Bangs!
setlocal EnableDelayedExpansion
echo !var!
You can nest it like Aacini shows it.
Or you can use the return technic or escape ! inside a EnableDelayed block with ^^!
Setlocal EnableDelayedExpansion
echo 1^^!^^!^^!^^!^^!^^!^^!
REM *** Or use a self removing quote
echo !="! 2^!^!^!^!^!^!
Setlocal DisableDelayedExpansion
echo 3 !!!!!!!!
set var=Hello
(
endlocal
rem Bring the value behind the endlocal barrier
set var=%var%
)
echo var is still there, %var%
The return technic can also be used for exclamation marks, but then it is a bit more complex.
It can be found at Make an environment variable survive ENDLOCAL
Related
So I know how to do simple string replacing with the SET command.
Example:
set /p a=
set a=%a:<=^<%
echo %a%
(This example will change the prompted variable to the same thing but with the < character to be ^< to be be properly echoed if needed to)
I want to do the same thing but with the % character but I can't get it to work.
#ECHO OFF
SETLOCAL
SET "something=%%he%%llo%%"
ECHO %something%
ECHO ============
SETLOCAL ENABLEDELAYEDEXPANSION
SET "anotherthing=!something:%%=x!"
endlocal&SET "anotherthing=%anotherthing%"
ECHO %something%
ECHO %anotherthing%
GOTO :EOF
Like this, you mean?
There was nothing wrong with your method, short of using doublequotes to protect from poison characters.
The main issue now, is in trying to see the content, because you'd be Echoing poison characters. To see it actually in place I included two methods, a Set command to list all variables beginning with a, (with a findstr thrown in to isolate only the one named a), and by using delayed expansion, which prevents the variable from being expanded when the command line is read/parsed. At the very bottom I included a method of showing it with the Echo command without using delayed expansion or doublequotes, as you can see to escape a caret, ^, you'll need in this case to use two more carets
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "a="
Set /P "a="
Set "a=%a:<=^<%"
Echo(%a%
Pause
(Set a) 2> NUL | %SystemRoot%\System32\findstr.exe /B /L /I "a="
Pause
SetLocal EnableDelayedExpansion
Echo(!a!
EndLocal
Pause
Echo(%a:^<=^^^<%
Pause
The full (known) rules for variable replacement can be found at SO:
Percent Expansion Rules
In short:
The search expression in a percent replacement expression can not start with a percent.
The search expression in a delayed replacement expression can not start with a exclamation mark.
But you can replace percent signs with delayed expression
setlocal EnableDelayedExpansion
set /p var=
set "var=!var:%%=REPLACE!"
echo(!var!
The percent sign itself has to be doubled here, because it collapse to a single percent in phase2 (percent expansion)
I have written one batch script, to get the all pdf files in the directory including Subfolders, but i want to know is it possible to assign the value of %%x in the some other variable, like set temp = %%~na.
#echo off
setlocal enabledelayedexpansion
setlocal
for /r %%a in (*.pdf) do (
echo %%~na
)
endlocal
Almost exactly as you had it.
#echo off
setlocal enabledelayedexpansion
for /R %%a in (*.pdf) do (
set "var=%%~na"
echo !var!
)
You do not have to endlocal as it will end when the script completes in this case.
Doing setlocal a second time is an issue. You did setlocal at the beginning when you enabled delayedexpansion but that being said, you never used it. Look at setlocal /? you will notice ! is used instead of % in order to tell the system which variables should be used in the delayed environment expansion.
I'm trying to rename my files to remove any characters that cause problems in scripts. This works well for ampersand and exclamation point but when the file has the percent sign it doesn't show up in the variable to begin with. How do I pass files with special characters via for loop?
for %%v in (*) do call :validate "%%v"
exit /b
:validate
set "original=%~nx1"
set "newtitle=%original:!=%"
set "newtitle=%newtitle:&=and%"
setlocal enabledelayedexpansion
set "newtitle=!newtitle:%%= percent!"
if not "%newtitle%"=="%original%" ren %1 "%newtitle%"
Your problem is the line for %%v in (*) do call :validate "%%v", because thecall` starts the parser a second time and there all percents are evaluated a second time.
You should save the value into a variable and access these variable in your function instead.
setlocal DisableDelayedExpansion
for %%v in (*) do (
set "filename=%%~v"
call :validate
)
exit /b
:validate
setlocal EnableDelayedExpansion
set "original=!filename!"
set "newtitle=%original:!=exclam%"
set "newtitle=!newtitle:&=and!"
set "newtitle=!newtitle:%=percent!"
A possible way is to use delayed expansion:
set "newfilename=%filename:&=and%"
setlocal EnableDelayedExpansion
set "newfilename=!newfilename:%%= percent!"
endlocal
The %-sign must be escaped by doubling it in a batch file.
Note that the variable is no longer available beyond the endlocal command.
I used the quoted syntax, which is the safest way to define variables, even with white-spaces and special characters.
If I have a batch file and I am setting arrays with an index that is a variable
#echo off
SET x=1
SET myVar[%x%]=happy
How do I echo that to get "happy" ?
I've tried
ECHO %myVar[%x%]%
ECHO %%myVar[%x%]%%
ECHO myVar[%x%]
But none of them work.
It works fine if I use the actual number for the index
ECHO %myVar[1]%
But not if the index number is also a variable
SET x=1
SET myVar[%x%]=happy
call echo %%myvar[%x%]%%
set myvar[%x%]
for /f "tokens=2* delims==" %%v in ('set myvar[%x%]') do #echo %%v
setlocal enableDelayedExpansion
echo !myvar[%x%]!
endlocal
I would recommend you to use
setlocal enableDelayedExpansion
echo !myvar[%x%]!
endlocal
as it is a best performing way
There is a special ! character in batch to deal with your situation. Use echo !myVar[%x%]!, from How to return an element of an array in Batch?. ! means delayed expansion. The variable myVar will not get expanded until after %x% is, yielding the expression you want.
one way you can do this is to use
call echo %%myVar[%x%]%%
call lets you put variables in places where they wouldn't normally work, if you use the double percents
When I have setlocal ENABLEDELAYEDEXPANSION set in a cmd script is there any way I can escape a ! that I want to use as a parameter to a command?
#echo off
setlocal ENABLEDELAYEDEXPANSION
echo I want to go out with a bang!
echo I still want to go out with a bang^!
That's what I found (^^)
#echo off
setlocal ENABLEDELAYEDEXPANSION
echo I want to go out with a bang^^!
An additional remark to the answer of FrVaBe.
Normally the ^^! works, but in quotes you only need ^! instead.
echo I want to go out with a bang^^!
echo He said "Bang^!"
This is a result of the escape mechanism of the batch parser.
First the parser parses a line and the caret escapes the next character, in this case it has an effect for &|<>()"<linefeed>, but only outside of quotes, as inside of the quotes all characters are "normal" and the caret itself has no effect.
With delayed expansion an extra parse step follows, there is the caret also an escape character for the next character, but only affects the ! and ^, and quotes are ignored in this parsing step.
This extra step will be executed only, if there is at least one ! in the line.
setlocal DisableDelayedExpansion
echo DisableDelayedExpansion
echo one caret^^
echo one caret^^ bang! "boom^!"
echo(
setlocal EnableDelayedExpansion
echo EnableDelayedExpansion
echo one caret^^
echo none caret^^ bang^^! "boom^!"
---- OUTPUT ------
DisableDelayedExpansion
one caret^
one caret^ bang! "boom^!"
EnableDelayedExpansion
one caret^
none caret bang! "boom!"
EDIT
Here is a slightly modified example that better illustrates the various escape permutations that are required, depending on the context. The only case that requires unusual escaping is the last example when delayed expansion is on and there exists at least one ! on the line.
#echo off
setlocal DisableDelayedExpansion
echo DisableDelayedExpansion
echo caret^^ "caret^"
echo caret^^ bang! "caret^ bang!"
echo(
setlocal EnableDelayedExpansion
echo EnableDelayedExpansion
echo caret^^ "caret^"
echo caret^^^^ bang^^! "caret^^ bang^!"
-- OUTPUT --
DisableDelayedExpansion
caret^ "caret^"
caret^ bang! "caret^ bang!"
EnableDelayedExpansion
caret^ "caret^"
caret^ bang! "caret^ bang!"
To use an explanation point in batch with Delayed Expansion enabled, you must first add the explanation point to a variable with it disabled. See the below example with both DISABLEDELAYEDEXPANSION and ENABLEDELAYEDEXPANSION state.
#echo off
setlocal DISABLEDELAYEDEXPANSION
set DB_password=encrypt!Pws
echo %DB_password%
SETLOCAL ENABLEDELAYEDEXPANSION
echo !DB_password!
Thanks. To add to this valuable point, if one's script contains a variable whose value contains an "!", then the following approach will render that value as-is:
#echo off
SETLOCAL EnableDelayedExpansion
set /P omg=Enter a value that contains an exclamation-point:
echo Traditional: %omg%
echo Alternative: !omg!
pause