如果不存在,如何将更新的文件复制到新文件夹并创建目录作为源? [英] how to copy updated files to new folder and create directory as source if not exist?

查看:82
本文介绍了如果不存在,如何将更新的文件复制到新文件夹并创建目录作为源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图比较2个文件夹中的文件,并将那些新的和更新的文件复制到diff文件夹中,例如:

I was trying to compare files in 2 folders and copy those new and updated files to a diff folder, for example:

newFolder has  
a\aa.txt (new folder and new file)  
b\aa.txt  
b\ab.exe (modified)  
b\ac.config (new file)  
aa.txt (modified)  
ab.exe (new file)  
ac.config

oldFolder has  
b\aa.txt  
b\ab.exe  
aa.txt  
ac.config

在这种情况下,我希望diff文件夹中的内容应该是:

In this case, what I expect in diff folder should be:

diffFolder    
a\aa.txt  
b\ab.exe  
b\ac.config  
aa.txt  
ab.exe

到目前为止,我一直在Google上搜索并尝试不同的方法,但仍然无法实现这一目标.
我已经得到了文件列表,其中包含需要使用
复制的文件及其路径 xcopy/edyl"newFolder \ *""oldFolder"
并尝试使用

So far I have been searching on google and trying different approaches, but still cannot achieve this.
I have get the files list includes files needed to be copied along with their path by using
xcopy /edyl "newFolder\*" "oldFolder"
and was trying using

for /f %%F in ('xcopy /e /dyl "new\*" "old"') do @xcopy %%F diff /e

这弄乱了diffFolder
也尝试过

this messes up diffFolder
also tried

for /f %%F in ('xcopy /e /dyl "new\*" "old"') do @robocopy new diff %%F /e

这仅在diffFolder中创建目录,但未复制文件,给我错误:无效参数#3:"newFolder \ a \ aa.txt"

this only create directories in diffFolder but not copy files, gave me error: invalid parameter #3 :"newFolder\a\aa.txt"

for /f %%F in ('xcopy /e /dyl "new\*" "old"') do @copy "%%F" "diff" >nul

仅复制文件而不创建目录.
我也在尝试使用Powershell,但使用@copy的结果相同.

only copy files not create directories.
I was also trying to use powershell but result the same with @copy.

有人可以帮助我解决这一特定问题吗?

Any one can help me on this specific issue?

提前谢谢!

推荐答案

由于这是用 powershell 标签标记的,因此这就是我在powershell中的做法.

Since this is tagged with a powershell tag, this is how I would do it in powershell.

首先使用目录名称设置一些变量:

First setup some variables with directory names:

#create path variables
$olddir = "C:\oldFolder"
$newdir = "C:\newFolder"
$diffdir = "C:\diffFolder"

现在,使用带有 -recurse 参数的 get-childitem 获取每个目录中的文件列表,并通过 where-object 进行过滤退出目录:

Now, get the list of files in each directory using get-childitem with -recurse parameter, piped through where-object to filter out directories:

#Get the list of files in oldFolder
$oldfiles = Get-ChildItem -Recurse -path $olddir | Where-Object {-not ($_.PSIsContainer)}

#get the list of files in new folder
$newfiles = Get-ChildItem -Recurse -path $newdir | Where-Object {-not ($_.PSIsContainer)}

现在,比较列表,但仅比较 LastWriteTime 属性(可以使用 Length 代替或与 LastWriteTime - LastWriteTime一起使用),长度).

Now, compare the lists, but compare only the LastWriteTime property (can use Length instead of or alongside LastWriteTime - LastWriteTime,Length).

确保使用 -Passthru 选项,以便每个文件作为对象传递,而所有文件属性仍可访问.

Make sure to use the -Passthru option so that each file is passed through as an object with all file properties still accessible.

通过 sort-object 滚动以对 LastWriteTime 属性进行排序,因此文件的处理时间从最早到最新.然后通过管道进入 foreach 循环:

Pipe through sort-object to sort on LastWriteTime attribute, so files are processed oldest to newest. Then pipe into a foreach loop:

Compare-Object $oldfiles $newfiles -Property LastWriteTime -Passthru | sort LastWriteTime | foreach {

在循环中,为每个文件构建保留目录结构的新名称(用diffdir路径替换olddir和newdir路径).

In the loop, for each file, build the new name retaining the directory structure (replace olddir and newdir paths with diffdir path).

使用 Split-Path 仅获取新路径的目录,并测试其是否存在-如果不存在,请使用 mkdir 作为 copy创建它-item 不会创建目标目录,除非复制目录而不是文件.

Get just the directory of the new path using Split-Path and test if it exists - if it doesn't, create it using mkdir as copy-item won't create target directory unless copying a directory, not a file.

然后,复制文件(可以使用copy命令中的 -whatif 选项让它只告诉您将复制的内容,而无需实际执行):

Then, copy the file (you can use the -whatif option in the copy command to have it just tell you what it would copy, without actually doing it):

     $fl = (($_.Fullname).ToString().Replace("$olddir","$diffdir")).Replace("$newdir","$diffdir")
     $dir = Split-Path $fl
     If (!(Test-Path $dir)){
        mkdir $dir
     }
     copy-item $_.Fullname $fl
}

因此,完整的脚本是:

#create path variables
$olddir = "C:\oldFolder"
$newdir = "C:\newFolder"
$diffdir = "C:\diffFolder"

#Get the list of files in oldFolder
$oldfiles = Get-ChildItem -Recurse -path $olddir | Where-Object {-not ($_.PSIsContainer)}

#get the list of files in new folder
$newfiles = Get-ChildItem -Recurse -path $newdir | Where-Object {-not ($_.PSIsContainer)}

Compare-Object $oldfiles $newfiles -Property LastWriteTime -Passthru | sort LastWriteTime | foreach {
     $fl = (($_.Fullname).ToString().Replace("$olddir","$diffdir")).Replace("$newdir","$diffdir")
     $dir = Split-Path $fl
     If (!(Test-Path $dir)){
        mkdir $dir
     }
     copy-item $_.Fullname $fl
}

这篇关于如果不存在,如何将更新的文件复制到新文件夹并创建目录作为源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