Rename a part of a folder (1 Viewer)

habiler

Registered User.
Local time
Today, 21:24
Joined
Aug 10, 2014
Messages
70
Good morning, everyone,

I would like to standardize the labels of my files. So I have a folder

c: Staff Eric Lepetit (123) folder1 Folder2 Folderx ABC.txt
c: Personnel John Medium 456 folder1 Folder2 Folderx DEF.txt
c: Staff Matt Eric Lepetit folder1 Folder2 Folderx GHI.txt

And I would like to have as a result

c:\Staff\Eric Lepetit (123)\folder1\Folder2\Folderx\ABC.txt
c:\Staff\John Medium (456)\folder1\Folder2\Folderx\DEF.txt
c:\Staff\Matt lesage (789)\folder1\Folder2\Folderx\GHI.txt

To do this I have an access table (DEC) with NomNameMatr field (ex: Eric Lepetit (123))

In fact it would be a rename of
c:\Staff\& 'NomNameMatr'&\folder1\Folder2\Folderx\xxx.txt
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:24
Joined
Oct 29, 2018
Messages
21,575
Hi. With over 20 views with no response, I may not be the only one confused by your question. Would you mind elaborating a little bit more? Thanks.
 

habiler

Registered User.
Local time
Today, 21:24
Joined
Aug 10, 2014
Messages
70
Moving a folder a folder

Now a try to create or rename a folder if not exists. Thats OK. But if the folder don't exist move him to a another folder d
but that s cant i do. I use fso .


Code:
Private Sub RenFile_Click()
Dim fso As Object
    Dim rootPath As String, destPath As String, strPath As String, strPathNomName As String, strPathNomName1, strPathDecisions, strSql As String
    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset
 
   rootPath = "C:\Personnel\"    '<< Change
   destPath = "C:\Pers\"    '<< Change
  
    Set dbs = CurrentDb
    Set fso = CreateObject("Scripting.FileSystemObject")
'
    strSql = "SELECT Matr, NomName, NomName1,NomNameMatr FROM DEC"
    strSqlWkn = "SELECT Matr, NomName, NomName1,NomNameMatr FROM DEC"
    ' NomName = Eric LePetit
    ' NomName1 = Eric LePetit 3478
    ' NomNameMatr = Eric LePetit (3478)
       
    
    
    Debug.Print destPath
    
    Set rst = dbs.OpenRecordset(strSql, dbOpenSnapshot, dbFailOnError)
  '  Set fso = CreateObject("Scripting.Filesystemobject")
    Set destpathFolder = fso.GetFolder(destPath)
           With rst

        If rst.RecordCount = 0 Then
            MsgBox "No files to transfer"
        Else
            Do While Not rst.EOF
            
            strPath = rootPath & !NomNameMatr
            strPathNomName = rootPath & !NomName
            strPathNomName1 = rootPath & !NomName1
            
            If Len(Dir(strPathNomName1, vbDirectory)) <> 0 Then
            MsgBox " Directory C:\Personnel\NomName1 finded"
            Name strPathNomName1 As strPath
            DeleteFile = strPathNomName1
            
            Else
            
            If Len(Dir(strPathNomName, vbDirectory)) <> 0 Then
            MsgBox " Directory C:\Personnel\NomName finded"
            Name strPathNomName As strPath
            DeleteFile = strPathNomName
            
            Else
            
            strPath = rootPath & !NomNameMatr
            If Len(Dir(strPath, vbDirectory)) = 0 Then
            MsgBox " Directory introuvable"
            MkDir strPath
           'Set destpathFolder = fso.GetFolder(destPath)
            
           fso.MoveFile source:=strPath, destination:=destPath
            End If
            
            strPathDecisions = rootPath & !NomNameMatr & "\" & "Cessation de fonction"
            If Len(Dir(strPathDecisions, vbDirectory)) = 0 Then
            MsgBox " Directory strPathDecisions introuvable donc je la cr?e"
            MkDir strPathDecisions
            End If
            .MoveNext
            End If
            '.MoveNext
            End If
           ' .MoveNext
            Loop
      
       End If
   End With
    Close
    Set fso = Nothing
    Set rst = Nothing
    Set dbs = Nothing
End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:24
Joined
Sep 21, 2011
Messages
14,521
Well done for using code tags, but I could not make hide or hare of your indentation?:eek:

From the link I posted you can rename the folder BUT must be in the parent folder.?

So first you will need to rename C:\Personel\ to C:\Pers\
Then use the newly named top folder from then on.

Try it manually first before automating it.

HTH


