Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of pushd and popd command with UNC path

Tags:

batch-file

I am running the following batch file to run a stored package in SQL and I am getting a UNC Paths are not supported error. If I run it on the server it runs correctly. If I share it with other users it does not. I do not want to create a map drive. I've been reading about pushd command and it may be an option, but not sure how it works. Can someone guide me?

"\\Server\c$\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\DTExec.exe" /f "\\Sever\c$\Packages\Unapplied and Patient Balances.dtsx"
like image 475
John Molina Avatar asked Mar 11 '23 11:03

John Molina


1 Answers

You can use pushd as a quick version of net use if you will.
As you already observed UNC paths are not supported and so you have to work around. The command pushd \\Server\Path\ will create a network drive on the machine it is running on like Z:\ , automatically switch to it (like cd /d Z:\) and pushing the path on a stack (relevant for later).

You can then use this to access the server directories:

pushd "%~dp0"
pushd \\Server\c$
"Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\DTExec.exe" /f "Packages\Unapplied and Patient Balances.dtsx"
popd

Should do the trick.

The command popd is used to
1) delete the network drive and
2) pop the directory from the path

So you could actually could first push all the server paths you need (up to ~20 based on your file system) and in a loop execute the same thing for each path and execute popd to go to the next stored path.

When adding a pause before the popd and looking into the explorer, you can see the drive connected.

NOTE: This will only work if at least one drive letter is still unused as it is needed for temporary mapping!

Edit: Added pushd "%~dp0" to get rid of a warning mentioned in the comments. %dp0 stands for the drive and path of the 0th batch-file argument that is always the batch-file itself. The ~ removes potential surrounding quotes.

Edit2: I started my Windows 7 VM to test this on my own. I wrote an .exe-file that only purpose it is to execute a batch-file that simply will create a file on the Desktop with the current time. I have no server so I could not test it to 100% but when trying to run it from the VM using pushd \\localhost\c$ it worked fine... I tried to establish a connection from the host machine, but could not get it to work that way nor the other. Have you tried just running a batch-file that you create on the server? Something like:

%time%>"%USERPROFILE%\Desktop\myFile.txt"

Just to test the connection?

Feel free to ask if something is not clear!

like image 139
geisterfurz007 Avatar answered Mar 25 '23 01:03

geisterfurz007