Solved

Passing arguements to call batch file from Vb.Net

Posted on 2014-10-10
6
845 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

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.

Question has a verified solution.

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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now