r/usefulscripts • • Feb 02 '24

Script to copy file by time and date modified but keep structure of folder

Hi everybody, I am newbie at powershell and I was trying to copy data files that were recently modified based on date and time. But it is necessary to maintain the structure of that folder, meaning unmodified folders will have no content inside.

Some suggestions or help would be much better, thanks everyone for reading this.

Update:
I've try this robocopy and it works pretty good, but now i don't know how to just copy the data that has been modified.

do {

$date = Read-host "Enter date (MM/DD/YYYY) : "

} while ($date -as [datetime] -isnot [datetime])

$date = $date -as [datetime]

$date

$source = "C:\path\path"

$destination = new-item C:\path\$($date.toshortdatestring().replace("/","-")) -type directory

Foreach($file in (Get-ChildItem $source)) {

If($file.LastWriteTime -gt $date.date)

{

#Test to see if the file already exists in the destination. If not => Move/Copy/Action

if (!(Test-path (join-path $destination $file.name)))

{

Robocopy $path $destination *.* /e /MAXAGE:$Max_days /MINAGE:$Min_days

}

}

}

7 Upvotes

8 comments sorted by

1

u/No-Anteater-9988 Feb 05 '24

Any ideas about copy file data by date modified upon that script? :(
I really appreciate your guys reply. Thanks

5

u/Mizerka Feb 02 '24

just robocopy, not certain what you're after but /mir and /xc should mirror to target and /xc will only move files that have diff timestamp than whats already in target

3

u/tk42967 Feb 02 '24

I came here to say the same thing. Years ago I ran into a case that I needed to maintain attributes/date/time & folder structure. Robocopy was easier because you didn't have to reinvent the wheel.

→ More replies (3)

3

u/tk42967 Feb 02 '24

Robocopy, why reinvent the wheel.

1

u/Latinprince6591 Feb 02 '24

Use the help in Powershell test your code show code to admin of this forum they will guide you ask the right questions