Results 1 to 8 of 8

Thread: Saving multiple files with same name, different number

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2000
    Location
    West Monroe, Louisiana
    Posts
    33
    I have written a small special purpose text editor that I use to prepare draft copies of text documents in the law office where I work after school. It works great, but I need to add a feature to it, if possible.

    A legal document may have a newly edited draft each day for several days. I am required to save these progressive drafts by the same name with a progressing number. i.e. Excell01, Excell02, Excell03, etc.

    My question: Can someone please give me a sample code that I can include in my text editor that will allow me, when saving a file, to type only the file name (without the number) and have the program look up and add the next available number to the end of the name. It's often difficult and time consuming to find the next available number.

    Please help if you can!

    Thanks!

    Wendy

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539

    Talking run a loop

    run a loop that will add numbers to end of file
    example
    Code:
    for counter = 1 to 10
       fileName = fileName & Format(Counter, "00")
    next counter
    you sould get
    Excell01
    Excell02
    Excell03
    Excell04
    Excell05
    Excell06
    Excell07
    Excell08
    Excell09
    Excell10

    hope that helps

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2000
    Location
    West Monroe, Louisiana
    Posts
    33
    Thanks, Kovan.

    I think this is what I need. I'll give it a try and let you know how I did with it. It may be a few days before I get it to work, though!

    Thanks again,

    Wendy

  4. #4
    Lively Member
    Join Date
    Aug 2000
    Location
    Ontario, Canada
    Posts
    79
    Hi Wendy, you can try this as well if you want. I didn't get a chance to test this, but I am aiming at this to search a directory for a filename base and it will increment it using a 000 numbering scheme. Try it, change it... Play with it, just a guess that this will work.

    Code:
    Private Function GetName(ByVal sDir As String, ByVal sFileName As String) As String
    
        On Error GoTo NumErr
    
        Dim sProc As String
        Dim x As Integer
        
        sProc = Dir(sDir, vbDirectory)
        Do While sProc <> ""
            If InStr(1, sProc, Left(sFileName, InStr(1, sFileName, ".") - 1)) > 0 And _
               InStr(1, sProc, Mid(sFileName, InStr(1, sFileName, "."), Len(sFileName))) > 0 Then
                x = CInt(Mid(sProc, Len(Trim(sProc)) - 6, 3))
                x = x + 1
                GetName = Left(sFileName, InStr(1, sFileName, ".") - 1) & _
                          CStr(Format(x, "000")) & Mid(sFileName, InStr(1, sFileName, "."), Len(sFileName))
            End If
            sProc = Dir
        Loop
        If GetName = "" Then GetName = sFileName
        
    NumErr:
        x = 0
        Resume Next
        
    End Function
    
    
    'Then Just call it using an example like the following:
    
    Private Sub Command1_Click()
    
        MsgBox (GetName("c:\temp\", "test.txt"))
    
    End Sub





    [Edited by CoreyS on 08-30-2000 at 01:18 PM]

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2000
    Location
    West Monroe, Louisiana
    Posts
    33
    Thanks Corey!

    I'll check this out later tonight if I can get to it. Looks like great code. I'll let you know how it goes.

    Wendy

  6. #6
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    'this assumes the files in the folder are assigned
    'with 3 numbers as last sequence of the naming convention
    'example..file001,file002,file003 or whatever name
    'but always a 3 digit number
    
    'will work for files ranging from 001 to 999
    
    
    Option Explicit
    
    'this is used to sort and array by alpha
    
    Sub iSort(iArray As Variant)
         
         Dim Loop1 As Long
         Dim Loop2 As Long
         Dim Temp As String
      
         For Loop1 = UBound(iArray) To LBound(iArray) Step -1
           For Loop2 = LBound(iArray) + 1 To Loop1
    
             If iArray(Loop2 - 1) > iArray(Loop2) Then
               Temp = iArray(Loop2 - 1)
               iArray(Loop2 - 1) = iArray(Loop2)
               iArray(Loop2) = Temp
             End If
           Next Loop2
         Next Loop1
       End Sub
    
    Private Sub Command1_Click()
    
        Dim stFile As String
        Dim sDir As String
        Dim i As Integer
        Dim myArr()
        Dim myLen As Integer
    'the folder I used for testing this was C:\abc
    'you change it to your folder
    
        stFile = Dir$("c:\abc\*.*")
        
    'loop through the folder and get the names of all files
        Do While stFile <> ""
           myLen = Len(stFile) 'used in left string function
    
    'redim and preserve the array as it's inside a loop
           ReDim Preserve myArr(i)
           
    'take the .ext from the filename
          myArr(i) = Left(stFile, myLen - 4)
          
    'increment for the array index
          i = i + 1
          
    'next file in directory
          stFile = Dir
          
        Loop
        
    'we now have the filenames so we sort them to put
    'the last number on the bottom
        Call iSort(myArr)
        
    'now we need to get the last 3 digits from the filename
    'we need this because the filename is a string and you
    'cannot add to a string so we extract them and edit them
        
        Dim NewFile As String
    'newfile = the last item after we sort
        NewFile = myArr(UBound(myArr))
    'now we take the last 3 digits from it's name
        NewFile = Right(NewFile, 3)
    'we change the string to an integer so we can add
        NewFile = CInt(NewFile)
    'we add 1 to the number we extracted
        NewFile = NewFile + 1
    'we fromat it to show 3 characters at all time
        NewFile = Format(NewFile, "000")
    'we display the number you will need to use
        MsgBox "My new number is " & NewFile
    
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  7. #7
    Member
    Join Date
    Jul 2000
    Location
    BFE in Oregon
    Posts
    33
    You can add text entries together as long as they're stored in variant variables.

    dim NewFile as variant
    NewFile=Format(Right$(NewFile, 3), "000") + 1

    Use of variants isn't good practice but it makes this code smaller (not enough to notice and perhaps harder to decypher). I didn't test the above 2 lines but in theory it will work.

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2000
    Location
    West Monroe, Louisiana
    Posts
    33
    WOW!

    Thanks to all you guys! It's evident that each of you put a lot of work and thought into this. I really appreciate you all helping me out in this. I'll drop you each an email when I get it working. (Tonight I study, tomorrow I go to classes and then to work. Not much time for coding!)

    Thanks again!

    Wendy

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