another dirty trick for windows batch file

I shared in another earlier post a dirty tick to change directory to where the batch file is located; this is useful when you want to run your batch file from any directory prompt;

C:\xxxx\yyy\anything\myscript.bat

The trick is one single line:

cd /d %0\..



However, this has some limitations.

It is more often than not when you release your batch file in a network shared directory, and your end user want to run directly from there (i.e. without copying to local disk); if the drive is not mapped to the local system, you will be caught in surprise when running the above line.


The error is "CMD does not support UNC paths as current directories";


The hack is simple -- another magic line:


pushd %~dp0