batch-filewindows-task-schedulersystem32

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.

What's the reason why it happens and how do I retain the working directory?


Solution

  • Put this line at the top of your bat file:

    pushd %~dp0
    

    See this post for details Difference between "%~dp0" and ".\"?