[AccessD] Rename files

A.D.TEJPAL adtp at hotmail.com
Thu Feb 8 05:52:59 CST 2007


Darren,

    Sample subroutine  P_AddPrefixSuffixToFileNames() as given below, will insert the desired prefix and / or suffix in all target files in the specified folder (including all subfolders belonging to this folder).

    It is a generic procedure with recursive feature (ensuring coverage of all subfolders as well). First argument provides the path of folder containing the files in question, while next three arguments are all optional. If last argument (file extn) is missing, all files get renamed. Otherwise, only the files with specified extn get affected.

    Sample call for inserting "inv103_" as prefix & "_016" as suffix in all pdf type files in the target folders, as desired by you, would be as follows:

        P_AddPrefixSuffixToFileNames  _
            "<<MyFolderPath>>", _
            "inv103_", "_016", ".pdf"

    Note - If only prefix or only suffix is required, the unwanted  optional argument can be omitted (If  such optional argument is not the last one, corresponding place holder comma should be provided).

Best wishes,
A.D.Tejpal
---------------

Sample SubRoutine
(Insertion of Prefix & Suffix in File Names)
=====================================
Sub P_AddPrefixSuffixToFileNames( _
                        ByVal FolderPath As String, _
                        Optional Prefix As Variant, _
                        Optional Suffix As Variant, _
                        Optional Extn As Variant)
    ' Inserts desired Prefix & Suffix in File Names
    ' (For all files in the target folder, including all its
    ' subfolders). If Extn has been specified, only
    ' files with this particular extn will get renamed.
    On Error GoTo ErrTrap
    Dim fso As FileSystemObject
    Dim pfd As Folder, sfd As Folder
    Dim fe As File
    
    Dim BaseName As String, FileExtn As String
    Dim Rtv As Variant, NewPath As String
    Dim Pfx As String, Sfx As String, Exn As String
    Dim Lpx As Long, Lsx As Long
    
    ' Translate optional arguments into strings
    Pfx = IIf(IsMissing(Prefix), "", Nz(Prefix, ""))
    Sfx = IIf(IsMissing(Suffix), "", Nz(Suffix, ""))
    Exn = IIf(IsMissing(Extn), "", Nz(Extn, ""))
    
    ' Exit if no prefix & suffix specified
    If Pfx = "" And Sfx = "" Then
        GoTo ExitPoint
    End If
    Lpx = Len(Pfx)
    Lsx = Len(Sfx)
    
    Set fso = New FileSystemObject
    Set pfd = fso.GetFolder(FolderPath)
    
    ' Provide dot at beginning of extn argument
    ' (if such dot is missing)
    Exn = IIf(Left(Exn, 1) = ".", "", ".") & Exn
    
    ' Cycle through the files in parent folder
    For Each fe In pfd.Files
        Rtv = Split(fe.Name, ".")
        BaseName = Rtv(0)
        FileExtn = "." & Rtv(1)
        ' Skip Renaming if already renamed      ' (A)
        If (Lpx > 0 And Left(BaseName, _
                    Lpx) = Pfx) Or (Lsx > 0 And _
                    Right(BaseName, Lsx) = Sfx) Then
            GoTo Skip
        End If
        
        NewPath = pfd.Path & "\" & Pfx & _
                        BaseName & Sfx & FileExtn
        If IsMissing(Extn) Then
            Name fe.Path As NewPath
        Else
            If FileExtn = Exn Then
                Name fe.Path As NewPath
            End If
        End If
Skip:
    Next
    
    ' Note - (A) is meant to prevent repeat action
    '            on same file. (In a For Each loop, an
    '            object's index position can get disturbed
    '            after renaming,, leading to repeat processing)
    
    ' Cycle through the files in all subfolders
    ' contained in current parent folder
    If pfd.SubFolders.Count > 0 Then
        For Each sfd In pfd.SubFolders
            If IsMissing(Extn) Then
                P_AddPrefixSuffixToFileNames sfd.Path, _
                                                Prefix, Suffix
            Else
                P_AddPrefixSuffixToFileNames sfd.Path, _
                                                Prefix, Suffix, Extn
            End If
        Next
    End If

ExitPoint:
    On Error Resume Next
    Set fe = Nothing
    Set sfd = Nothing
    Set pfd = Nothing
    Set fso = Nothing
    On Error GoTo 0
    Exit Sub
        
ErrTrap:
    MsgBox Err.Number & " - " & Err.Description
    Resume ExitPoint
End Sub
=====================================


-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Darren DICK
Sent: Wednesday, February 07, 2007 6:30 AM
To: 'Access Developers discussion and problem solving'
Subject: [AccessD] Rename files

Hi All

I have a renaming routine but can't quite work out the syntax
I have file names in a folder similar to

10.pdf
11.pdf
12.pdf
13.pdf

etc etc (All sequential) 

I want to prefix each file name in the nominated folder with "inv103" 
I can do this bit - no prob 
But I want to insert "-016" before the .PDF bit 
So final filename is like.

inv10310-016.pdf
inv10311-016.pdf
inv10312-016.pdf
inv10313-016.pdf 

and so on 

Any suggestions? 

Many thanks in advance 
Darren



More information about the AccessD mailing list