Announcement

Collapse
No announcement yet.

rename files in folder using pbcc

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • rename files in folder using pbcc

    I want to flip through a specific folder and generically rename all files within this folder (topic0001, topic0002, etc). Currently the files in folder have names that are hard to follow and control.

    What do I need? sample program???

    (no. not dialog box but direct change)

    Thank you.

  • #2
    See the NAME keyword for renaming. If you want to do a certain group of files then also see DIR$ since you could use a loop and DIR$ to rename multiple files that match a certain filespec.
    Scott Slater
    Summit Computer Networks, Inc.
    www.summitcn.com

    Comment


    • #3
      Don't rename WITHIN a DO..LOOP! It screws up the DIR$ NEXT.

      Make a list (array) of files in the folder, then NAME them one at a time after DIR$ CLOSE.
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        Here's a crude example of what I am referring to. This has not been tested, as I am just writing it now.

        Code:
        ' This does not check sFolder, but it should be done.
        ' This routine was written for demo purposes only.
        ' sFolder needs to end with a trailing \ backslash
        Sub RenameAll(ByVal sFolder As String)
           
           Local dCount As Dword
           Local sFile  As String
           
           Do
              If dCount = 0 Then        ' Initialize Dir$
                 sFile = Dir$(sFolder & "*.EXT")  ' use whatever extension here or use * with caution
              Else
                 sFile = Dir$
              End If
              If Len(sFile) = 0 Then Exit Loop
              Incr dCount
              Name sFolder & sFile As sFolder & "topic" & Format$(dCount, "0000") & ".EXT"
              If Err Then Exit Loop
           Loop
            
        End Sub
        Scott Slater
        Summit Computer Networks, Inc.
        www.summitcn.com

        Comment


        • #5
          Originally posted by Michael Mattias View Post
          Don't rename WITHIN a DO..LOOP! It screws up the DIR$ NEXT.

          Make a list (array) of files in the folder, then NAME them one at a time after DIR$ CLOSE.
          Interesting, did not know that, nor have I ever had to do this. Well in that case use:

          Code:
          Sub RenameAll(ByVal sFolder As String)
             
             Local dCount As Dword
             Local idx    As Dword
             Local sFile  As String
             
             Dim sFileList() As String
             
             Do
                If dCount = 0 Then        ' Initialize Dir$
                   sFile = Dir$(sFolder & "*.EXT")  ' use whatever extension here or use * with caution
                Else
                   sFile = Dir$
                End If
                If Len(sFile) = 0 Then Exit Loop
                Incr dCount
                ReDim Preserve sFileList(1 To dCount) As String
                sFileList(dCount) = sFolder & sFile
             Loop
             
             For idx = 1 To dCount
                Name sFileList(idx) As sFolder & "topic" & Format$(idx, "0000") & ".EXT"
                If Err Then Exit For
             Next idx
             
          End Sub
          Last edited by Scott Slater; 18 Mar 2009, 10:26 AM.
          Scott Slater
          Summit Computer Networks, Inc.
          www.summitcn.com

          Comment


          • #6
            Also, be aware that the NAME statement does NOT support wild cards. You have to (re)name a specific file name to another specific file name.
            There are no atheists in a fox hole or the morning of a math test.
            If my flag offends you, I'll help you pack.

            Comment


            • #7
              Actually, anything which affects the directory - rename a file, add a file - can mess up a DIR$ or FindFirstFile/FindNextFile loop. I can't remember if deleting a file will as well, but after getting burned by rename and add, I don't even bother to play with that fire anymore.

              You can also do something like this...
              Code:
                SPEC$ = "*.ext" 
                DO 
                   S = DIR$(spec$)
                   IF LEN (S) THEN 
                       DIR$ CLOSE
                       NAME S AS NewName 
                   ELSE
                      EXIT DO 
                   END IF 
                 LOOP
              .. restarting the search after changing one of the names.
              Last edited by Michael Mattias; 18 Mar 2009, 10:47 AM. Reason: Added option
              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                Thank you.

                Thanks to all especially Scott and Michael for their help. I saw the dir$ close and read the warning. I remember working on AS400 and doing something that affected filenames (i think delete) and the program ran wild for hours continuing to re-read through a specific library. Maybe similar to rename/delete under DIR$.

                Comment

                Working...
                X