Posted On June 25, 2021

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

kimconnect 0 comments
blog.KimConnect.com >> Codes >> How To Copy Folder in Legacy Windows with Long File Names?
$sourceDirectory='D:\SMBSHARE\LONGPATH'
$destinationDirectory='\\FILESERVER\LONGPATH'

function copyFolderWithLongNames($sourceDirectory,$destinationDirectory){
    $sourceParent=split-path $sourceDirectory -parent
    $sourceChild=split-path $sourceDirectory -leaf
    $destinationParent=split-path $destinationDirectory -parent
    $destinationChild=split-path $destinationDirectory -leaf
    $maxFolderLength=248
    if($sourceParent.length -le $maxFolderLength -and $destinationParent.length -le $maxFolderLength){
        $sourceJunction='C:\Source'
        $destinationJunction='C:\Destination'
        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
        [io.directory]::Delete($sourceJunction)
        [io.directory]::Delete($destinationJunction)
    }
    # 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 *

Related Post

PowerShell: How to Properly Delete a Msol User Account

# Set the user ObjectID attribute variable$msolUser="6f2fcfcd-...."# Move user account to Recycle BinRemove-MsolUser -ObjectId $msolUser#…

“Simon” Piano Game JavaScript Code

Demo: https://blog.kimconnect.com/wp-content/projects/pianogame.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head>…

PowerShell: Detect Antivirus Name on a Windows Machine

function getAntivirusName { $wmiQuery = "SELECT * FROM AntiVirusProduct" $antivirus = Get-WmiObject -Namespace "root\SecurityCenter2" -Query…