读取:特殊字符转义成普通字符序列;
readline.bat
- @echo off
- setlocal disabledelayedexpansion
- for /f "delims=#" %%. in (
- 'prompt #$E# ^& echo on ^& for %%_ in ^( . ^) do rem'
- ) do (
- set "_Esc=%%."
- )
- set _In= & set /p "_In="
- for /f "delims=" %%. in ("%_Esc%") do (
- if defined _In (
- call set "_In=%%_In:"=%%.D%%"
- call set "_In=%%_In:!=%%.E%%"
- setlocal ENABLEDELAYEDEXPANSION
- (
- set "_In=!_In:^=%%.C!"
- set "_In2="
- :READLINE_Replace
- if defined _In (
- if "!_In:~,1!" == "%%" (
- set "_In2=!_In2!%_Esc%P"
- ) else (
- set "_In2=!_In2!!_In:~,1!"
- )
- set "_In=!_In:~1!"
- goto READLINE_Replace
- )
- :READLINE_Replace2
- if defined _In2 (
- if "!_In2:~,1!" == "%_Esc%" (
- set "_In3=!_In3!$"
- ) else if "!_In2:~,1!" == "$" (
- set "_In3=!_In3!$$"
- ) else if "!_In2:~,1!" == ":" (
- set "_In3=!_In3!$A"
- ) else (
- set "_In3=!_In3!!_In2:~,1!"
- )
- set "_In2=!_In2:~1!"
- goto READLINE_Replace2
- )
- echo.!_In3!
- )
- endlocal
- )
- )
复制代码
写入:把转义字符序列替换回特殊字符;
writeall.cmd
- @echo off & setlocal ENABLEDELAYEDEXPANSION
- for /f "delims=" %%i in ('more') do (
- set "_Out=%%~i"
- set _OutBuf=
- :WRITEALL_Loop
- if "!_Out:~,2!" == "$$" (
- set "_OutBuf=!_OutBuf!$"
- set "_Out=!_Out:~2!"
- goto WRITEALL_Loop
- ) else if "!_Out:~,2!" == "$E" (
- set "_OutBuf=!_OutBuf!^!"
- set "_Out=!_Out:~2!"
- goto WRITEALL_Loop
- ) else if "!_Out:~,2!" == "$C" (
- set "_OutBuf=!_OutBuf!^^"
- set "_Out=!_Out:~2!"
- goto WRITEALL_Loop
- ) else if "!_Out:~,2!" == "$D" (
- set "_OutBuf=!_OutBuf!^""
- set "_Out=!_Out:~2!"
- goto WRITEALL_Loop
- ) else if "!_Out:~,1!" == "=" (
- set "_OutBuf=!_OutBuf!="
- set "_Out=!_Out:~1!"
- goto WRITEALL_Loop
- ) else if "!_Out:~,1!" == " " (
- set "_OutBuf=!_OutBuf! "
- set "_Out=!_Out:~1!"
- goto WRITEALL_Loop
- ) else if "!_Out:~,2!" == "$P" (
- set "_OutBuf=!_OutBuf!%%"
- set "_Out=!_Out:~2!"
- goto WRITEALL_Loop
- ) else if "!_Out:~,2!" == "$A" (
- set "_OutBuf=!_OutBuf!:"
- set "_Out=!_Out:~2!"
- goto WRITEALL_Loop
- ) else if defined _Out (
- set "_OutBuf=!_OutBuf!!_Out:~,1!"
- set "_Out=!_Out:~1!"
- goto WRITEALL_Loop
- )
- echo.!_OutBuf!
- )
复制代码
这两个必须写成单独的bat调用,不能直接call标号(setlocal以及一些环境问题)。
做了一定测试,应该没问题(希望别打脸)。 |