CMD:中失败%〜D0时,受理报价批处理文件的名称 [英] CMD: failure of %~d0 when CALL quotes the name of the batch file

查看:215
本文介绍了CMD:中失败%〜D0时,受理报价批处理文件的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为何%〜D0以下未能返回该批处理文件的驱动器字母S:当受理报价批处理文件的名称?

 取值:\\ DJ DAP>的test.bat型
R:
%〜D0小号:\\ DJ DAP>调用test.bat的!小号:\\ DJ DAP> R!R:\\> S:小号:\\ DJ DAP>称之为test.bat的小号:\\ DJ DAP> R!R:\\> R:R:\\>

编辑从以下杰里和MC回应:这是显示相同的非呼叫例如:

  R:\\> S:小号:\\ DJ DAP>的test.bat型
R:
%〜D0小号:\\ DJ DAP> test.bat的!小号:\\ DJ DAP> R!R:\\> S:!小号:\\ DJ DAP>中的test.bat小号:\\ DJ DAP> R!R:\\> R:R:\\>


解决方案

编辑 - npocmaka,你是对的。奇怪。

原来的答复中删除 - 我错了。

但问题不在于呼叫命令。问题是报价和CMD。

经过测试,似乎更在cmd中是如何的文件名进行处理以及如何处理的API调用一些错误的错误/功能。

通过下面的批处理文件(TEST.CMD)

 关闭@echo
    SETLOCAL ENABLEEXTENSIONS    从呼应驱动器D调用子程序:
    拨打:的getInfo
    回声。    C:
    回声从驱动器C调用子程序:
    拨打:的getInfo
    回声。    回声直接获取数据,而不子程序:获取信息
    回声------------------------------------------------- --------
    回声CD:CD%%
    回声D0:%〜D0
    回声DP0:%〜DP0
    回声F0:%〜F0
    回声------------------------------------------------- --------
    回声。
    GOTO:EOF

放置在D:\\ TEMP \\ testCMD并在驱动器C的当前目录:在C:\\用户,在执行的结果是:

1:调用不带引号 CMD从目录: TEST.CMD

 从驱动器D调用子程序:
-------------------------------------------------- -------
CD:D:\\ TEMP \\ testCMD
D0:D:
DP0:D:\\ TEMP \\ testCMD \\
F0:D:\\ TEMP \\ testCMD \\ TEST.CMD
-------------------------------------------------- -------
调用从驱动器C子程序:
-------------------------------------------------- -------
CD:C:\\用户
D0:D:
DP0:D:\\ TEMP \\ testCMD \\
F0:D:\\ TEMP \\ testCMD \\ TEST.CMD
-------------------------------------------------- -------
直接获取数据,而不子程序
-------------------------------------------------- -------
CD:C:\\用户
D0:D:
DP0:D:\\ TEMP \\ testCMD \\
F0:D:\\ TEMP \\ testCMD \\ TEST.CMD
-------------------------------------------------- -------

结果:一切OK

2:与报价 CMD从目录中调用TEST.CMD(不,不需要通话命令)

 从驱动器D调用子程序:
-------------------------------------------------- -------
CD:D:\\ TEMP \\ testCMD
D0:D:
DP0:D:\\ TEMP \\ testCMD \\
F0:D:\\ TEMP \\ testCMD \\ TEST.CMD
-------------------------------------------------- -------
调用从驱动器C子程序:
-------------------------------------------------- -------
CD:C:\\用户
D0:D:
DP0:D:\\ TEMP \\ testCMD \\
F0:D:\\ TEMP \\ testCMD \\ TEST.CMD
-------------------------------------------------- -------
直接获取数据,而不子程序
-------------------------------------------------- -------
CD:C:\\用户
D0:C:
DP0:C:\\用户\\
F0:C:\\用户\\ TEST.CMD
-------------------------------------------------- -------

结果:失败来获取%正确的值〜D0从CMD的主执行行仅如果直接拿到。与子程序调用的工作方式相同的预期。

不带引号的所有测试工作方案无故障。随着报价,如果呼叫线路包括驱动器(EJ:D:\\ TEST.CMD)的正确检索到的所有值。如果不开车列入批调用(EJ:TEST.CMD与一批目录的路径,或\\ TEMP \\ testCMD \\测试。 CMD由D根:),检索到不正确的值,但只有在批处理文件执行的主线。子程序总是得到正确的值。

