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

798 Views Asked by At

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?

1

There are 1 best solutions below

1
RGuggisberg On BEST ANSWER

Put this line at the top of your bat file:

pushd %~dp0

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