问题描述
我们有一些批处理脚本,它们看起来运行良好,但是我正在编辑其中的一个以包含一些新工作,并且当手动运行该脚本时(通过从Windows资源管理器中双击),每次按下命令时,我都会收到上述错误消息在脚本中遇到.这些是自年点以来一直存在的推送命令,脚本运行良好.手动运行的批处理脚本与计划程序启动的批处理脚本之间有区别吗?手动运行时如何禁用错误消息(如果可能的话)?
双击批处理文件cmd.exe
将使用选项/C
隐式启动,以在完成批处理文件执行后关闭命令进程,并设置目录的批处理文件作为当前目录.
如果双击的批处理文件位于网络共享上,并且使用UNC路径而不是将网络共享映射到驱动器号并使用网络驱动器,则cmd.exe
通知用户不支持将UNC路径用作当前目录并将%SystemRoot%
(Windows目录)设置为当前目录.
如果批处理文件旨在从任何目录执行,则可以忽略该警告,这意味着它不需要批处理文件的目录为当前目录.良好的编码批处理文件,尤其是Windows任务计划程序正在执行的批处理文件,可以使用任何目录作为当前目录来执行.
在将批处理文件作为计划任务运行时,任务计划程序在执行命令之前将%SystemRoot%\System32
(Windows系统目录)设置为当前目录,但在计划任务属性中将特定目录路径显式设置为start in directory. /p>
请注意,当您启用运行以UNC路径作为当前目录的批处理文件的支持时,某些控制台应用程序可能由于未针对该环境而设计而无法运行.
在您的情况下,批处理文件设计为按计划任务执行,因此考虑到我在问题在按计划任务执行批处理文件时必须考虑什么?,使用UNC路径双击批处理文件时,您可以忽略此警告消息.
当然,您也可以为批处理文件创建快捷方式文件(* .lnk),以通过将快捷方式文件属性中的开始于设置为%SystemRoot%\System32
,通过此快捷方式启动批处理文件.就像Windows Task Scheduler在默认情况下一样.
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)?
On double clicking a batch file cmd.exe
is started with implicit using option /C
to close command process after finishing batch file execution and with setting directory of the batch file as current directory.
If the double clicked batch file is on a network share and 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 UNC path as current directory is not supported and sets %SystemRoot%
(Windows directory) as current directory.
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.
On running a batch file as scheduled task the task scheduler sets %SystemRoot%\System32
(Windows System directory) as current directory before executing the command, except a specific directory path is explicitly set as start in directory in properties of scheduled task.
See also: How can I disable the Universal Naming Convention (UNC) check for command sessions?
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.
In your case with 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.
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路径作为当前目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!