I'm trying to create a batch script that will open a program in a different environment (so I can make a portable version of it).

I use this set APPDATA=%CD%\data to tell it to do that. The problem I'm facing is that I get the following error when the batch starts:

CMD.EXE was started with the above path as the current directory. UNC paths are not supported. Defaulting to Windows directory.

I want to, instead use the cd command to change it to the batch directory. Is there an easy way to do this?


If it is on a network drive you might still have issues unless you map the drive buy why not do the following?

cd /d %APPDATA%

By doing a cls you can also clear the error but it the script is run from a UNC path you will receiving this error, you will just have to map it or CLS the error out.

| improve this answer | |

Try putting this in the batch:

pushd "%~p0" 2> nul
pushd "\\%~p0" 2> nul

If started from a UNC folder, the 1st line will fail, but the 2nd will work.

If started from a drive mapped folder, the 2nd line will fail, but the 1st will work.

Either way, the current folder will be where the batch file is sitting, and you can change folders from there.

Later, you can use cd to show which folder you are working from.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.