[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
第2题解答
  1. @echo off
  2. :字符替换练习题
  3. echo 解答替换"他娘的"为"TND"
  4. set "a=你他娘的是谁?他娘的我不告诉你。"
  5. set "b=你他娘的到底是谁?我他娘的还就不告诉那个他娘的你。"
  6. echo %a:他娘的=tnd%&echo. &echo %b:他娘的=tnd%&echo 按任意键获取第2结果&pause>nul
  7. echo.
  8. echo %a:他娘的=%&echo. &echo %b:他娘的=%&echo 按任意键结束本题&pause>nul
复制代码

TOP

复杂啊。。有点不懂, 努力。。

TOP

@echo off
setlocal enabledelayedexpansion
for /f %%i in (1.txt) do (
set a=%%i
set b=!a:他娘的=TND!
echo !b!
)
pause

TOP

  1. @echo off&setlocal enabledelayedexpansion
  2. for /f %%a in (a.txt) do (
  3.     set str=%%a
  4.     set str=!str:他娘的=TND!
  5.     echo !str!
  6. )
  7. pause>nul
  8. for /f %%a in (a.txt) do (
  9.     set str=%%a
  10.     set str=!str:他娘的=!
  11.     echo !str!
  12. )
  13. pause>nul
复制代码
only the strong survive!

TOP

  1. @echo off&setlocal enabledelayedexpansion
  2. for /l %%i in (1 1 2) do (
  3. for /f %%a in (a.txt) do (
  4.      set /a n+=1
  5.      set k=%%a
  6.      set b=!k:~,1!
  7.      set c=TND
  8.      if "!n!"=="1" (
  9.         set d=!k:~4,3!
  10.         set e=!k:~-6!
  11.         if "%%i"=="1" (
  12.            echo !b!!c!!d!!c!!e!
  13.         ) else (
  14.            echo !b!!d!!e!
  15.         )
  16.      ) else (
  17.         set d=!k:~4,6!
  18.         set e=!k:~-12,7!
  19.         set f=!k:~-2!
  20.         if "%%i"=="1" (
  21.            echo !b!!c!!d!!c!!e!!c!!f!
  22.         ) else (
  23.            echo !b!!d!!e!!f!
  24.         )
  25.      )
  26. )
  27. pause>nul
  28. )
  29. pause>nul
复制代码
only the strong survive!

TOP

  1. @echo off
  2. setlocal EnableExtensions
  3. setlocal EnableDelayedExpansion
  4. for /f %%a in (bat02.txt) do (
  5.         set str=%%a
  6.         echo !str:他娘的=TND!
  7. )
复制代码

[ 本帖最后由 mac007cn 于 2010-12-27 14:46 编辑 ]

TOP

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /f %%i in (a.txt) do (
  4. set a=%%i
  5. set a=!a:他娘的=tnd!
  6. echo !a!
  7. )
  8. for /f %%i in (a.txt) do (
  9. set a=%%i
  10. set a=!a:他娘的=!
  11. echo !a!
  12. )
  13. endlocal
  14. pause
复制代码

TOP

  1. @echo off&setlocal EnableDelayedExpansion
  2. for %%i in (你他娘的是谁?他娘的我不告诉你。 你他娘的到底是谁?我他娘的还就不告诉那个他娘的你。) do (
  3. set var1=%%i
  4. set var2=!var1:他娘的=TND!
  5. echo !var2!
  6. set var2=!var1:他娘的=!
  7. echo !var2!
  8. )
  9. pause
复制代码

TOP

这样也可以

<1>@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%i in (a.txt) do (
set "var=%%i"
echo !var:他娘的=TND!)
pause
<2>@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%i in (a.txt) do (
set "var=%%i"
echo !var:他娘的=!)
pause

TOP

多谢defanive前辈指教,实现了换行的功能。

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set n=^
  4. for /f %%i in (a.txt) do (
  5.     set o=%%i
  6.     set "s1=!s1!!o:他娘的=TND!!n!"      
  7.     set "s2=!s1:TND=!"   
  8. )
  9. echo !s1!
  10. echo !s2!
  11. pause>nul
复制代码
不过原理不是很清楚只是依葫芦画瓢,所以希望知道原理的前辈给晚辈详细讲解一番,晚辈在此先谢过了。。。

TOP

不知道还能再精简吗?

  1. @echo off
  2. for /f %%i in (a.txt) do (
  3.     set o=%%i
  4.     setlocal enabledelayedexpansion
  5.     set "s1=!s1!!o:他娘的=TND!"      
  6.     set "s2=!s1:TND=!"   
  7. )
  8. echo !s1!
  9. echo !s2!
  10. pause>nul
复制代码
有点遗憾是没能实现换行显示!还请各位前辈赐教。。。

TOP

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "tokens=*" %%a in ( a.txt ) do (
set ti1=%%a
set "ti=!ti1:他娘的=TND!"
echo !ti! >>b.txt
)
del a.txt
ren b.txt a.txt
for /f "tokens=*" %%a in ( a.txt ) do (
set ti1=%%a
set "ti=!ti1:TND=!"
echo !ti! >>b.txt
)
del a.txt
ren b.txt a.txt

TOP

答:
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. echo =======替换版==========
  4. for /f %%a in (a.txt) do (
  5.    set a=%%a
  6.    set a=!a:他娘的=TND!
  7.    echo !a!
  8. )
  9. echo.
  10. echo =======去除版==========
  11. for /f %%a in (a.txt) do (
  12.    set a=%%a
  13.    set a=!a:他娘的=!
  14.    echo !a!
  15. )
  16. pause>nul
复制代码
效果:
[attach]2728[/attach]

======================
end;

TOP

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set "str="
  4. for /f "delims=" %%a in (a.txt) do (
  5.     set "str=!str!%%a"
  6. )
  7. echo !str!
  8. echo.
  9. echo 下面把"他娘的"换成TND。
  10. echo ------------------------------------------------------------------
  11. set "nstr=!str:他娘的=TND!"
  12. echo !nstr!
  13. echo ------------------------------------------------------------------
  14. echo.
  15. echo 下面把"他娘的"去掉。
  16. echo ------------------------------------------------------------------
  17. set "nstr1=!str:他娘的=!"
  18. echo !nstr1!
  19. echo ------------------------------------------------------------------
  20. pause
复制代码
好像写得有点繁,呵呵。
问个问题:批处理中有对文本进行进行换行的常量吗?如VB中有vbcrlf。
进步怎么这么慢呢?

TOP

@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (a.txt) do (
    set var=%%a
    set change=!var:他娘的=TND!
    echo !change!>>b.txt
   
)
for /f "tokens=*" %%b in (b.txt) do (
    set change=%%b
set str=!change:TND=!
    echo !str!
)
pause>nul

TOP

返回列表