Results 1 to 6 of 6

Thread: [RESOLVED] How to Check if file Name already Exist in a specified folder in my computer

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    90

    Resolved [RESOLVED] How to Check if file Name already Exist in a specified folder in my computer

    Hi All,

    I made a program to capture photo,make a file name to it base on the serial number of the items and save in a specified or selected path in my computer, I want to upgrade my program, so that when Users capture same items with same serial number it will alert and tell Users that this serial number is already captured,means already have copy in the path in my computer. so that user will not continue.

    here is my code please help me modify it to avoid duplicate data in my specified path.
    thanks in advanced.

    Code:
    'select path where to save the photo using folderbrowsedialog1 
     Private Sub btnSelectPath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectPath.Click 
            If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then 
                txtPath.Text = FolderBrowserDialog1.SelectedPath 
            End If 
        End Sub 
     
    'when i click this button it will save the photo in a specified path with a fileName of serial number. 
     
     Private Sub btnTop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTop.Click 
     
      Dim strFileName As String 
            strFileName = txtSerialNo.Text & Format(Now, " -T") & ".jpg" 
     
     If txtSerialNo.Text <> "" Then 
                    PictureBox1.Image.Save(txtPath.Text & "\" & strFileName, System.Drawing.Imaging.ImageFormat.Jpeg) 
                    Else 
                    MsgBox("Scan Sn", MsgBoxStyle.Critical, "Error") 
                    txtSerialNo.Focus() 
                End If
    how can i check if the file name is already in the path i specified,
    thank you.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: How to Check if file Name already Exist in a specified folder in my computer

    To find information about files/folders, you can use the System.IO namespace, eg:
    Code:
    If System.IO.File.Exists(txtPath.Text & "\" & strFileName) Then
      'it exists
    End If

  3. #3
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,836

    Re: How to Check if file Name already Exist in a specified folder in my computer

    Please remember next time...elections matter!

  4. #4
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: How to Check if file Name already Exist in a specified folder in my computer

    here a few samples...

    Code:
     Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    
            Dim di As New IO.DirectoryInfo("E:\FebBackup\Berit Praxis")
    
            ' LINQ-Abfragestring
          
    
            ''//get .jpg Files not older than 3 monthes
            'ListBox1.DataSource = (From fi As IO.FileInfo In di.GetFiles() _
            '  Where fi.Extension Like ".jpg" And fi.CreationTime > Now.AddMonths(-3) _
            '  Order By fi.LastAccessTime).ToList
    
            ''// get all jpg from Foldername
            '   ListBox1.DataSource = (From fi As IO.FileInfo In di.GetFiles() _
            'Where fi.Extension Like ".JPG" _
            'Order By fi.LastAccessTime).ToList
    
            ''// check if .jpg File is in Folder and return to Listbox
            ListBox1.DataSource = (From fi As IO.FileInfo In di.GetFiles() _
             Where fi.Name Like "Test.JPG" _
                 Order By fi.LastAccessTime).ToList
            ListBox1.DisplayMember = "Name"
    
    
        End Sub
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    90

    Re: How to Check if file Name already Exist in a specified folder in my computer

    hi Sir si_the_geek ,
    You made it very simple!!!!!
    thank you very much!!

  6. #6
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: How to Check if file Name already Exist in a specified folder in my computer

    Quote Originally Posted by BONITO View Post
    hi Sir si_the_geek ,
    You made it very simple!!!!!
    thank you very much!!
    Code:
    If System.IO.File.Exists(IO.Path.Combine(txtPath.Text, strFileName)) Then

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width