max07
max07

Reputation: 141

Use string from visual basic in batch file

I am working on a programm in visual basic, that will start a batch file which does a few things. What I need is passing an argument from visual basic to my batch file.

Here is what I have so far in visual basic:

If M.Msg = WM_DEVICECHANGE Then
            Select Case M.WParam
                Case DBT_DEVICEARRIVAL
                    Dim DevType As Integer = Runtime.InteropServices.Marshal.ReadInt32(M.LParam, 4)
                    If DevType = DBT_DEVTYP_VOLUME Then
                        Dim Vol As New DEV_BROADCAST_VOLUME
                        Vol = Runtime.InteropServices.Marshal.PtrToStructure(M.LParam, GetType(DEV_BROADCAST_VOLUME))
                        If Vol.Dbcv_Flags = 0 Then
                            For i As Integer = 0 To 20
                                If Math.Pow(2, i) = Vol.Dbcv_Unitmask Then
                                    Dim Usb As String = Chr(65 + i) + ":\"
                                    MsgBox("New device found!" & vbNewLine & vbNewLine & "The drive letter is: " & Usb.ToString & vbNewLine & "Start backup?")
                                    Dim DosRun As Process = New Process
                                    Dim strArgs As String
                                    strArgs = Usb.ToString
                                    DosRun.StartInfo.WindowStyle = ProcessWindowStyle.Maximized
                                    DosRun.StartInfo.FileName = "C:\Users\info\Desktop\Backup.bat"
                                    DosRun.StartInfo.Arguments = Usb
                                    DosRun.Start()
                                    Exit For
                                End If
                            Next
                        End If
                    End If

This is part of what my batch file looks like:

xcopy %Usb% %ziel% /E /V /W /I /F /H /D /Y /EXCLUDE:C:\Users\info\Desktop\Exclude.txt

I need to pass the argument Usb to the batch file. Can anyone help me?

Upvotes: 2

Views: 213

Answers (2)

Visual Vincent
Visual Vincent

Reputation: 18310

In Batch files, arguments are caught by entering % and the number of the argument. %1 would be the first argument, %2 the second one, and so on.

Simply change %Usb% to %1 to catch the first argument.

xcopy %1 %ziel% /E /V /W /I /F /H /D /Y /EXCLUDE:C:\Users\info\Desktop\Exclude.txt

Upvotes: 3

miroxlav
miroxlav

Reputation: 12184

In your batch file, replace %Usb% with %1 and you are good to go.

Upvotes: 1

Related Questions