Solved

Passing arguements to call batch file from Vb.Net

Posted on 2014-10-10
6
956 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
[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
  • 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
Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 
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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

726 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