lxmxn『第 6 楼』的代码挺不错的,但是有以下缺憾:
1、因为启用了变量延迟功能,所以,会把文本中的感叹号置换为空;
2、因为普通for语句忽略以分号打头的行,所以此代码将不能提取这些行的内容;
3、if 语句中的 :end 并不能被其他语句作为标签来调用,也就是说,其他语句中的goto end部分执行的时候会出错,并不能跳转到这里;
4、SET A="%1" 中的引号是多余的,因为在call的时候,%1已经被强行赋予了引号,因此会导致 if "%A%"=="" goto end 一句始终得不到执行;
在这个代码的基础上,得到如下代码:
版本一:
@echo off
:: 把半角双引号换成空格,把等号换成〓,兼容其他特殊字符
:: 能处理以分号打头的行,但是会忽略空行
:: 能兼容unicode格式的文本
:: 每行之间空一行显示
for /f "delims= eol=" %%i in ('type %1') do (
set "str=%%i"
call set "str=%%str:"= %%"
call :pickup
)
pause
goto :eof
:pickup
ping -n 1 127.1>nul
if "%str:~0,1%"=="=" set "str=〓%str:~1%"
set /p "=%str:~0,1%"<nul
set "str=%str:~1%"
if defined str goto pickup
echo.&echo.
goto :eof
版本二:
@echo off
:: 与版本一的几点区别:
:: 1、不能处理unicode文本
:: 2、每行之间不再强行加上空行,并且能原样显示空行
for /f "delims= eol=" %%i in ('findstr /n .* %1') do (
set "str=%%i"
call set "str=%%str:"= %%"
call set "str=%%str:*:=%%"
call :pickup
)
echo.
pause
goto :eof
:pickup
if not defined str echo.&goto :eof
if "%str:~0,1%"=="=" set "str=〓%str:~1%"
set /p "=%str:~0,1%"<nul
ping -n 1 127.1>nul
set "str=%str:~1%"
if defined str goto pickup
echo.
goto :eof
Last edited by namejm on 2007-2-2 at 02:06 PM ]
lxmxn's post on floor 6 has a good code, but there are the following drawbacks:
1. Because the variable delay function is enabled, it will replace the exclamation marks in the text with empty;
2. Because the ordinary for statement ignores the lines starting with semicolons, this code will not be able to extract the content of these lines;
3. The :end in the if statement cannot be called as a label by other statements, that is, when the goto end part in other statements is executed, an error will occur and it cannot jump here;
4. The quotes in SET A="%1" are redundant, because when calling, %1 has been forcibly given quotes, so the sentence if "%A%"=="" goto end will never be executed;
Based on this code, the following code is obtained:
Version 1:
@echo off
:: Replace half-width double quotes with spaces, replace equal signs with 〓, compatible with other special characters
:: Can handle lines starting with semicolons, but will ignore blank lines
:: Can be compatible with unicode - formatted text
:: Display with a blank line between each line
for /f "delims= eol=" %%i in ('type %1') do (
set "str=%%i"
call set "str=%%str:"= %%"
call :pickup
)
pause
goto :eof
:pickup
ping -n 1 127.1>nul
if "%str:~0,1%"=="=" set "str=〓%str:~1%"
set /p "=%str:~0,1%"<nul
set "str=%str:~1%"
if defined str goto pickup
echo.&echo.
goto :eof
Version 2:
@echo off
:: Several differences from version 1:
:: 1. Cannot handle unicode text
:: 2. No longer forcibly add blank lines between each line, and can display blank lines as they are
for /f "delims= eol=" %%i in ('findstr /n .* %1') do (
set "str=%%i"
call set "str=%%str:"= %%"
call set "str=%%str:*:=%%"
call :pickup
)
echo.
pause
goto :eof
:pickup
if not defined str echo.&goto :eof
if "%str:~0,1%"=="=" set "str=〓%str:~1%"
set /p "=%str:~0,1%"<nul
ping -n 1 127.1>nul
set "str=%str:~1%"
if defined str goto pickup
echo.
goto :eof
Last edited by namejm on 2007 - 2 - 2 at 02:06 PM ]