Using UNC paths in Batch files

15 Mar

I tend to write a lot of batch files, mostly for backing up this and that. When working with UNC paths you can sometimes encounter errors such as:

Using xopy (c:\>xcopy \\server\path c:\path) tends to get around this, but sometimes you need a bit more.

I came across PUSHD and POPD today which are pretty useful.

Basically it creates a temporary mapped drive (starting at Z: and then backwards through the alphabet). To run it, simply call PUSHD to the UNC path, run your bits and pieces and then call POPD to remove the mapping, eg:

pushd \\server\path
run commands
popd

If you want to just map network drives and then un-map them, use:

net use x: \\server\path
net use x: /delete

Simples.