Setting CMD CD to the directory that the batch was run in

0

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?

Freesnöw

Posted 2011-09-07T11:59:13.320

Reputation: 836

Answers

1

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

APPDATA=%~dp0
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.

David Remy

Posted 2011-09-07T11:59:13.320

Reputation: 1 899

2

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.

Glen Little

Posted 2011-09-07T11:59:13.320

Reputation: 791