Solved

Passing arguements to call batch file from Vb.Net

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

Expert Comment

by:Kyle Abrahams
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 39

Accepted Solution

by:
Kyle Abrahams earned 500 total points
Comment Utility
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
Comment Utility
@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
Comment Utility
Thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 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

11 Experts available now in Live!

Get 1:1 Help Now