标题: [文本处理] 请教为何批处理设置的变量使用IF框起来后就不正确了? [打印本页]
作者: diablozhao 时间: 2021-11-24 11:24 标题: 请教为何批处理设置的变量使用IF框起来后就不正确了?
两个批处理主要是下面【等号】内的区别
1.这是符合我预期结果的代码,等号内的结果正确显示。- @echo off
- set egg=6
- set C_PATH=c:\KL500\C
- set D_PATH=d:\kl300\D
- echo cpath=%C_PATH%
- echo dpath=%D_PATH%
- if "%egg%"=="6" CALL :DZPATH "%C_PATH%" "%D_PATH%"
- echo =====================================
- set aaa=%dzdk1%
- echo aaa=%aaa%
- set bbb=%dzdk2%
- echo bbb=%bbb%
- set CEP=%dzdk1%%dzp1%
- set DGP=%dzdk2%%dzp2%
- echo CEP=%CEP%
- echo DGP=%DGP%
- echo =====================================
- pause
-
- :END
- goto:eof
-
- :DZPATH
- rem path1
- set dzdk1=%~d1
- set dzp1=%~p1
- set dzfn1=%~n1
- set dzfx1=%~x1
- rem path2
- set dzdk2=%~d2
- set dzp2=%~p2
- set dzfn2=%~n2
- set dzfx2=%~x2
- rem path3
- set dzdk3=%~d3
- set dzp3=%~p3
- set dzfn3=%~n3
- set dzfx3=%~x3
- rem path4
- set dzdk4=%~d4
- set dzp4=%~p4
- set dzfn4=%~n4
- set dzfx4=%~x4
- goto:eof
复制代码
显示的结果是
=====================================
aaa=c:
bbb=d:
CEP=c:\KL500\
DGP=d:\kl300\
=====================================
2.这个我多加了个 IF 把等号内的内容框起来后,echo显示的结果就不对了。
请教为什么会这样?是格式写错了吗?- @echo off
- set egg=6
- set C_PATH=c:\KL500\C
- set D_PATH=d:\kl300\D
- echo cpath=%C_PATH%
- echo dpath=%D_PATH%
- if "%egg%"=="6" CALL :DZPATH "%C_PATH%" "%D_PATH%"
- echo =====================================
- if "%egg%"=="6" (
- set aaa=%dzdk1%
- echo aaa=%aaa%
- set bbb=%dzdk2%
- echo bbb=%bbb%
- set CEP=%dzdk1%%dzp1%
- set DGP=%dzdk2%%dzp2%
- echo CEP=%CEP%
- echo DGP=%DGP%
- )
- echo =====================================
-
- pause
-
- :END
- goto:eof
-
- :DZPATH
- rem path1
- set dzdk1=%~d1
- set dzp1=%~p1
- set dzfn1=%~n1
- set dzfx1=%~x1
- rem path2
- set dzdk2=%~d2
- set dzp2=%~p2
- set dzfn2=%~n2
- set dzfx2=%~x2
- rem path3
- set dzdk3=%~d3
- set dzp3=%~p3
- set dzfn3=%~n3
- set dzfx3=%~x3
- rem path4
- set dzdk4=%~d4
- set dzp4=%~p4
- set dzfn4=%~n4
- set dzfx4=%~x4
- 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 |