Results 1 to 6 of 6

Thread: [RESOLVED] Best method to append filename

  1. #1

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Resolved [RESOLVED] Best method to append filename

    I have a program that produces custom rtf files. These files are emailed and a copy is sent to a folder called "Emailed Documents"
    The fileName is in this format:
    Customer Lastname Customer Address Document Type ("Proposal").rtf or
    Doe 1256 Oak Ave Invoice.rtf This works good so far. Now I want to expand on this and allow another "Invoice" to be sent and not overwrite the original, and have copies of both or maybe even 3 or more. So for the second Invoice it would be:
    Doe 1256 Oak Ave Invoice[2].rtf
    3rd invoice:
    Doe 1256 Oak Ave Invoice[3].rtf
    etc
    This name appending would have to be done before emailing as to not send duplicate attachements. heres where i need help.
    Pull all the files into a listbox and code against that or an array or ?
    Then to determine how to search and append the file name. Split ?
    Right ? Mid? or ?
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Best method to append filename

    Code:
        Dim strFile As String
        Dim strNewFileName As String
        Dim intFileNbr As Integer
    
        intFileNbr = 2
        strNewFileName = "Doe 1256 Oak Ave Invoice.rtf" ' This needs the path prepended to it
        strFile = Dir$(strNewFileName)
        Do While Len(strFile)
            DoEvents
            strFile = Dir$
            strNewFileName = strNewFileName & "[" & intFileNbr & "]"
            intFileNbr = intFileNbr + 1
        Loop
    
        MsgBox strNewFileName

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Best method to append filename

    I think a simple loop with the Dir$() function can do the trick.
    Note, you should ensure that your file name does not include invalid filename characters
    Code:
    Private Sub Command1_Click()
        Dim myFile As String
        myFile = GetUniqueFileName("C:\Windows\Temp", "Doe 1256 Oak Ave Invoice.rtf")
        Open myFile For Output As #1
        Close #1
    End Sub
    
    Private Function GetUniqueFileName(ByVal Path As String, ByVal FileNameBase As String) As String
    
        Dim uniqueID As Long
        Dim sFile As String
        Dim sExt As Long, iExt As Integer
        
        If Right$(Path, 1) <> "\" Then Path = Path & "\"
        sFile = Dir$(Path & FileNameBase, vbArchive)
        If sFile = vbNullString Then
            GetUniqueFileName = Path & FileNameBase
        Else
            iExt = InStrRev(FileNameBase, ".")
            If iExt = 0 Then
                FileNameBase = FileNameBase & "[#]"
            Else
                FileNameBase = Left$(FileNameBase, iExt - 1) & "[#]" & Mid$(FileNameBase, iExt)
            End If
            Do 
                uniqueID = uniqueID + 1
                sFile = Dir$(Path & Replace$(FileNameBase, "#", uniqueID), vbArchive)
            Loop Until sFile = vbNullString
            GetUniqueFileName = Path & Replace$(FileNameBase, "#", uniqueID)
        End If
    
    End Function
    Last edited by LaVolpe; Nov 19th, 2008 at 09:46 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Best method to append filename

    I usually use this:
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        Dim myFile As String
        
        myFile = UniqueFullFileName("C:\Windows\Temp", "Doe 1256 Oak Ave Invoice", ".rtf")
        Debug.Print myFile
        Open myFile For Output As #1
        Close #1
    End Sub
    
    Function UniqueFullFileName(sPath As String, sBaseName As String, sExt As String) As String
        Dim i As Long
        '-- Don't care, sPath may or may not ending with "\"
        UniqueFullFileName = sPath & "\" & sBaseName & sExt
        Do While Len(Dir$(UniqueFullFileName, 55))
        '-- 55 = vbArchive Or vbDirectory Or vbHidden Or vbReadOnly Or vbSystem
            i = i + 1
            UniqueFullFileName = sPath & "\" & sBaseName & "[" & i & "]" & sExt
        Loop
    End Function
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  5. #5

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: Best method to append filename

    Thanks guys thats easier than i thought. With a little modifaction they will work as i am passing the whole path with filename. Martin a small thing the .rtf was not at the end as person recieving the document can just click it and be opened with Word or WordPad
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  6. #6

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