0

I am trying to make a copy of a PDF file from a network drive (sourceFile) to the user's Temp folder but there is a good chance that the path of sourceFile is longer than 260 characters.

I tried to adopt ChipsLetten's answer in this question and modify it to perform CopyFile but it returns Error 5: Invalid procedure call or argument.

Private Sub Test_CopyFile()

    Const testfolder As String = "\\servername\folder1\folder2\folder3\folder4\folder5\folder6"
    Const testfile As String = "superlongfilename.pdf"
    Const sourceFile As String = testfolder & "\" & testfile 'Actual test path is 280 characters long
    
    Dim destinationPath As String: destinationPath = Environ("Temp") & "\" & testfile
        
    Debug.Print LongFileCopy(sourceFile, destinationPath)
End Sub

Private Function LongFileCopy(pathCopy As String, pathDestination As String) As Boolean
    Dim fsoObject As Scripting.FileSystemObject
    Const MAX_PATH_LENGTH As Integer = 260

    If Len(pathCopy) > MAX_PATH_LENGTH Then
        pathCopy = "\\?\UNC\" & Mid$(pathCopy, 3)
    End If
    Set fsoObject = New Scripting.FileSystemObject
    If fsoObject.FileExists(pathCopy) Then
        fsoObject.CopyFile pathCopy, pathDestination, True  'Error 5: Invalid procedure or argument at this line
        LongFileCopy = True
    End If
End Function

Does anyone have any solution to this? Thanks in advance!

Raymond Wu
  • 3,357
  • 2
  • 7
  • 20
  • You can use the [SHFileOperation](http://allapi.mentalis.org/apilist/SHFileOperation.shtml) API for this. I just tested it on a 281 character path and it worked. [HERE](https://stackoverflow.com/questions/14227172/copy-files-with-progress-bar) is an example on how to use it. – Siddharth Rout May 24 '21 at 04:48
  • Thanks for the quick response! I just tried it but nothing seems to be copied. I did a Debug.Print on SHFileOperation and it returned 2. Quick google says it's an error for file not found but I confirm the path is correct (even ChipsLetten's answer returns True). – Raymond Wu May 24 '21 at 05:04
  • I created a very long folder in in windows temp directory `C:\Windows\Temp\New test Folder1\New test Folder2\New test Folder3\New test Folder4\New test Folder5\New test Folder6\New test Folder7\New test Folder8\New test Folder9\New test Folder10\New test Folder11\New test Folder12\New test Folder13` and then manually copied a file from desktop `New super long name of this document.txt` in this folder. Then I simply used the above code to create a copy of this file in this folder and it worked. – Siddharth Rout May 24 '21 at 05:10
  • Does the `pathDestination` exist? – Siddharth Rout May 24 '21 at 05:12
  • TEST2: Copied `C:\Users\routs\Desktop\New super long name of this document.txt` to `C:\Windows\Temp\New test Folder1\New test Folder2\New test Folder3\New test Folder4\New test Folder5\New test Folder6\New test Folder7\New test Folder8\New test Folder9\New test Folder10\New test Folder11\New test Folder12\New test Folder13\New super long name of this documentA.txt` with the above code and it works. – Siddharth Rout May 24 '21 at 05:19
  • `pathDestination` doesn't exist, the actual implementation resides in a folder within the Temp folder and it's always empty. I'm not sure if it matters but in your test, the long path is the destination while mine is the source. – Raymond Wu May 24 '21 at 05:38
  • `pathDestination doesn't exist` In that case, create the destination folder using MKDIR and then copy the file. it will work :) – Siddharth Rout May 24 '21 at 05:40
  • Sorry but that's not what I meant, the folder to `pathDestination` (which is Environ("Temp")) do exist but not the file itself i.e. no case of overwrite needed. I did another test but using a `sourceFile` of 98 character path (but in the same UNC path) and it worked. – Raymond Wu May 24 '21 at 05:44
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/232793/discussion-between-siddharth-rout-and-raymond-wu). – Siddharth Rout May 24 '21 at 05:44

0 Answers0