【问题标题】:Using powershell to sync files from one directory to a directory with a similar name but different dir structure使用powershell将文件从一个目录同步到名称相似但目录结构不同的目录
【发布时间】:2019-03-03 19:58:48
【问题描述】:

所以我有两个单独的服务器,我需要同步具有相同名称但位于不同目录结构中的目录。

例如一台服务器是\\calafs01\GFX_Drop\x\*

x 是 GFX_Drop 中的任何目录,我希望复制它的所有子文件夹及其内容(仅限修改和新文件)

另一台服务器是\\calamedia\edit\y\x\*

y 是我们的用户使用的组织类别。

我需要将 calafs01 上的 x 中的任何内容复制到 calamedia,但是由于 y 这使得它变得非常困难。

我可以轻松地创建一个包含所有 x 值的数组:

$Projects = Get-ChildItem \\calafs01\GFX_Drop | %{$_.name} 

但问题是让脚本在 calamedia 上要复制到的任何 y 目录中找到目录 x。

假设一旦我知道如何使用 robocopy...

提前致谢

根据 NAS 的回答编辑:

所以我很笨,意识到calamedia中x之后还有另一个目录,但它总是称为GFX,所以目标路径实际上是\\calamedia\edit\y\x\GFX\*

假设这将是一个简单的添加......所以拿起你的脚本并这样做:

$get_y = Get-ChildItem -Directory -Path \\calamedia\edit | Get-ChildItem -Directory     # get all dirs one level deeper

    Get-ChildItem -Directory -Path \\calafs01\GFX_Drop\ | ForEach-Object {              # get all folders 'x'
        $dest_y = $get_y | Where-Object -Property Name -eq $_.Name       # match folder names
        if ($dest_y) {
            Copy-Item -Path $_.FullName -Recurse -Destination "$dest_y.Parent.FullName/GFX" -Force -WhatIf
            # or robocopy if you need to copy modified/new files only
        }
    }

基本上只是将/GFX 添加到$dest_y.Parent.FullName 的末尾(带引号),认为这会起作用,但现在它在目的地上被绊倒了。扩展目录时是否出现语法错误?

编辑 2:

知道了。

PS C:\Users\chris.slagel> $get_y = Get-ChildItem -Directory -Path \\calamedia\edit | Get-ChildItem -Directory     # get all dirs one level deeper

Get-ChildItem -Directory -Path \\calafs01\GFX_Drop\ | ForEach-Object {              # get all folders 'x'
    $dest_y = $get_y | Where-Object -Property Name -eq $_.Name       # match folder names
    if ($dest_y) {
        Copy-Item -Path $_.FullName -Recurse -Destination "$($dest_y.Parent.FullName)\GFX" -Force -WhatIf
        # or robocopy if you need to copy modified/new files only
    }
}
What if: Performing the operation "Copy Directory" on target "Item: \\calafs01\GFX_Drop\This Is A Test Destination: \\calamedia\edit\Theatrical\GFX".
What if: Performing the operation "Copy Directory" on target "Item: \\calafs01\GFX_Drop\This Is A Test - Copy Destination: \\calamedia\edit\Gaming\GFX".
What if: Performing the operation "Copy Directory" on target "Item: \\calafs01\GFX_Drop\This Is A Test - Copy (2) Destination: \\calamedia\edit\Home Entertainment\GFX".
What if: Performing the operation "Copy Directory" on target "Item: \\calafs01\GFX_Drop\This Is A Test - Copy (3) Destination: \\calamedia\edit\Home Entertainment\GFX".
What if: Performing the operation "Copy Directory" on target "Item: \\calafs01\GFX_Drop\This Is A Test - Copy (4) Destination: \\calamedia\edit\TV & Streaming\GFX".
What if: Performing the operation "Copy Directory" on target "Item: \\calafs01\GFX_Drop\This Is A Test - Copy (5) Destination: \\calamedia\edit\VR\GFX".

看起来它正确地找到了类别文件夹,但它缺少 x 部分(项目名称,在这种情况下是所有“这是一个测试”文件夹)

例如,第一个目的地应该是\\calamedia\edit\Theatrical\This Is A Test\GFX

【问题讨论】:

  • 能否总能找到目标服务器上的文件夹 x 更深一层(在 'calamedia\' 和 'x' 之间是否只有一个文件夹 (y))?
  • 是的,总是只有一个 y 文件夹。
  • 使用"$($dest_y.Parent.FullName)\GFX"在引号内扩展+反斜杠`\`
  • 您在修改后的问题中不需要Parent,只需要合并/覆盖(现有目标)文件夹,这不是必需的,因为您现在有GFX。跨度>
  • 查看我上面关于改进 Edit2 的评论

标签: arrays powershell variables robocopy


【解决方案1】:
$get_y = Get-ChildItem -Directory -Path \\calamedia\ | Get-ChildItem -Directory     # get all dirs one level deeper

Get-ChildItem -Directory -Path \\calafs01\GFX_Drop\ | ForEach-Object {              # get all folders 'x'
    $dest_y = $get_y | Where-Object -Property Name -eq $_.Name       # match folder names
    if ($dest_y) {
        Copy-Item -Path $_.FullName -Recurse -Destination $dest_y.Parent.FullName -Force -WhatIf
        # or robocopy if you need to copy modified/new files only
    }
}

【讨论】:

  • 我忘记了什么哈哈。根据您的回答修改了问题。
猜你喜欢
  • 1970-01-01
  • 2022-10-17
  • 2020-04-11
  • 1970-01-01
  • 1970-01-01
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
  • 2010-09-10
相关资源
最近更新 更多