Solved

Passing arguements to call batch file from Vb.Net

Posted on 2014-10-10
6
875 Views
Last Modified: 2014-10-15
I am calling .bat file from Vb.Net by passing Source File and Destination File Folder as Arguements.

If i call .bat file directly from cmd and pass the arguements, .bat file works !! I checked the values passed on the variables and everything looks correct to me. Still i couldn't find why my syntax fails to process the command in the .bat file. I dont get any errors or exceptions.


Dim strSourceFile As String = String.Empty
            strSourceFile = "D:\csv\TestMO.csv"

            Dim strDestPath As String = String.Empty
            strDestPath = "//home/myfolder/myfolder/"

            Dim strUserName As String = String.Empty
            strUserName = "username"

            Dim strPassword As String = String.Empty
            strPassword = "password"

            Dim strHostName As String = String.Empty
            strHostName = "hostname"

            Dim iPort As Integer
            iPort = 22

            Dim sSFTP As String = String.Empty
            sSFTP = "sftp://" & strUserName & ":" & strPassword & "@" & strHostName & ":" & iPort & strDestPath

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

            Dim proc As New System.Diagnostics.Process()
            proc.StartInfo.UseShellExecute = False
            proc.StartInfo.FileName = strBatchFile
            proc.StartInfo.Arguments = String.Format("{0},{1}", strSourceFile, sSFTP)
            proc.StartInfo.CreateNoWindow = True
            proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
            proc.Start()

Open in new window

0
Comment
Question by:chokka
  • 4
  • 2
6 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 40374208
proc.StartInfo.Arguments = String.Format("{0},{1}", strSourceFile, sSFTP)

shouldn't that just be:

 proc.StartInfo.Arguments = String.Format("{0} {1}", strSourceFile, sSFTP)  



eg:

callsfxcl.bat   strSourcefile,sSFTP  
    vs
callsfxcl.bat  strSourcefile sSFTP

what's in the bat file or how do you need the parameters to be called?
0
 

Author Comment

by:chokka
ID: 40375820
Inside the .bat file.



@Echo off


::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:: This is the file on the remote site that we will be downloading

Set SRC= %1
Set DST= %2

:: This is the destination folder on the local machine (could be passed in 
:: as a parameter (e.g. %1))

Set SFXCLCMD="C:\Program Files (x86)\V Software\Clients\sfxcl.exe" %SRC% %DST%

:: Now, issue the actual SFXCL command to transfer the file
%SFXCLCMD%

:: Check for Failure
if not %errorlevel%==0 goto TRANSFER_FAILED

:: Success!
Goto Finished

::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:TRANSFER_FAILED
FOR /f "tokens=1-3 delims=: " %%a in ('Time /T') do SET CurTime=%%a:%%b%%c
echo.    %CurTime% : Transfer Failure.  
goto End

	
::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:Finished
FOR /f "tokens=1-3 delims=: " %%a in ('Time /T') do SET CurTime=%%a:%%b%%c
echo %CurTime% : Transfers Completed successfuly. 

::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:End

Open in new window

0
 

Author Comment

by:chokka
ID: 40375825
If i called directly the .bat file from command prompt by providing sourcefile and destinationfile parameters, I am able to see the output. .Bat file is executed correctly.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 40380098
your format is off.

If you're looking for a %1 %2  that's 2 separate parameters.

 proc.StartInfo.Arguments = String.Format("{0},{1}", strSourceFile, sSFTP)  

would read as "mybat.bat SRC,DEST"

you need it to read as "mybat.bat SRC DEST"

which is:
 proc.StartInfo.Arguments = String.Format("{0} {1}", strSourceFile, sSFTP)
0
 

Author Comment

by:chokka
ID: 40380907
@Kyle, Thank you !! I tried both ways. But my hunch is that something went wrong in that specific .exe file. I will try reinstalling the application.
0
 

Author Closing Comment

by:chokka
ID: 40382089
Thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTML - Color not displaying correctly in EMAIL. 6 37
SQL Help 27 45
Help with preventing downloading a zip file 10 37
Winform Module - What is the ASP.Net equiv 2 21
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

773 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