A
A
Andrey Strelkov2021-08-30 12:58:23
PowerShell
Andrey Strelkov, 2021-08-30 12:58:23

How to copy only new or changed files/folders in the last hour in Powershell?

Good afternoon, tell me please, I want to copy folders and files from one directory to another once a day, but only those that have been created / changed in the last hour. Those. do something like delta synchronization

There is the following hierarchy
Folder_1
-File_1_1
-File_1_2
Folder_2
-File_2_1
-Folder_2_1
--File_2_1_1
Folder_3

Now if a new file suddenly appears in the folder Folder_2_1 or any other changes, then they will have to be copied

I do this way

Get-ChildItem -Path $folder_donor | Where-Object {$_.LastWriteTime -gt (Get-Date).AddHours(-1) } | Copy-Item -Destination $folder_recipient -Recurse -Container


And it seems that the method is working, it copies files, repeats the hierarchy, BUT when creating or changing files, the attributes of the folder in which it is located also change, and thus the folder also falls under this selection, and as a result, not only the changed file is copied to the new location, but and all files in the changed folder

A, you need to copy only the files that have changed, and if this file is in a certain folder, then only copy the necessary folder hierarchy, without the left unchanged files

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir, 2021-08-30
@FluffyBeaver

Get-ChildItem has a -File parameter that allows you to limit the result (as you might guess - only files).

G
GaryManshow, 2021-08-30
@GaryManshow

Good afternoon!
When a file is changed, except for the date, the Hash of the file is changed. Using Get-FileHash you can pinpoint changes

M
Morrowind, 2021-08-31
@hekkaaa

Hey!
Here it is enough to apply the -File -Recurse
parameters at the beginning. This pipeline walked through the git repository meme through folders and copied files specifically, ignoring the folders.
Get-ChildItem -File -Recurse | Copy-Item -Destination C:\Temp\
As I see date filters and variables for -Destination you have already configured. Just a little modification on your part.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question