简体   繁体   English

为什么在执行不同目录中的批处理文件时工作目录更改为 system32 以及如何更改它

[英]Why working directory changes to system32 when executing a batch file that's in a different directory and how to change it

I have a batch file that's in my desktop and it works properly when I execute it using double click.我的桌面上有一个批处理文件,当我使用双击执行它时它可以正常工作。 But when I execute it using a task scheduler or run as administrator, the working directory changes to C:\Windows\system32 and it doesn't work properly.但是当我使用任务计划程序执行它或以管理员身份运行时,工作目录更改为 C:\Windows\system32 并且无法正常工作。

What's the reason why it happens and how do I retain the working directory?它发生的原因是什么以及如何保留工作目录?

Put this line at the top of your bat file:将此行放在 bat 文件的顶部:

pushd %~dp0

See this post for details Difference between "%~dp0" and ".\"?有关详细信息,请参阅此帖子“%~dp0”和“.\”之间的区别?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM