Copy-item Files in Folders and subfolders in the same directory structure of source server using PowerShell

前端 未结 5 1397
独厮守ぢ
独厮守ぢ 2021-02-05 02:20

I am struggling really hard to get this below script worked to copy the files in folders and sub folders in the proper structure (As the source server).

Lets say, there

5条回答
  •  暖寄归人
    2021-02-05 02:37

    one time i found this script, this copy folder and files and keep the same structure of the source in the destination, you can make some tries with this.

    # Find the source files
    $sourceDir="X:\sourceFolder"
    
    # Set the target file
    $targetDir="Y:\Destfolder\"
    Get-ChildItem $sourceDir -Include *.* -Recurse |  foreach {
    
        # Remove the original  root folder
        $split = $_.Fullname  -split '\\'
        $DestFile =  $split[1..($split.Length - 1)] -join '\' 
    
        # Build the new  destination file path
        $DestFile = $targetDir+$DestFile
    
        # Move-Item won't  create the folder structure so we have to 
        # create a blank file  and then overwrite it
        $null = New-Item -Path  $DestFile -Type File -Force
        Move-Item -Path  $_.FullName -Destination $DestFile -Force
    }
    

提交回复
热议问题