How To Copy Folder in Legacy Windows with Long File Names?


function copyFolderWithLongNames($sourceDirectory,$destinationDirectory){
    $sourceParent=split-path $sourceDirectory -parent
    $sourceChild=split-path $sourceDirectory -leaf
    $destinationParent=split-path $destinationDirectory -parent
    $destinationChild=split-path $destinationDirectory -leaf
    if($sourceParent.length -le $maxFolderLength -and $destinationParent.length -le $maxFolderLength){
        cmd /c mklink /J $sourceJunction $sourceParent # Local volumes are required as Source Directory
        cmd /c mklink /D $destinationJunction $destinationParent
        cmd /c robocopy "$sourceJunction\$sourceChild" "$destinationJunction\$destinationChild" /copyall /E /R:0 /Z /NP
    # Other methods that could also work
    #robocopy "\\?$sourceDirectory" "\\?$destinationDirectory"
    # OR
    #Subst X: $sourceDirectory
    #Subst Y: $destinationDirectory
    #robocopy X:\ Y:\ /copyall /E /R:0 /Z /NP
    #subst X: /d
    #subst Y: /d

copyFolderWithLongNames $sourceDirectory $destinationDirectory

Leave a Reply

Your email address will not be published. Required fields are marked *