CMD 不支持 UNC 路径作为当前目录 [英] CMD does not support UNC paths as current directories

查看:337
本文介绍了CMD 不支持 UNC 路径作为当前目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的批处理脚本似乎运行良好,但我正在编辑其中一个脚本以包含一些新工作,并且在手动运行脚本时(通过从 Windows 资源管理器双击),每次执行 pushd 命令时都会收到上述错误消息在脚本中遇到.这些是自年点以来一直存在的 pushd 命令,并且脚本运行良好.手动运行的批处理脚本和由调度程序启动的批处理脚本之间有区别吗?手动运行时如何禁用错误消息(如果可能)?

We have batch scripts that seem to run fine however I am editing one of them to include some new work and when manually running the script (by double-clicking from windows explorer) I get the above error message each time a pushd command in the script is encountered. These are pushd commands that have been there since the year dot and the scripts are running fine. Is there a difference between the batch script running manually and when started by a scheduler? How can I disable the error message when running manually (if this is possible)?

推荐答案

Windows 文件资源管理器 explorer.exe 启动 cmd.exe 并隐式使用选项 /C 完成批处理文件执行后关闭命令进程,并在双击批处理文件时将批处理文件目录设置为当前目录.

The Windows File Explorer explorer.exe starts cmd.exe with implicit usage of the option /C to close command process after finishing batch file execution and with setting the batch file directory as current directory on double clicking a batch file.

如果双击的批处理文件位于网络共享上并且使用 UNC 路径而不是将网络共享映射到驱动器号并使用网络驱动器,cmd.exe 会通知用户不支持将 UNC 路径作为当前目录并将 %SystemRoot%(Windows 目录)设置为当前目录.

If the double clicked batch file is on a network share and a UNC path is used instead of mapping the network share to a drive letter and use the network drive, cmd.exe informs the user that the UNC path as current directory is not supported and sets %SystemRoot% (Windows directory) as current directory.

如果批处理文件被设计为从任何目录执行,则可以忽略警告,这意味着它不需要批处理文件的目录是当前目录.好的编码批处理文件,尤其是那些被Windows任务调度程序执行的文件,可以在任何目录作为当前目录执行.

The warning can be ignored if the batch file is designed for being executed from any directory which means it does not require that the directory of the batch file is the current directory. Good coded batch files, especially those being executed by Windows task scheduler, can be executed with any directory being the current directory.

另见:如何禁用对命令会话的通用命名约定 (UNC) 检查?

可以使用注册表值配置 Windows 命令处理器 cmd.exe 通过在命令提示符窗口中运行来接受当前目录的 UNC 路径:

It is possible to configure with a registry value that the Windows command processor cmd.exe accepts a UNC path for the current directory by running in a command prompt window:

%SystemRoot%System32
eg.exe ADD "HKEY_CURRENT_USERSoftwareMicrosoftCommand Processor" /v DisableUNCCheck /t REG_DWORD /d 1 /f

请注意,当您启用对以 UNC 路径作为当前目录运行批处理文件的支持时,某些控制台应用程序可能会因为不是为该环境设计而无法运行.

Please note that when you enable support for running batch files with UNC path as current directory, some console applications could fail running because of not being designed for that environment.

任务调度器将%SystemRoot%System32(Windows系统目录)设置为当前目录,然后再执行运行批处理文件作为计划任务的命令,除了明确设置特定目录路径为start in 计划任务属性中的目录.

The task scheduler sets %SystemRoot%System32 (Windows system directory) as current directory before executing the command on running a batch file as scheduled task, except a specific directory path is explicitly set as start in directory in properties of scheduled task.

在您的情况下,批处理文件设计为作为计划任务执行,因此考虑到我在回答问题时列出的所有方面 将批处理文件作为计划任务执行时必须考虑什么?,使用 UNC 路径双击批处理文件时,您可以忽略此警告消息.

In your case with the batch file designed for execution as scheduled task and taking therefore all aspects into account as listed by me in answer on the question What must be taken into account on executing a batch file as scheduled task?, you can ignore this warning message on double clicking the batch file using UNC path.

当然你也可以为批处理文件创建一个快捷方式文件(*.lnk),通过这个快捷方式启动批处理文件,快捷方式文件属性中的Start in设置为%SystemRoot%System32 就像 Windows 任务调度程序在默认情况下所做的那样.

Of course you can also create a shortcut file (*.lnk) for the batch file for starting the batch file via this shortcut with Start in in properties of shortcut file is set to %SystemRoot%System32 as Windows task scheduler does by default.

这篇关于CMD 不支持 UNC 路径作为当前目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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