Avatar of AlHal2
AlHal2
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Trigger executable from batch file

I have a batch file like this.  The batch file and program are in the same directory.  When I run the batch file as above I get a message about IOPermissions.

set PATH=\\Server\test1\test2\software\
call %PATH%Preprocessor.exe %PATH%config.cfg
pause

However when I change the batch file to this the program works.

set PATH=\\Server\test1\test2\software\
call Preprocessor.exe %PATH%config.cfg
pause

Why should including the UNC path cause the program to fail like this?
I'm using C# in visual studio 2008.  It's a windows (not web) application.
C#Scripting LanguagesMicrosoft DOSWindows OSWindows Batch

Avatar of undefined
Last Comment
AlHal2

8/22/2022 - Mon
slightwv (䄆 Netminder)

Responding to a Neglected question alert.

UNC is not supported in Windows BAT scripts:
http://social.technet.microsoft.com/Forums/systemcenter/en-US/bf917460-d71b-49f2-b315-829866b1d022/unc-path-are-not-supported?forum=configmgrswdist

I'm curious how C# enters into the picture.
Steve Knight

Have you actually used "%path%"?  That is the system reserved variable for the search path used to find exe's so is not a good idea for user variable.  Suggest use "%mypath%" or whatever.  Also if your real server path has a space in the filename or dir name anywhere will need " " around it.

Also an easy option is to use pushd which maps a temporary drive to the path if it is a UNC, or for local drive  then changes to that dir, i.e.

@echo off
set MyPATH=\\Server\test1\test2\software
pushd %MyPath%
  call Preprocessor.exe "%MyPATH%\config.cfg"
popd
pause

Open in new window


Steve
SOLUTION
pepr

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
sarabande

to add to before comments:

slightwv has explained why the call of %path%preprocessor.exe failed.

the call without %path% worked because you changed the system environment variable PATH as already pointed out by dragon-it.

dragon-it is right that it is a bad idea to use PATH as a private macro but for the sample script it helped to find the executable and to pass a valid path as argument to the program.

you could do the following to have the advantages but not the drawbacks:

@echo off
set EXEDIR="\\Server\test1\test2\software"
set PATH=%PATH%;%EXEDIR%
call Preprocessor.exe %EXEDIR%\config.cfg
pause

Open in new window


Sara
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
AlHal2

ASKER
Thanks for your comments.  I need to run this batch file from a remote machine.  Therefore I think I need
call %EXEDIR%\Preprocessor.exe %EXEDIR%\config.cfg
Steve Knight

just use pushd to change the current dir to that path.  Also you don't need / want call as it is an EXE, i.e.

@echo off
pushd \\server\share\whatever
  Preprocessor.exe config.cfg
popd

That will set the current dir for the exe so it presumably will pickup the path for config.cfg from the same dir then.

Steve
AlHal2

ASKER
I still get this error when I try to open a file with the streamreader object.

Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
   at System.Environment.Exit(Int32 exitCode)

Why should the permission to open a file vary with how I call the executable?
Could it be that specifying the path means I need a certain level of permission on the server as a whole?
For testing purposes the batch file, the executable, input files and output files are all on the same server.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
sarabande

Therefore I think I need
 call %EXEDIR%\Preprocessor.exe %EXEDIR%\config.cfg

calls using an unc path work from my win7 cmd shell.

but from c# it obviously is not the same as you had problems with "call %PATH%\Preprocessor.exe

so actually i would extend the Path environment variable temporary in the batch or - alternatively - define a drive for the unc path in the batch file

net use z: "\\Server\test1"
if errorlevel 1 goto error
call z:\test2\software\preprocessor.exe %EXEDIR%\config.cfg
goto end
:error

Open in new window

....
:end
Sara
Steve Knight

Or just change to the right directory with pushd/cd ?!

Could it be you have the same exe locally so when you ran without a path it ran your local copy.  It is likely then UC will be involved and running a local exe will work differently to a remote one. So... from a cmd.exe prompt typed manually does it work with

pushd "\\server\share\whatever"
Preprocessor.exe config.cfg

or

"\\server\share\whatever\Preprocessor.exe" "\\server\share\whatever\config.cfg"

i.e. forgetting running it from c# which works from cmd.exe as that is what batch would run.

Also do you need to run a batch, can you not just run the exe from shell directly from c# with command line parameter in there too?

Steve
AlHal2

ASKER
I'm afraid the problem remains.  It occurs when I use a streamreader.  How do you know it's not some kind of server permissioning issue?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
slightwv (䄆 Netminder)

I remember our developer and sys admin having this discussion.  There is a policy issue running .Net apps from network drives.

I forget the exact action to take.

A quick Google brought back the link below and it seemed familiar:
http://social.msdn.microsoft.com/Forums/vstudio/en-US/e415255c-0fd3-4500-8e46-4d36220913fe/systemsecuritysecurityexception-request-for-the-permission-of-type?forum=wcf
AlHal2

ASKER
Below is an extract from the link.  This is a windows rather than a web app  I'm running it by double clicking on the batch file.  What would be the equivalent of the config file?

Security Exception
Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
 
Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
slightwv (䄆 Netminder)

>>What would be the equivalent of the config file?


You'll have to wait for a better .Net Expert than I am.  I know just enough to be dangerous but not all the admin and policy stuff.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
AlHal2

ASKER
Raising to 500 points.
sarabande

do you get the same exception when you run the batch in administrator mode?

is there more info available like the mentioned 'stack trace below'?

I wonder why you said in the original q. that the batch "worked" when not using unc. is this still the case? or did you try with a different executable?

Sara
AlHal2

ASKER
The server is quite old, so there is no option to run in admimistrator mode.
I've attached stack trace.
The batch works when not using unc.

This fails.
set exePATH=IPaddress\tqaftp\RDF\software
\\%exePATH%\PreProcessor2.exe \\%exePATH%\config.cfg
pause

This works
set exePATH=IPAddress\tqaftp\RDF\software
PreProcessor2.exe \\%exePATH%\config.cfg
pause
StackTrace.doc
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
AlHal2

ASKER
Thanks both.