0

I want to check if the current file exists at the same time save video to different file but it only overwrites to an existing file.

Here is my code:

Private Sub ButtonVIDEO_Click(ByVal sender As System.Object, e As System.EventArgs) Handles ButtonVIDEO.Click

    f = New Filters
    cap = New Capture(f.VideoInputDevices(0), f.AudioInputDevices(0))
    cap.PreviewWindow = PictureBox1
    Dim Filename As String = "c:\\folder\MyFile"
    Dim i As Integer = 0
    Dim extension As String = ".mp4"

    If ButtonVIDEO.BackColor = Color.Black Then
        cap.Filename = Filename + extension

        If File.Exists(Filename) Then
            Do
                i = i + 1
            Loop While File.Exists(Filename + i.ToString() + extension)
            Filename = Filename + i.ToString()
        End If

        cap.Cue()
        cap.Start()
        ButtonVIDEO.BackColor = Color.Red
        ButtonVIDEO.Text = "PLAYING"
        ButtonPHOTO.Hide()



    End If
End Sub
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
anonymous1
  • 13
  • 5

1 Answers1

0

You are assigning the file name to cap before searching for an unsused file name. Also, there is an error in your logic, as you are creating the full file name with Filename + i.ToString() + extension but you also append i to Filename itself with Filename = Filename + i.ToString(). So, in then end, you are appending the number twice.

Private Sub ButtonVIDEO_Click(ByVal sender As System.Object, e As System.EventArgs) _
    Handles ButtonVIDEO.Click

    Dim Filename As String = "c:\folder\MyFile"
    Dim i As Integer = 0
    Dim extension As String = ".mp4"
    Dim path As String = Filename & extension

    f = New Filters
    cap = New Capture(f.VideoInputDevices(0), f.AudioInputDevices(0))
    cap.PreviewWindow = PictureBox1

    If ButtonVIDEO.BackColor = Color.Black Then
        If File.Exists(path) Then
            Do
                i = i + 1
                path = Filename & i & extension
            Loop While File.Exists(path)
        End If

        cap.Filename = path
        cap.Cue()
        cap.Start()
        ButtonVIDEO.BackColor = Color.Red
        ButtonVIDEO.Text = "PLAYING"
        ButtonPHOTO.Hide()
    End If
End Sub

The path should have only one backslash in c:\folder. In VB strings are concatenated with &, not +.

See answer to The difference between + and & for joining strings in VB.NET

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188