首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用powershell将文件从一个目录同步到名称相似但目录结构不同的目录

使用powershell将文件从一个目录同步到名称相似但目录结构不同的目录
EN

Stack Overflow用户
提问于 2018-09-28 04:42:57
回答 1查看 252关注 0票数 0

因此,我有两个独立的服务器,我需要同步具有相同名称但在不同目录结构中的目录。

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

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

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

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

我需要把x上的任何东西拷贝到calamedia中,但是因为y,这使得操作变得非常困难。

我可以很容易地创建一个包含所有x值的数组:

代码语言:javascript
复制
$Projects = Get-ChildItem \\calafs01\GFX_Drop | %{$_.name} 

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

假设一旦我能弄清楚怎么做就使用机器复制...

提前感谢

EDIT per NAS答案:

所以我是个哑巴,我意识到在calamedia中x之后还有另一个目录,但它总是叫做GFX,所以目标路径实际上是\\calamedia\edit\y\x\GFX\*

假设这是一个简单的添加...因此,使用您的脚本并执行以下操作:

代码语言:javascript
复制
$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的末尾(带引号),以为这会起作用,但现在它在目的地被绊倒了。展开目录时是否出现语法错误?

EDIT 2:

明白了。

代码语言:javascript
复制
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部分(项目名称,在本例中是所有的"This is a test“文件夹)

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

EN

回答 1

Stack Overflow用户

发布于 2018-09-28 05:25:12

代码语言:javascript
复制
$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
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52544847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档