Board logo

标题: [文本处理] 请教为何批处理设置的变量使用IF框起来后就不正确了? [打印本页]

作者: diablozhao    时间: 2021-11-24 11:24     标题: 请教为何批处理设置的变量使用IF框起来后就不正确了?

两个批处理主要是下面【等号】内的区别

1.这是符合我预期结果的代码,等号内的结果正确显示。
  1. @echo off
  2. set egg=6
  3. set C_PATH=c:\KL500\C
  4. set D_PATH=d:\kl300\D
  5. echo cpath=%C_PATH%
  6. echo dpath=%D_PATH%
  7. if "%egg%"=="6" CALL :DZPATH "%C_PATH%" "%D_PATH%"
  8. echo =====================================
  9. set aaa=%dzdk1%
  10. echo aaa=%aaa%
  11. set bbb=%dzdk2%
  12. echo bbb=%bbb%
  13. set CEP=%dzdk1%%dzp1%
  14. set DGP=%dzdk2%%dzp2%
  15. echo CEP=%CEP%
  16. echo DGP=%DGP%
  17. echo =====================================
  18. pause
  19. :END
  20. goto:eof
  21. :DZPATH
  22. rem path1
  23. set dzdk1=%~d1
  24. set dzp1=%~p1
  25. set dzfn1=%~n1
  26. set dzfx1=%~x1
  27. rem path2
  28. set dzdk2=%~d2
  29. set dzp2=%~p2
  30. set dzfn2=%~n2
  31. set dzfx2=%~x2
  32. rem path3
  33. set dzdk3=%~d3
  34. set dzp3=%~p3
  35. set dzfn3=%~n3
  36. set dzfx3=%~x3
  37. rem path4
  38. set dzdk4=%~d4
  39. set dzp4=%~p4
  40. set dzfn4=%~n4
  41. set dzfx4=%~x4
  42. goto:eof
复制代码
显示的结果是
=====================================
aaa=c:
bbb=d:
CEP=c:\KL500\
DGP=d:\kl300\
=====================================


2.这个我多加了个 IF 把等号内的内容框起来后,echo显示的结果就不对了。
请教为什么会这样?是格式写错了吗?
  1. @echo off
  2. set egg=6
  3. set C_PATH=c:\KL500\C
  4. set D_PATH=d:\kl300\D
  5. echo cpath=%C_PATH%
  6. echo dpath=%D_PATH%
  7. if "%egg%"=="6" CALL :DZPATH "%C_PATH%" "%D_PATH%"
  8. echo =====================================
  9. if "%egg%"=="6" (
  10. set aaa=%dzdk1%
  11. echo aaa=%aaa%
  12. set bbb=%dzdk2%
  13. echo bbb=%bbb%
  14. set CEP=%dzdk1%%dzp1%
  15. set DGP=%dzdk2%%dzp2%
  16. echo CEP=%CEP%
  17. echo DGP=%DGP%
  18. )
  19. echo =====================================
  20. pause
  21. :END
  22. goto:eof
  23. :DZPATH
  24. rem path1
  25. set dzdk1=%~d1
  26. set dzp1=%~p1
  27. set dzfn1=%~n1
  28. set dzfx1=%~x1
  29. rem path2
  30. set dzdk2=%~d2
  31. set dzp2=%~p2
  32. set dzfn2=%~n2
  33. set dzfx2=%~x2
  34. rem path3
  35. set dzdk3=%~d3
  36. set dzp3=%~p3
  37. set dzfn3=%~n3
  38. set dzfx3=%~x3
  39. rem path4
  40. set dzdk4=%~d4
  41. set dzp4=%~p4
  42. set dzfn4=%~n4
  43. set dzfx4=%~x4
  44. goto:eof
复制代码
显示的结果是:
=====================================
aaa=
bbb=
CEP=
DGP=
=====================================
作者: diablozhao    时间: 2021-11-24 11:36

原因找到了,要加延迟变量
作者: Batcher    时间: 2021-11-24 12:00

回复 2# diablozhao


是的,可以多研究一下:批处理中的变量延迟扩展、变量嵌套
http://bbs.bathome.net/thread-2899-1-1.html
作者: diablozhao    时间: 2021-11-25 09:38

回复 3# Batcher


    谢谢!




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2