logo
Tags down

shadow

Why aren't variable names surrounded by exclamation marks outputting values even after enabling delayed expansion?


By : be9616
Date : September 16 2020, 07:00 PM
Does that help You never tell the %var1% variable to expand, so it doesn't. Let's trace this out:
code :


Share : facebook icon twitter icon

Windows batch - Delayed expansion removes exclamation mark


By : J narh
Date : March 29 2020, 07:55 AM
around this issue I wrote batch script to copy CSS files over to a certain directory and the problem is exclamation marks are removed when I open the CSS files in the destination directory. , I'd put it here were I you...
code :
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "Batch_tmp\%fileName%_tmp.css"') do (
    SETLOCAL DISABLEDELAYEDEXPANSION
    if %%a lss %start% echo(%%b
    if %%a gtr %end% echo(%%b
    ENDLOCAL
 )) > Batch_tmp\!fileName!.css
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:: force directories, start and end
SET runtime_dest=c:\temp
SET filename=test
SET start=2
SET end=4

:: create batch_temp & clear

MD "%cd%\Batch_tmp" 2>NUL >NUL
DEL "%cd%\Batch_tmp\*?*" /F /Q

:: make copy of original .css
xcopy "%RUNTIME_DEST%\%filename%.css" "%cd%\Batch_tmp" /y /z /r

echo creating tmp copy...
rem Rename the file so we can modify and create finalized version
ren "Batch_tmp\%fileName%.css" "%fileName%_tmp.css"

echo coping all line except section...
rem Copy all lines, except the ones in start-end section
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "Batch_tmp\%fileName%_tmp.css"') do (
    if %%a lss %start% echo(%%b
    if %%a gtr %end% echo(%%b
 )) > Batch_tmp\!fileName!.css
rem Copy all lines, except the ones in start-end section
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "Batch_tmp\%fileName%_tmp.css"') do (
SETLOCAL disabledelayedexpansion
    if %%a lss %start% echo(%%b
    if %%a gtr %end% echo(%%b
ENDLOCAL
 )) > Batch_tmp\!fileName!WITHSETLOCAL_ENDLOCAL.css

ECHO ============================
ECHO Batch_tmp\%fileName%_tmp.css
TYPE Batch_tmp\%fileName%_tmp.css
ECHO =======^^^^^^^^=================this is the original file
ECHO Batch_tmp\%fileName%.css
TYPE Batch_tmp\%fileName%.css
ECHO =======^^^^^^^^=================this is the result with delayed expansion enabled
ECHO Batch_tmp\%fileName%WITHSETLOCAL_ENDLOCAL.css
TYPE Batch_tmp\%fileName%WITHSETLOCAL_ENDLOCAL.css
ECHO ========^^^^^^^^================this is the result with the advised setlocal/ENDLOCAL IN place
GOTO :eof
C:\temp\test.css
1 File(s) copied
creating tmp copy...
coping all line except section...
============================
Batch_tmp\test_tmp.css
.foo
omit me
and me
and me
{
   color: pink !important;
}
=======^^^^=================this is the original file
Batch_tmp\test.css
.foo
{
   color: pink important;
}
=======^^^^=================this is the result with delayed expansion enabled
Batch_tmp\testWITHSETLOCAL_ENDLOCAL.css
.foo
{
   color: pink !important;
}
========^^^^================this is the result with the advised setlocal/ENDLOCAL IN place

Windows batchfiles - Using exclamation marks within variable values without disabling delayed expansion


By : reverb
Date : March 29 2020, 07:55 AM
wish help you to fix your issue There are multiple bugs in your edited code.
You are using findstr/n, this prefixes each line with a line number and a colon.
code :
for /f "delims=: tokens=1,*" %%A in ("!string!") do set "string=%%B"
set "string=!string:*:=!" Remove all up to the first colon
setlocal disabledelayedexpansion
for /f "tokens=* delims=" %%A in ('"findstr /n ^^ %INTEXTFILE%"') do (
  SET "string=%%A"
  setlocal EnableDelayedExpansion
  set "string=!string:*:=!"
  if defined string (
    SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!
  )
    (echo(!modified!) >> %OUTTEXTFILE%
  endlocal
)
endlocal
(echo Sample)    >> myOutputFile.txt
set "var1=/?"
set "var2=    "
set "var3=ON"
echo %var1%
echo %var2%
echo %var3%

How to avoid delayed expansion removing the exclamation marks in file names?


By : user1084141
Date : March 29 2020, 07:55 AM
may help you . How to avoid delayed expansion removing the exclamation marks in file names? , As Mofi suggested, you could disable delayed expansion and use
code :
call set filesList=%%filesList%%"%%~nxB"
setlocal DisableDelayedExpansion
for /F "delims=" %%B in ('dir %%A^* /a-d /b') do  ( 
    set "line=%%B"

    setlocal EnableDelayedExpansion
    REM *** Process the content of line here
    echo !line!
    endlocal
)

Escaping exclamation marks with delayed expansion


By : gutty19
Date : March 29 2020, 07:55 AM
this one helps.
To avoid loss of exclamation marks, enable delayed expansion only when it is really needed and expand normal % variables and for meta-variables only when delayed expansion is disabled:
code :
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "rootname=Common Filename"

set "replace="old">"
set "replaced="new">"

set "source=%rootname%_unqiue_suffix.txt"
set "target=Fixed\%source%"

(
    for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%source%"') do (
        set "line=%%b"
        setlocal EnableDelayedExpansion
        if defined line set "line=!line:%replace%=%replaced%!"
        echo(!line!
        endlocal
    )
) > "%target%"

endlocal

Delayed expansion and exclamation marks in strings


By : Sagar Jois
Date : March 29 2020, 07:55 AM
Does that help The problem is the parameter expansion in set filename=%%O. In %%O is still the exclamation mark, but when delayed expansion is enabled, the bangs are dropped.
The conclusion is simple, delayed expansion have to be disabled when you expand a FOR parameter.
Related Posts Related Posts :
shadow
Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk