File System Object - Rename Folders

Mile-O

Back once again...
Local time
Today, 14:43
Joined
Dec 10, 2002
Messages
11,316
I'm taking a look through the File System Object (requires MicroSoft Scripting Runtime .dll) and can find everything I need to move files/folders, delte them, create them, etc. but I can't see a method to rename files and folders.

Does anyone know how this is achieved?
 
movefile? will move from one name to the other:D :p :rolleyes:
 
Worked it out. This will move and rename a folder - there, obviously, will be a need for a few edits should someone else wish to use it.

Code:
Private Sub cmdArchive_Click()

    On Error GoTo Err_cmdArchive_Click

    Const OrigFolder As String = "MyFolder"
    Const Archive As String = "Archive/"

    Dim fso As FileSystemObject
    Dim fsoArchive As Folder
    Dim fsoFolder As Folder
    
    Dim strSource As String
    Dim strDest As String
    
    Set fso = New FileSystemObject
    Set fsoArchive = fso.GetFolder(QuoteStore & Archive)
    
    strSource = QuoteStore & OrigFolder
    strDest = QuoteStore & Archive
    
    If MsgBox("Are you sure you wish to archive the folder '" & OrigFolder & "'?", vbQuestion + vbYesNo) = vbYes Then
        With fso
            .MoveFolder strSource, strDest
            For Each fsoFolder In fsoArchive.SubFolders
                If fsoFolder.Name = OrigFolder Then
                    fsoFolder.Name = fsoFolder.Name & "-" & Format(Now, "yyyymmddhhnnss")
                End If
            Next
        End With
    End If
    
Exit_cmdArchive_Click:
    strSource = vbNullString
    strDest = vbNullString
    Set fso = Nothing
    Set fsoFolder = Nothing
    Set fsoArchive = Nothing
    Exit Sub
    
Err_cmdArchive_Click:
    MsgBox Err.Description, vbExclamation, "Error #" & Err.Number
    Resume Exit_cmdArchive_Click
    
End Sub
 
I can't see a method to rename files and folders.
Check the Access VBA help files for the Name Statement.

Renames a disk file, directory, or folder.

Syntax

Name oldpathname As newpathname

The Name statement syntax has these parts:

Part Description
oldpathname Required. String expression that specifies the existing file name and location — may include directory or folder, and drive.
newpathname Required. String expression that specifies the new file name and location — may include directory or folder, and drive. The file name specified by newpathname can't already exist.

Remarks

The Name statement renames a file and moves it to a different directory or folder, if necessary. Name can move a file across drives, but it can only rename an existing directory or folder when both newpathname and oldpathname are located on the same drive. Name cannot create a new file, directory, or folder.

Using Name on an open file produces an error. You must close an open file before renaming it. Name arguments cannot include multiple-character (*) and single-character (?) wildcards.
 
Milo:

This may appear to be a stupid question, but what does QuoteStore and Archive refer to in your code?
 
I got that, but where in the code is QuoteStore set to anything?
 

Users who are viewing this thread

Back
Top Bottom