Results 1 to 5 of 5
  1. #1
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    340

    Using fso

    I'm tying to move a SubFolder and it's contents with
    FSO.MoveFolder Source:=FromPath, Destination:=ToPath
    Mindful of the rule It is not possible to use a folder that exist in ToPath

    However if I don't make the path Access tells me Path not found, and if I do make it it says Permission denied.
    Anyone know how to resolve this ?My fromPath is "F:\Library\1959\Smith\001\Texts"
    toPath is "P:\Backup\Library\1959\Smith\001\texts"

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716

  3. #3
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    340
    Rather frustrating, in spite of the claims in that link his routines does not work, at least in this case.
    It generates error 76 so I added this to his error handler
    Code:
    If Not Dir(sFolderSource, vbDirectory) = "" Then
            If GetAttr(sFolderSource) And vbDirectory = vbDirectory Then
                Debug.Print "The 'Source Folder' = True"
            End If
    end If
    Now the output is
    The 'Source Folder' = True
    The 'Source Folder' could not be found to make a copy of.
    I can probably devise some other way to do the job, but if anyone can explain this ambiguity it would be much appreciated.

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716
    Middlemarch,

    My intent by showing Daniel's article was only that he had info indicating fso.movefolder did not work.

    I have found a few links that show fso.movefolder - none works?? All give error
    https://analystcave.com/vba-filesyst...ba-movefolder/
    https://www.devguru.com/content/tech...ovefolder.html

    Other people had issues/couldn't get MoveFolder to work
    see this stackoverflow thread

    The code below did move all files from my source to my destination folder.
    **Note: This was a few files from "c:\test" to "c:\dest".
    I separately created c:\dest so commented the MkDir within the code.
    The files were moved to dest folder and removed from test folder -- and no errors.

    I realize this does not handle subfolders, but

    Code:
    Sub FSOMoveAllFiles()
        Dim FSO As New FileSystemObject
        Dim FromPath As String
        Dim ToPath As String
        Dim FileInFromFolder As Object
    
        FromPath = "C:\test\"
       ' MkDir "C:\Dst\"
        ToPath = "C:\Dest\"
        
        Set FSO = CreateObject("Scripting.FileSystemObject")
    
        For Each FileInFromFolder In FSO.GetFolder(FromPath).files
        Debug.Print "Moving  " & FileInFromFolder.name
            FileInFromFolder.Move ToPath
        Next FileInFromFolder
    
    End Sub
    UPDATE: After running the code above, I went back to Daniel's sample code to copy then delete folder.
    I changed the source and destination folders as below. The files were moved to test and dest was deleted.
    No errors!!
    Which is confusing???

    Code:
    ' Procedure : MoveFolder
    ' Author    : CARDA Consultants Inc.
    ' Website   : http://www.cardaconsultants.com
    ' Purpose   : Move a folder
    '             Better version of the FSO's MoveFolder method which is basically a "rename"
    '             method, hence it only works if the source and destination reside on
    '             the same volume (same as move.exe under WinXP) and typically returns
    '             a permission denied error.
    ' Copyright : The following may be altered and reused as you wish so long as the
    '             copyright notice is left unchanged (including Author, Website and
    '             Copyright).  It may not be sold/resold or reposted on other sites (links
    '             back to this site are allowed).
    '
    ' Input Variables:
    ' ~~~~~~~~~~~~~~~~
    ' sFolderSource         Folder to move
    ' sFolderDestination    Folder to move the folder to
    ' bOverWriteFiles       Whether to overwrite file(s) if the folder already exists
    '
    ' Usage Example:
    ' ~~~~~~~~~~~~~~~~
    ' MoveFolder("C:\Temp", "D:\Development\New")
    '
    ' Revision History:
    ' Rev       Date(yyyy/mm/dd)        Description
    ' **************************************************************************************
    ' 1         2010-Nov-14                 Initial Release
    '---------------------------------------------------------------------------------------
    Function MoveFolder(sFolderSource As String, sFolderDestination As String, _
                        bOverWriteFiles As Boolean) As Boolean
    On Error GoTo Error_Handler
        Dim fs As Object
        
        MoveFolder = False
        Set fs = CreateObject("Scripting.FileSystemObject")
        fs.CopyFolder sFolderSource, sFolderDestination, bOverWriteFiles
        fs.DeleteFolder sFolderSource, True
        MoveFolder = True
    
    Error_Handler_Exit:
        On Error Resume Next
        Set fs = Nothing
        Exit Function
    
    Error_Handler:
        If Err.Number = 76 Then
         MsgBox "The 'Source Folder' could not be found to make a copy of.", _
                    vbCritical, "Unable to Find the Specified Folder"
        Else
            MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
                   "Error Number: " & Err.Number & vbCrLf & _
                   "Error Source: MoveFolder" & vbCrLf & _
                   "Error Description: " & Err.Description, _
                   vbCritical, "An Error has Occurred!"
        End If
        Resume Error_Handler_Exit
    End Function

    Switched source and destination for this attempt.
    Code:
    Sub danielMove()
        Dim src As Folder
        Dim dest As Folder
        MoveFolder "c:\dest", "c:\test", True
    End Sub
    Further: I then recreated c:\dest, switched the source and destinaton folders and ran Daniel's code again.
    All files moved to c:Test and cest was deleted --No errors???


    Then, to test with a subfolder to confirm that my findings were real.

    I then added a subfolder DestSub1 to c:\Dest and added a few files to the subfolder. I again separately created c:\Test.
    Then reversed the source and destination folder names and ran Daniel's code again.

    Code:
    Sub danielMove()
        Dim src As Folder
        Dim dest As Folder
        MoveFolder "c:\dest", "c:\test", True
    End Sub
    And it worked without errors. Moved folder and subfolder and all files to c:\test and c:\test\destsub1

    ****My only suggestion is to try Daniel's code again, and let us know your result.

  5. #5
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    340
    Thanks for your help. Subfolders were the problem I believe. Mission was to move a subfolder on Drive F: to a subfolder in a different path on Drive P:
    After making the path I could then use Daniels routine, but had to move everything up one folder. All very messy. Then Kill that extra folder.. using RmDir instead of Kill (that drove me batty for half an hour)

Please reply to this thread with any new information or opinions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums