batch-file
2015-06-30 109 views 3 likes 
3

我試圖用enabledelayedexpansion讀取文件enabledelayedexpansion .File包含enabledelayedexpansion的特殊character.Because!之後的文本將被忽略。請提出一些解決此問題的方法。 注意: - 我必須使用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!" 

以上僅僅是腳本的一部分。文本文件將包含文件path.rest所有替代工作正常,只有在情況下!其造成的問題

+0

編輯。希望這給你的問題的答案 – ABC

回答

3

爲了從文件中讀取內容,不破壞它,你需要使用延遲反覆TECHNIC或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 
+0

不一起工作!..我從文件中讀取數據。請讓我知道是否有其他的變化有許多工作要做 – ABC

+0

如果它不與'工作!'?在'FOR ...(!textfile!)'部分或在哪裏? – jeb

+0

文本文件包含!字符串: - <! - 可選: - > – ABC

0

編輯:完全覆蓋。我既可以保持一個!感嘆號在輸出或通過^^!(或^^^^!^^^^^^!等)甚至數目的^^插入記號取代,如下所示:

@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; 
+0

文件有很少**!**標記。在這些情況下,它仍然失敗 – ABC

+0

這不能工作,因爲驚歎號丟失在行'set「行= %%我「' – jeb

+0

任何方式來處理它? – ABC

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個空行在一起(即可以容易地增加保護定義一組替代的如果需要更多線路)。

相關問題