我正在尝试使用enabledelayedexpansion读取文件.File包含特殊字符。因为enabledelayedexpansion!之后的文本被忽略。请提出一些解决此问题的方法。 注意: - 我必须使用enabledelayedexpansion。
`
@echo off
set "search1='"
set "search2=""
set "search3=&"
set "search4=<"
set "search5=>"
set "search6=!"
set "replace1='"
set "replace2=""
set "replace3=&"
set "replace4=<"
set "replace5=>"
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所有替换只在以下情况下正常工作!它引起的问题
答案 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:<=<!"
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[&]=&"
set "replace[']='"
set replace["]="
set "replace[<]=<"
set "replace[>]=>"
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:&=&!"
set "line=!line:<=<!"
set "line=!line:>=>!"
call set "line=%%line:!quote!="%%"
set "line=!line:'='!"
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 ""
<'abc'>&"cde"T
"<x!y!z!>"
==>D:\bat\SO\31130282.bat "^"
<'abc'>&"cde"T
"<x^^!y^^!z^^!>"
==>D:\bat\SO\31130282.bat "^^"
<'abc'>&"cde"T
"<x^^^^!y^^^^!z^^^^!>"