开发者

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

开发者 https://www.devze.com 2023-04-06 17:27 出处:网络
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

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 ph开发者_StackOverflowysical 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


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.


That's called a UNC path.
You can use it just like any other path.

However, the user that your ASP.Net code is running as must have read access to the network share.


Apparently, you do have a current-directory issue.
The .bat file is trying to run myprogram.exe from the current directory.

You can make a wrapper batch file on your local machine that maps the network share:

pushd \\server\c$\dir
call filename.bat
popd

You can put this wrapper file anywhere, then call it from your code.

0

精彩评论

暂无评论...
验证码 换一张
取 消