Solved

Call a .bat file from Vb.net

Posted on 2014-09-29
19
1,014 Views
Last Modified: 2014-09-29
I am calling a .bat file from Vb.net Program. I kept .bat file in my Project folder\ShellScript\fileUpload.bat

I have an issue with System.Diagnostics.Process.Start Syntax. This syntax goes to C:\Windows\System32\ - Folder.

I have to call an .exe ( sfxcl.exe ). This is located in C:\Program Files (x86)\VanDyke Software\Clients

Do System.Diagnostics.Process.Start will not go directly to .exe ?? I can execute the .bat file from any drive. I have run the .bat file from D drive.


           
 Dim sBatchfilePath As String = String.Empty
            sBatchfilePath = AppDomain.CurrentDomain.BaseDirectory
            sBatchfilePath = sBatchfilePath.Replace("\bin\Debug", "\ShellScript")
            sBatchfilePath = sBatchfilePath & "fileUpload.bat"

            System.Diagnostics.Process.Start(sBatchfilePath)

Open in new window

0
Comment
Question by:chokka
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 5
  • 4
19 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40350082
Can you change the batch file?  If so and you put in at the start

@echo off
pushd "C:\Program Files (x86)\VanDyke Software\Clients"

then the current directory at that point will be the directory you want regardless of how it is called?
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40350110
When you display the value of the string sBatchfilePath what value is stored in it? It may not be where you think.
0
 

Author Comment

by:chokka
ID: 40350129
At present, I am getting the value in the variable : SbatchFilePath  as C:\Windows\System32\

I have to clean up the .bat file also.

I tried using Pusd syntax, But it doesn't work.
D--Ups.SalesApps.Net-ShellScript-sfxclUp
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40350144
But this is not where the bat file is, C:\Windows\System32\, or the copy of the one you wish to run.

From what directory, full path, is the application running from?
0
 

Author Comment

by:chokka
ID: 40350155
I am calling .bat file from my Vb.Net Project. I kept .bat file in the Project folder\ShellScript\ is the sub folder.

sfxcl.exe is located in C:\Program Files (x86)\VanDyke Software\Clients
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 40350164
BTW You can do:

echo %Time% : File transfer script started. >> %LOGFILE%

instead of this:

FOR /f "tokens=1-3 delims=: " %%a in ('Time /T') do SET CurTime=%%a:%%b%%c
echo %CurTime% : File transfer script started. >> %LOGFILE%

Also if you add the path to your executable in here it should do too?

Set SFXCLCMD="C:\Program Files (x86)\VanDyke Software\Clients\SFXCL" %SRC% %SFXCLOPTS% %DST%
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40350168
What is the full path to the project folder where the debug\bin directory is in.
0
 

Author Comment

by:chokka
ID: 40350198
Project Folder : D:\MyProject\bin\Debug

Batch file is in : D:\MyProject\ShellScript
0
 

Author Comment

by:chokka
ID: 40350231
Set SFXCLCMD="C:\Program Files (x86)\VanDyke Software\Clients\SFXCL" %SRC% %SFXCLOPTS% %DST%


Doesnt worked.
0
 

Author Comment

by:chokka
ID: 40350233
Still i am having a tough time on calling .bat file from .Net Code
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40350241
yes anything changed in bat file is pointless if it isn't running!
0
 

Author Comment

by:chokka
ID: 40350242
How to rewrite the .bat file , which can be executed from any drive in the computer. Let's not get into .Net Program. I just want to invoke .bat file from any drive.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40350264
chokka please read the comments I placed in your code.

'' You start out with an empty string in the variable sBatchfilePath
Dim sBatchfilePath As String = String.Empty

'' After executing the next line of code sBatchfilePath should contain "D:\MyProject\bin\Debug\"
sBatchfilePath = AppDomain.CurrentDomain.BaseDirectory

'' After the next line is executed sBatchfilePath should contain "D:\MyProject\bin\Debug\ShellScript\"
sBatchfilePath = sBatchfilePath.Replace("\bin\Debug", "\ShellScript")

'' And finally after the next line sBatchfilePath is executed it should contain "D:\MyProject\bin\Debug\ShellScript\fileUpload.bat"
sBatchfilePath = sBatchfilePath & "fileUpload.bat"

'' So I do not understand why sBatchfilePath is equal to At present, C:\Windows\System32\ as you stated in one of the post 
'' when it should be "D:\MyProject\bin\Debug\ShellScript\fileUpload.bat"
System.Diagnostics.Process.Start(sBatchfilePath)

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40350281
So is the batch file running but the default path is wrong, or is the batch file not running?

If it is running and you want to see where it is running then type

echo %cd%
pause

at the top of it.

Steve
0
 

Author Comment

by:chokka
ID: 40350304
@Fernando, I am not able to understand too why sBatchfIlePath is mapping to C:\Windows\System32.

I have no idea
0
 

Author Comment

by:chokka
ID: 40350308
Steve, You are right. I have to make sure .bat file is running correctly or not. My understanding is to fix the .bat file and then call it from .Net
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40350323
Lets try this, change this line of code

System.Diagnostics.Process.Start(sBatchfilePath)

to this for a test.

System.Diagnostics.Process.Start("D:\MyProject\bin\Debug\ShellScript\fileUpload.bat")

Then run the application and see what happens.
0
 

Author Comment

by:chokka
ID: 40350543
@Fernando, one you mentioned in the comment id 40350323 doesnt work.  System cannot find the file path is the Error Message, i get.

This syntax works. Because, File Path is correct.

System.Diagnostics.Process.Start("D:\\Ups.SalesApps.Net\\ShellScript\\fileUpload.bat")
0
 

Author Closing Comment

by:chokka
ID: 40350563
This Syntax works : Set SFXCLCMD="C:\Program Files (x86)\VanDyke Software\Clients\SFXCL" %SRC% %SFXCLOPTS% %DST%
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question