0

I want to use a Powershell script for copy a folder recursive to a other position. This must the Powershell todo:

  • copy file and folders from position A to position B
  • UNC PAth must have (for example \net.local\Files\EDV)
  • On position B must all empty Folder clear
  • The structure of position B must equals with postion A
  • Missing folders should be created on B .
  • It should only copy files that are older than 180 days
  • The Script must create a logfile with Information about the filename and path, File size, file date

I have begun with this script:

$a = '\\serverA\folderA'
$b = '\\serverB\folderB'

#This copies the files
Get-ChildItem $a -Recurse -File | Foreach_Object {Copy-Item $_ -Destination $b}

#Removes empty files
Get-ChildItem $b -File | Foreach-Object {IF($_.Length -eq 0) {Remove-Item $_}}

I need help..

1

1 Answer 1

1

This code copies a directory to another directory, the rest should be straight forward. In $toreplace every backslash should be escaped with an extra backslash.

$a = [System.IO.DirectoryInfo]'C:\Users\oudou\Desktop\dir'
$b = [System.IO.DirectoryInfo]'C:\Users\oudou\Desktop\dir_copy'


function recursive($a,$b)
{
    foreach ($item in @(Get-ChildItem $a.FullName))
    {
        if($item -is [System.IO.DirectoryInfo])
        {
            if ( -not (Test-Path $item.FullName.Replace($a.FullName,$b.FullName)))
            {
                New-Item -ItemType Directory $item.FullName.Replace($a.FullName,$b.FullName)
            }
            $dest = Get-ChildItem $item.FullName.Replace($a.FullName,$b.FullName)
            $dest
            recursive($item, $dest)
        }
        else
        {
            [string]$y = $item.FullName
            $toreplace = "C:\\Users\\oudou\\Desktop\\dir"
            $replace = "C:\Users\oudou\Desktop\dir_copy"
            $y -replace $toreplace , $replace            
            Copy-Item  $item.FullName  ($item.FullName -replace $toreplace , $replace)
        }
    }
}


recursive $a $b
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.