且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果不存在,如何将更新的文件复制到新文件夹并创建目录作为源?

更新时间:2022-11-05 11:40:59

由于这是用 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
}