为什么呢?不知道。 \\用户\\ TEST.CMD C制备$ C>这是回答没有这样的文件 CMD,但忽略它并继续执行,显示了错误的价值观

所以,没有回答,不好意思。但我不得不文件这一点。在房间里的一些大师?

Why the following failure of %~d0 to return the batch file's drive letter S: when CALL quotes the name of the batch file?

S:\!DJ DAP>type test.bat
R:
%~d0

S:\!DJ DAP>call test.bat

S:\!DJ DAP>R:

R:\>S:

S:\!DJ DAP>call "test.bat"

S:\!DJ DAP>R:

R:\>R:

R:\>

EDIT following responses from Jerry and MC: Here's a non-CALL example showing the same:

R:\>s:

S:\!DJ DAP>type test.bat
R:
%~d0

S:\!DJ DAP>test.bat

S:\!DJ DAP>R:

R:\>S:

S:\!DJ DAP>"test.bat"

S:\!DJ DAP>R:

R:\>R:

R:\>

解决方案

EDIT - npocmaka, you are right. Strange.

Original answer removed - I was wrong.

But problem is not the call command. The problem are the quotes and cmd.

After testing, it seems more a bug/feature in how filenames are processed and how cmd are handling some errors in api calls.

With the following batch file (test.cmd)

@echo off
    setlocal enableextensions

    echo Calling subroutine from drive d:
    call :getInfo
    echo.

    c:
    echo Calling subroutine from drive c:
    call :getInfo
    echo.

    echo Getting data directly without subroutine

:getInfo
    echo ---------------------------------------------------------
    echo cd    : %cd%
    echo d0    : %~d0
    echo dp0   : %~dp0
    echo f0    : %~f0
    echo ---------------------------------------------------------
    echo.
    goto :EOF

placed in d:\temp\testCMD and current directory in drive c: is C:\Users, the results on execution are:

1.- Calling without quotes from cmd directory: test.cmd

Calling subroutine from drive d:
---------------------------------------------------------
cd    : D:\temp\testCMD
d0    : D:
dp0   : D:\temp\testCMD\
f0    : D:\temp\testCMD\test.cmd
---------------------------------------------------------


Calling subroutine from drive c:
---------------------------------------------------------
cd    : C:\Users
d0    : D:
dp0   : D:\temp\testCMD\
f0    : D:\temp\testCMD\test.cmd
---------------------------------------------------------


Getting data directly without subroutine
---------------------------------------------------------
cd    : C:\Users
d0    : D:
dp0   : D:\temp\testCMD\
f0    : D:\temp\testCMD\test.cmd
---------------------------------------------------------

Result: everything ok.

2.- Calling with quotes from cmd directory "test.cmd" (no, no need for call command)

Calling subroutine from drive d:
---------------------------------------------------------
cd    : D:\temp\testCMD
d0    : D:
dp0   : D:\temp\testCMD\
f0    : D:\temp\testCMD\test.cmd
---------------------------------------------------------


Calling subroutine from drive c:
---------------------------------------------------------
cd    : C:\Users
d0    : D:
dp0   : D:\temp\testCMD\
f0    : D:\temp\testCMD\test.cmd
---------------------------------------------------------


Getting data directly without subroutine
---------------------------------------------------------
cd    : C:\Users
d0    : C:
dp0   : C:\Users\
f0    : C:\Users\test.cmd
---------------------------------------------------------

Result: Failure to get correct value of %~d0 ONLY if directly get from main execution line of cmd. The same with subroutine call works as expected.

All scenarios tested without quotes work without failure. With quotes, if the calling line include the drive (ej: "d:.\test.cmd") all values are correctly retrieved. If not drive included in batch call, (ej: "test.cmd" with batch directory in path, or "\temp\testCMD\test.cmd" from root of D:), incorrect values retrieved, but only from main line of execution in batch file. Subroutines always get correct values.

Why? No idea. But when tracing cmd execution with procmon, in failure cases, when cmd.exe try to retrieve the information for the file, a QueryDirectory API call is made for C:\Users\test.cmd which is answered with NO SUCH FILE, but cmd ignores it and continues execution, showing the wrong values.

So, no answer, sorry. But i had to "document" this. Some guru in the room?

这篇关于CMD:中失败%〜D0时,受理报价批处理文件的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