We help IT Professionals succeed at work.

Trigger executable from batch file

1,076 Views
Last Modified: 2014-05-21
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.
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Distinguished Expert 2019

Commented:
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 KnightIT Consultancy
CERTIFIED EXPERT

Commented:
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
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2016

Commented:
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

Author

Commented:
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 KnightIT Consultancy
CERTIFIED EXPERT

Commented:
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

Author

Commented:
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.
CERTIFIED EXPERT
Top Expert 2016

Commented:
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 KnightIT Consultancy
CERTIFIED EXPERT

Commented:
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

Author

Commented:
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?
CERTIFIED EXPERT
Most Valuable Expert 2012
Distinguished Expert 2019

Commented:
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

Author

Commented:
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.
CERTIFIED EXPERT
Most Valuable Expert 2012
Distinguished Expert 2019

Commented:
>>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.

Author

Commented:
Raising to 500 points.
CERTIFIED EXPERT
Top Expert 2016

Commented:
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

Author

Commented:
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
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks both.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.