How to apply! with allowed extension in batch mode

I am trying to read a file using enabledelayedexpansion.File contains a special character. Due to enableelayedexpansion! and the text after that is ignored. Please suggest some way to solve this problem. note: - I need to use enabledelayedexpansion.

`

 @echo off 
set "search1='"
set "search2=""
set "search3=&"
set "search4=<"
set "search5=>"
set "search6=!"
set "replace1=&apos;"
set "replace2=&quot;"
set "replace3=&amp;"
set "replace4=&lt;"
set "replace5=&gt;"
set "replace6=^!"
setlocal enabledelayedexpansion 
set "textfile=!input!"
set "newfile=!input!1"
echo !textfile!
echo !newfile!
break>"!newfile!" 


(for /f  "delims=" %%i in (!textfile!) do (

    setlocal enabledelayedexpansion
        set "line=%%i"

    for /f "usebackq tokens=1,2 delims=$" %%a in ('"!search3!"$"!replace3!"') do set "line=!line:%%~a=%%~b!"
    for /f "usebackq tokens=1,2 delims=$" %%a in ('"!search4!"$"!replace4!"') do set "line=!line:%%~a=%%~b!"
    for /f "usebackq tokens=1,2 delims=$" %%a in ('"!search5!"$"!replace5!"') do set "line=!line:%%~a=%%~b!"
   for /f "usebackq tokens=1,2 delims=$" %%a in ('"!search2!"$"!replace2!"') do set "line=!line:%%~a=%%~b!"
  for /f "usebackq tokens=1,2 delims=$" %%a in ('"!search1!"$"!replace1!"') do set "line=!line:%%~a=%%~b!"


  setlocal enabledelayedexpansion
        echo(!line!
    endlocal
    ))>>"!newfile!"

      

The above is just part of the script. textfile will contain file path.rest, all substitutions work fine only if! its causing problem

+3


source to share


3 answers


To read the content from a file without destroying it, you need to use the delayed switch technique, or read it with set /p

.



setlocal DisableDelayedExpansion
(
  for /f "delims=" %%i in (textfile.txt) do (
    set "line=%%i"  -- This must be done while delayed expansion is disabled
    setlocal EnableDelayedExpansion
    set "line=!line:<=&lt;!"
    echo(!line!
    endlocal
  )
) > outfile.txt

      

+3


source


Reading a file with SET /P

is another way to avoid extension lag issues, which can be easier than FOR /F

. I am also showing you an easier way to define a set of overrides using an array

@echo off
setlocal EnableDelayedExpansion

rem Define the set of replacements
set "replacA[&]=&amp;"
set "replace[']=&apos;"
set replace["]=&quot;
set "replace[<]=&lt;"
set "replace[>]=&gt;"
REM set "replace[!]=^!"   // Not needed

set "textfile=!input!"
set "newfile=!input!1"
echo !textfile!
echo !newfile!

call :ProcessFile < "!textfile!" > "!newfile!"
goto :EOF


:ProcessFile
set empty=0
set "line="
:emptyLine
set /P line=

if not defined line (
   set /A empty+=1
   if !empty! gtr 2 exit /B
   echo/
   goto emptyLine
)

for /F "tokens=2,3 delims=[]=" %%a in ('set replac') do set "line=!line:%%a=%%b!"
echo !line!
goto ProcessFile

      



The hardest part in the subroutine :ProcessFile

is only used to store a maximum of two blank lines (which can easily be expanded to more lines if needed).

+1


source


Edit : completely rewritten. I can either keep the exclamation mark !

in the output, or replace it with ^^!

(or ^^^^!

, ^^^^^^!

etc.) with an even number ^^

:

@ECHO OFF >NUL
SETLOCAL enableextensions enabledelayedexpansion
set "textfile=files\31130282textfile.txt"
set "newfile=files\31130282textfileNew.txt"
set "line="
set "quote=""
setlocal DisableDelayedExpansion
  set "exclamation=!"
  set "exclamationNew=%~1!"
  (for /f  "delims=" %%i in (%textfile%) do (
    set "line=%%i"
    SETLOCAL enabledelayedexpansion
      set "line=!line:&=&amp;!"
      set "line=!line:<=&lt;!"
      set "line=!line:>=&gt;!"
      call set "line=%%line:!quote!=&quot;%%"
      set "line=!line:'=&apos;!"
      call set "line=%%line:!exclamation!=!exclamationNew!%%"
      echo(!line!
    ENDLOCAL
  ))>"%newfile%"
endlocal
type "%newfile%"

      

Output

==>type files\31130282textfile.txt"
<'abc'>&"cde"T
"<x!y!z!>"

==>D:\bat\SO\31130282.bat ""
&lt;&apos;abc&apos;&gt;&amp;&quot;cde&quot;T
&quot;&lt;x!y!z!&gt;&quot;

==>D:\bat\SO\31130282.bat "^"
&lt;&apos;abc&apos;&gt;&amp;&quot;cde&quot;T
&quot;&lt;x^^!y^^!z^^!&gt;&quot;

==>D:\bat\SO\31130282.bat "^^"
&lt;&apos;abc&apos;&gt;&amp;&quot;cde&quot;T
&quot;&lt;x^^^^!y^^^^!z^^^^!&gt;&quot;

      

0


source







All Articles