Results 1 to 5 of 5

Thread: How do I NOT overwrite files I am creating that have the same filename?

  1. #1

    Thread Starter
    Lively Member koskilla's Avatar
    Join Date
    Jan 2007
    Location
    LA, California
    Posts
    79

    How do I NOT overwrite files I am creating that have the same filename?

    This program is pretty huge, so I wont go into too much detail, but basically the program takes a file, renames it based on a naming convention I have created, and sorts it into the appropriate folder. If the folder doesn't exist, it will be created, etc. Then the files are added to a database where I can search for them and pull them up.

    Everything works great, except one thing - sometimes files need to have the same name, and when this happens, it will overwrite the old one and add the entry to the database. So I have to entries in the db (which is fine) but only one file. So if I try to open the first file from my db, the new one opens and the old one doesnt exist because it has been overwritten. Does this make sense?

    What I want to be able to do is check if a file exists and if it does, just add a (1) or (2) to the end of it. How would I do this?

    Code:
    FindFileName 'naming convention that creates the filename
        Dim x As String
        Dim y As String
            x = Trim$(strImportPath)
            strFileExt = Right$(x, Len(x) - InStr(x, ".") + 1)
            y = strName & strFileExt
            lblName.Caption = y
    If detailError = False Then
    '''Move File
        MakeSureDirectoryPathExists strNameFolder
        FileCopy x, y
        Kill strImportPath
        
    '''Save to Documents Database
        SaveToDB
    
    
    
    Public Sub SaveToDB()
        Set Rs = New ADODB.Recordset
        Rs.Open "tbl_documents", Cn, adOpenKeyset, adLockPessimistic, adCmdTable
           
        With Rs
            .AddNew
            .Fields("Location") = strName & strFileExt
            .Fields("FileType") = strFileExt
            .Fields("Name") = cboName.Text
            .Update
        End With
          
          
        Rs.Close
        Set Rs = Nothing
    End Sub
    
    Here is my code for making the folder, copying the file and renaming it:

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How do I NOT overwrite files I am creating that have the same filename?

    Something like
    Code:
    Private Sub Command1_Click()
    If Dir$("c:\MyFile.Text") <> vbNullString Then
       'file exists so create a new one with a 1 appeneded to it
    Else
       'file doesn't exist so create as normal
    End If
    End Sub

  3. #3
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: How do I NOT overwrite files I am creating that have the same filename?

    Well to get you started you can use this to check if a file already exist or not .
    You will have to change it to use your names and variables

    'assuming already connected and
    'recordset already Dimmed and set

    Code:
            'check record exists 
            strSQL = "SELECT Field_Name From tbl_Name"
            strSQL = strSQL & " WHERE Field_Name = " & OriginalFileName
            rs.Open strSQL, cn, adOpenKeyset, adLockPessimistic, adCmdText
            
            If Not (rs.EOF And rs.BOF) Then
                    'There is record!
                    'Your code to add number to end of file name and then save
              Else
                    'There is no record!
                    'Your code to add file
            End If
            
            rs.Close
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  4. #4
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: How do I NOT overwrite files I am creating that have the same filename?

    Also, to get the number of records with the file name use this:
    Code:
    Dim lngNumRecs As Long
    
    strSQL = "Select Count(*) From tbl_Name WHERE Field_Name = '" & YourFileName & "'"
    
    rs.Open strSQL, cn, adOpenKeyset, adLockReadOnly, adCmdText 
    
    If Not rs.BOF And Not rs.EOF Then 
         lngNumRecs = rs.Fields(0).Value
    End IF
    Remember you will have to change the name of the file being searched for in case they are there and they would already have a number at the end, eg File1 or File 22
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  5. #5

    Thread Starter
    Lively Member koskilla's Avatar
    Join Date
    Jan 2007
    Location
    LA, California
    Posts
    79

    Re: How do I NOT overwrite files I am creating that have the same filename?

    I didn't even think to check the database for existing files. haha, that would make sense! thanks!

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