Code:
Private Sub RenFile_Click()
    Dim fso As Object
    Dim rootPath As String, destPath As String, strPath As String, strPathNomName As String, strPathNomName1, strPathDecisions, strSql As String
    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset

    rootPath = "C:\Personnel\"    '<< Change
    destPath = "C:\Pers\"    '<< Change

    Set dbs = CurrentDb
    Set fso = CreateObject("Scripting.FileSystemObject")
    '
    strSql = "SELECT Matr, NomName, NomName1,NomNameMatr FROM DEC"
    strSqlWkn = "SELECT Matr, NomName, NomName1,NomNameMatr FROM DEC"
    ' NomName = Eric LePetit
    ' NomName1 = Eric LePetit 3478
    ' NomNameMatr = Eric LePetit (3478)



    Debug.Print destPath

    Set rst = dbs.OpenRecordset(strSql, dbOpenSnapshot, dbFailOnError)
    '  Set fso = CreateObject("Scripting.Filesystemobject")
    Set destpathFolder = fso.GetFolder(destPath)
    With rst

        If rst.RecordCount = 0 Then
            MsgBox "No files to transfer"
        Else
            Do While Not rst.EOF

                strPath = rootPath & !NomNameMatr
                strPathNomName = rootPath & !NomName
                strPathNomName1 = rootPath & !NomName1

                If Len(Dir(strPathNomName1, vbDirectory)) <> 0 Then
                    MsgBox " Directory C:\Personnel\NomName1 finded"
                    Name strPathNomName1 As strPath
                    DeleteFile = strPathNomName1

                Else

                    If Len(Dir(strPathNomName, vbDirectory)) <> 0 Then
                        MsgBox " Directory C:\Personnel\NomName finded"
                        Name strPathNomName As strPath
                        DeleteFile = strPathNomName

                    Else

                        strPath = rootPath & !NomNameMatr
                        If Len(Dir(strPath, vbDirectory)) = 0 Then
                            MsgBox " Directory introuvable"
                            MkDir strPath
                            'Set destpathFolder = fso.GetFolder(destPath)

                            fso.MoveFile source:=strPath, Destination:=destPath
                        End If

                        strPathDecisions = rootPath & !NomNameMatr & "\" & "Cessation de fonction"
                        If Len(Dir(strPathDecisions, vbDirectory)) = 0 Then
                            MsgBox " Directory strPathDecisions introuvable donc je la cr?e"
                            MkDir strPathDecisions
                        End If
                        .MoveNext
                    End If
                    '.MoveNext
                End If
                ' .MoveNext
            Loop

        End If
    End With
    Close
    Set fso = Nothing
    Set rst = Nothing
    Set dbs = Nothing
End Sub
 

habiler

Registered User.
Local time
Today, 21:24
Joined
Aug 10, 2014
Messages
70
I'm sorry Gasman but i don't understand your devlopment:confused:
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:24
Joined
Sep 21, 2011
Messages
14,521
From what I have read and tested, you must rename the folders like this

C:\Personnel to C:\Pers
Then you can rename the folders in that newly named folder

c:\Pers\Eric LePetit 3478 to C:\Pers\Eric LePetit (3478)

If there was a folder in that perhaps called Documents and you wanted to rename to Docs, then you would need to be in C:\Pers\Eric LePetit (3478) before you could do that.

It appears you can only rename a folder if you are in it's parent folder.

Any clearer.?
 

habiler

Registered User.
Local time
Today, 21:24
Joined
Aug 10, 2014
Messages
70
OK, I created my folder immediately in Pers
 

habiler

Registered User.
Local time
Today, 21:24
Joined
Aug 10, 2014
Messages
70
But the problem is that I have to move the subfolders also
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:24
Joined
Sep 21, 2011
Messages
14,521
Ok, I managed to get this to work.
It will take folder db in c:\temp and put in in c:\temp\test as dbs. Test needs to exist.

All the files in db stay in their relative positions.

Hopefully you can adjust to suit.?
This is how I normally work things out. Try it manually or with fixed values. Inspect the code variables as the code runs. Slow I admit, but it works for me.

When I get it working, I change it for variables.

HTH

Code:
Sub RenFolder()
    Dim fso As Object
    Dim rootPath As String, destpath As String, strPath As String, strPathNomName As String, strPathName As String, strSql As String
    Dim DestpathFolder As Object
    
    rootPath = "C:\Temp\"    '<< Change
    destpath = "C:\Temp\Test\"    '<< Change
    strPathNomName = "DB"
    strPathName = "DBS"
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    '
    Debug.Print destpath

    'Set DestpathFolder = fso.GetFolder(rootPath)
    Name rootPath & strPathNomName As destpath & strPathName

    Set fso = Nothing
    Set DestpathFolder = Nothing
    
End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:24
Joined
Sep 21, 2011
Messages
14,521
Well it appears you can rename a whole path with fso.

I just renamed "c:\Test\db" to "C:\Temp\Test\Test1\DBS" using the code and a few amendments for the correct path.

Make sure you have trailing \ for your folders.?
 

Users who are viewing this thread

Top Bottom