5

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"
aschipfl
  • 33,626
  • 12
  • 54
  • 99
John Molina
  • 179
  • 3
  • 14
  • 1
    read [this](http://stackoverflow.com/questions/9013941/how-to-run-batch-file-from-network-share-without-unc-path-are-not-supported-me) – elzooilogico Dec 06 '16 at 14:08
  • Read this: [`pushd`](http://ss64.com/nt/pushd.html) and [`popd`](http://ss64.com/nt/popd.html) – aschipfl Dec 06 '16 at 23:32

1 Answers1

8

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!

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
  • I did exactly what you said and paused it and can see that a drive letter y is created but still get UNC paths are not supported. – John Molina Dec 06 '16 at 14:43
  • Have a look at the code in my answer. Notice that I have deleted the parts of the paths that were causing the issue: `\\Server\c$`. If that does not solve the issue, are there any other messages? – geisterfurz007 Dec 06 '16 at 14:55
  • '\\ServerName\C$\BatFile CMD.exe was started with the above path as the current directory. UNC paths are not supported. Defaulting to windows directory. C:\Winddows>pushd \\servername\c$ Y:\>"Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\DTExec.exe" /f "Packages\ Unapplied and Patient Balances.dtsx" Y:\popd – John Molina Dec 06 '16 at 15:03
  • This is what it says when I pause it – John Molina Dec 06 '16 at 15:08
  • Has the thing you wanted it to do work? As there was nothing that said `could not find path specified` it should have. In the post elzooilogico linked as comment it read that you could add `pushd %dp0` to get rid of the warning. I will edit that. – geisterfurz007 Dec 06 '16 at 15:16
  • Nope. It doesn't run the .bat file to create the report. It works perfect on the server with pushd \\ServerName\c$ "Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\DTExec.exe" /f "Packages\Unapplied and Patient Balances.dtsx" popd – John Molina Dec 06 '16 at 15:32
  • Do you probably have to grant administrative permissions to execute it? Or run the batch as Admin – geisterfurz007 Dec 06 '16 at 15:36
  • Did it work or didnt it? I am still learning batch and I am eager to know what the problem is/was... – geisterfurz007 Dec 06 '16 at 16:26
  • When I am home I will try something... We will get there! – geisterfurz007 Dec 06 '16 at 16:29
  • @JohnMolina So after nearly one hour of testing I made the edit above... Not really proud of it :/ – geisterfurz007 Dec 06 '16 at 17:43