简体   繁体   English

使用 PowerShell 复制具有文件夹结构的最近修改的文件

[英]Copy Recently Modified Files with Folder Structure Using PowerShell

I'm new to PowerShell.我是 PowerShell 的新手。 Can someone please help me with my requirement as below.有人可以帮我满足我的要求吗,如下所示。 I have Folder, subFolders, and sub-subfolders,... and files inside each level of folders.我有文件夹、子文件夹和子子文件夹,...以及每个级别文件夹中的文件。 If any file gets modified/created, I need to copy that modified/created file with the respective folder structure.如果有任何文件被修改/创建,我需要使用相应的文件夹结构复制该修改/创建的文件。

Eg, I have a folder structure like below.例如,我有一个如下所示的文件夹结构。

src
├── classes
│   └── ClassFile1(file)
├── objects
│   └── ObjectFile1(file)
└── Aura
    └── Component
        ├── ComponentFile1(file)
        └── ComponentFile2(file)

Now if ComponentFile1 gets changed, then I need to copy folder structure only related to that file to my target folder.现在,如果 ComponentFile1 发生更改,那么我需要将仅与该文件相关的文件夹结构复制到我的目标文件夹。 Like src/aura/Component/ComponentFile1.比如 src/aura/Component/ComponentFile1。

I have tried something like this which is not working.我试过这样的东西,但没有用。

$Targetfolder= "C:\Users\Vamsy\desktop\Continuous Integration\Target"

$Sourcefolder= "C:\Users\Vamsy\desktop\Continuous Integration\Source"

$files = get-childitem $Sourcefolder -file | 
          where-object { $_.LastWriteTime -gt [datetime]::Now.AddMinutes(-5) }|
          Copy-Item -Path $files -Destination $Targetfolder -recurse -Force

Any Help on this appreciated.对此的任何帮助表示赞赏。

Please try the code below.请尝试下面的代码。

$srcDir = "C:\Users\Vamsy\desktop\Continuous Integration\Target"
$destDir = "C:\Users\Vamsy\desktop\Continuous Integration\Source"

Get-ChildItem $srcDir -File -Recurse |
Where-Object LastWriteTime -gt (Get-Date).AddMinutes(-5) |
ForEach-Object {
    $destFile = [IO.FileInfo]$_.FullName.Replace($srcDir, $destDir)
    if($_.LastWriteTime -eq $destFile.LastWriteTime) { return }
    $destFile.Directory.Create()
    Copy-Item -LiteralPath $_.FullName -Destination $destFile.FullName -PassThru
}

What you want is easy achievable with robocopy instead of copy-item .使用robocopy而不是copy-item可以轻松实现您想要的。 It has the options for sysncing folders, purging deleted folders, and many others.它具有用于系统化文件夹、清除已删除文件夹和许多其他选项的选项。 Usage is something like this:用法是这样的:

$source = 'C:\source-fld'
$destination = 'C:\dest-fld'
$robocopyOptions = @('/NJH', '/NJS') #just add the options you need
$fileList = 'test.txt' #optional you can provide only some files or folders or exclude some folders (good for building a project when you want only the sourcecode updated)

Start robocopy -args "$source $destination $fileList $robocopyOptions"

Some useful options:一些有用的选项:

/s - include non-empty subdirectories /e to include the all the subdirs (sometimes needed as content will be put there at build or test time) /b - backup mode - copy newr files only /purge - delete what was deleted in source folder /s - 包括非空子目录/e以包括所有子目录(有时需要,因为内容将在构建或测试时放在那里) /b - 备份模式 - 仅复制较新的文件/purge - 删除源中删除的内容文件夹

For all the parameters see robocopy docs对于所有参数,请参阅robocopy 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM