将文件复制为符号链接并在 Windows 上维护目录结构?

将文件复制为符号链接并在 Windows 上维护目录结构?

我看过这个问题:以符号链接的形式递归复制整个目录,并保留当前符号链接如果我读得正确的话,我想在 Windows 中做同样的事情。

因此,我有这个:

Dir1\Dir_A\File.ext
Dir1\Dir_A\Dir_A_A\file2.ext
Dir1\Dir_B\File2.ext
...

我希望目标目录上的目录结构与 Dir1 完全相同,但所有文件都是指向源文件的符号链接。因此,我最终得到了以下结果:

Target_Dir\Dir_A\File.ext <- file is symlink, folders are created
Target_Dir\Dir_A\Dir_A_A\file2.ext <- file is symlink, folders are created
Target_Dir\Dir_B\File3.ext <- file is symlink, folders are created
...

我该如何实现这一点?我还在寻找一个批处理脚本或可以按计划执行的脚本,以便每隔一段时间复制新文件,跳过已经创建的符号链接,有点像同步作业,减去文件的实际复制,而是(符号)链接它们。

答案1

PowerShell:使用符号链接文件进行递归文件夹复制

您可以使用获取子项创建一个大批变量和环形迭代对象属性并加入一些条件如果逻辑来帮助创建满足您需求的可行解决方案。

本质上这...

  • 创建与源匹配的目标文件夹结构
  • 创建指向每个源匹配文件的源匹配目标符号链接文件结构

您只需设置$src指向源根文件夹位置的值和$dest指向根目标文件夹位置的值 - 其余逻辑将完成其余所有工作。

$src = "C:\Source\Folder\"
$dest = "C:\Destination\Folder\"
$src = $src.Replace("\","\\")

$i = Get-ChildItem -LiteralPath $src -Recurse
$i | % { Process {
    $apath = $_.FullName -Replace $src,""
    $cpath = $dest + $apath
    If(!(Test-Path (Split-Path -Parent $cpath))){New-Item -ItemType Directory -Force -Path (Split-Path -Parent $cpath)}
    If(!$_.PSIsContainer){If(!(Get-Item $cpath -ErrorAction SilentlyContinue)){New-Item -Path $cpath -ItemType SymbolicLink -Value ([WildcardPattern]::Escape($_.FullName)) -Force}}
    }}

支持资源

相关内容