如何处理 !使用批量的enabledelayedexpansion

时间:2015-06-30 05:32:09

标签: batch-file

我正在尝试使用enabledelayedexpansion读取文件.File包含特殊字符。因为enabledelayedexpansion!之后的文本被忽略。请提出一些解决此问题的方法。 注意: - 我必须使用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!"

以上只是脚本的一部分。 textfile将包含文件path.rest所有替换只在以下情况下正常工作!它引起的问题

3 个答案:

答案 0 :(得分:3)

要在不破坏文件的情况下从文件中读取内容,您需要使用延迟切换技术或使用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

toggeling是必要的,因为FOR循环变量的扩展仅在禁用的延迟扩展模式下是安全的,否则删除/解释感叹号。

答案 1 :(得分:1)

通过SET /P读取文件是避免延迟扩展相关问题的另一种方法,可能比FOR /F更简单。我还向您展示了一种通过数组

定义替换集的简单方法
@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

:ProcessFile子程序中最复杂的部分仅用于将最多2个空行保存在一起(如果需要,可以很容易地增加到更多行)。

答案 2 :(得分:0)

修改:完全覆盖。我可以在输出中保留!感叹号,或者用{em>偶数的数字替换^^!(或^^^^!^^^^^^!等) ^^注意如下:

@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%"

<强>输出

==>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;