Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we run a batch file (.bat) from the shared path

I have a shared path (like //servername/c$/batches/) where all my batch files are located now I am writing an web application in C# to run .bat files from that application. I know way to do it when I have a physical path. But here I dont have a physical path. Is it possible to do it.

EDIT# 1

I execute my bat files just by double clicking on them or open the cmd progam on the physical server and then navigate to the drive and execute the bat file.

EDIT #2

when I put UNC path the get the following error

I getting an error myprogram.exe is not recognized as an internal or external command operable program or batch file. 9009

like image 992
Praneeth Avatar asked Sep 21 '11 17:09

Praneeth


1 Answers

Batch files don't support UNC paths as their "current directory". There's a hackish work around of doing:

pushd "%~dp0"
your batch stuff
popd

%~dp0 expands to the current (d)rive/(p)ath/(0)batchfilename


example:

ok. a Simple batch file:

pushd %~dp0
echo "Hello from batch land"
echo %~dp0
popd

put that on a server somewhere, and try to run it via a unc path:

C:\> \\server\share\test.bat

You'll get as output:

C:\>pushd \\server\share\

Z:\>echo Hello from batch land
Hello from batch land

Z:\>echo \\server\share\
\\server\share\

Z:\>popd

C:\>

Weird, but it works.

like image 101
Marc B Avatar answered Oct 20 '22 01:10

Marc B