0

I'm trying to copy to another directory but it's copying over there all the way (C: \ users ...), and I don't want that. I just want to copy the current folder and its subdirectories. I also want to leave a symbolic link when the file is moved. I got this script here on the site, it's a great script! Thank you all for your attention The script is this:

$sourceDir = "C:\Users\295078898\Documents\Teste"
$archiveTarget = "C:\Users\295078898\Downloads\Teste"
$dateToday = Get-Date
$date = $dateToday.AddDays(-20)

$items = Get-ChildItem $sourceDir -recurse |
         Where-Object {!$_.PSIsContainer -and $_.LastWriteTime -le $date}

foreach ($item in $items) {
  $withoutRoot = $item.FullName.Substring([System.IO.Path]::GetPathRoot($item.FullName).Length);
  $destination = Join-Path -Path $archiveTarget -ChildPath $withoutRoot

  $dir = Split-Path $destination
  if (!(Test-Path $dir)) {
    mkdir $dir
  }

  Move-Item -Path $item.FullName -Destination $destination   

    function CreateLink {
        param (
            [string] $LinkName,
            [string] $TargetFile
        )

        &"cmd.exe" /c mklink "$LinkName" "$TargetFile" | Out-Null
    }

    CreateLink -LinkName $item.FullName -TargetFile $destination 
}
Filburt
  • 17,626
  • 12
  • 64
  • 115

1 Answers1

0

Try something like this:

$sourceDir = [string]"C:\TMP\295078898\Teste"
$archiveTarget = [string]"C:\Temp\295078898\Teste"
$dateToday = Get-Date
$date = $dateToday.AddDays(-20)

$items = Get-ChildItem $sourceDir -recurse |
     Where-Object {!$_.PSIsContainer -and $_.LastWriteTime -le $date}

foreach ($item in $items) {

    Move-Item -Path $item.FullName -Destination $archiveTarget   

    New-Item -ItemType SymbolicLink -Target $archiveTarget -Path $item.FullName -Force
}
Mike Kennedy
  • 374
  • 2
  • 6