【问题标题】:Copying files and it's subfolders复制文件及其子文件夹
【发布时间】:2020-09-29 04:14:23
【问题描述】:

我正在尝试复制到另一个目录,但它一直复制到那里(C:\ users ...),我不希望那样。我只想复制当前文件夹及其子目录。我还想在移动文件时留下一个符号链接。我在网站上找到了这个脚本,这是一个很棒的脚本!谢谢大家的关注脚本是这样的:

$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 
}

【问题讨论】:

    标签: windows powershell powershell-4.0 cmdlets movefile


    【解决方案1】:

    试试这样的:

    $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
    }
    

    【讨论】:

    • 嗨,迈克,它有效,谢谢!但是我需要保持相同的两个结构,并且在移动(xx)天内未访问的文件时,将其符号链接留在源文件夹中。
    • 嗨 Tiago,这是一个更新版本
    • 不客气 - 请将其标记为正确答案
    猜你喜欢
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    • 2011-07-19
    • 2016-07-24
    • 2016-03-03
    • 2021-10-23
    相关资源
    最近更新 更多