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

前端 未结 5 1385
独厮守ぢ
独厮守ぢ 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:35

    I wanted a solution to copy files modified after a certain date and time which mean't I need to use Get-ChildItem piped through a filter. Below is what I came up with:

    $SourceFolder = "C:\Users\RCoode\Documents\Visual Studio 2010\Projects\MyProject"
    $ArchiveFolder = "J:\Temp\Robin\Deploy\MyProject"
    $ChangesStarted = New-Object System.DateTime(2013,10,16,11,0,0)
    $IncludeFiles = ("*.vb","*.cs","*.aspx","*.js","*.css")
    
    Get-ChildItem $SourceFolder -Recurse -Include $IncludeFiles | Where-Object {$_.LastWriteTime -gt $ChangesStarted} | ForEach-Object {
        $PathArray = $_.FullName.Replace($SourceFolder,"").ToString().Split('\') 
    
        $Folder = $ArchiveFolder
    
        for ($i=1; $i -lt $PathArray.length-1; $i++) {
            $Folder += "\" + $PathArray[$i]
            if (!(Test-Path $Folder)) {
                New-Item -ItemType directory -Path $Folder
            }
        }   
        $NewPath = Join-Path $ArchiveFolder $_.FullName.Replace($SourceFolder,"")
    
        Copy-Item $_.FullName -Destination $NewPath  
    }
    

提交回复
热议问题