bkr009
bkr009

Reputation: 65

Powershell: Compare Last Modified to Specific Date and replace with correct Date

I'm still fairly new to powershell, so please bear with me.

I have 2 almost identical directories. Files and folders from the old directory were copied over to a new directory. However, during this transfer process, something happened to the last modified date. The files and folders in the new directory have incorrect last modified dates (ex: today).

Rather than re-doing the transfer process, which would take a long time, I'd like to write something in powershell that will compare the last modified dates of the two directories and correct the dates in the new directory.

I'd also like to check first if file/folder has been modified since the file transfer. There would be no reason to change the date on those files.

What I found from looking around and googling: Link1 Link2 Link 3 Link 4

I know that I can get the last modified date of a file with:

(Get-Item $filename).LastWriteTime

where $filename is the file directory.

I also came across the following:

dir $directory | ? {$_.lastwritetime -gt "6/1/19" -AND $_.lastwritetime -lt "12/30/19"}

I know I can get information regarding files that were modified between 2 dates. This, I can tweak to make it so the "less than (-lt)" can be used to check files that were not modified past a certain date.

dir $directory | ? {$_.lastwritetime -lt `12/13/19'}

This accomplishes one of my goals. I have a means to check if a file has been modified past a certain or not.

I saw this for changing the value lastwritetime

$folder = Get-Item C:\folder1
$folder.LastWriteTime = (Get-Date)

and realized this was simply

(Get-Item $filename).LastWriteTime = (Get-Date)

Which I could modify to meet my goal of replacing the new file's last write time wit the old file's correct time:

(Get-Item $filename).LastWriteTime = (Get-Item $filename2).LastWriteTime

I suppose what I'm struggling with is kind of putting it all together. I know how to recurse through files/folders for copy-item or even Get-Childitem by adding the "recurse" parameter. But I'm having difficulties wrapping my head around recursively navigating through each directory to change the dates.

Thank you for your help.

Upvotes: 4

Views: 8344

Answers (2)

Theo
Theo

Reputation: 61263

You could do the following to compare the LastWriteTime property of the original files and folders to the copies, while keping in mind that files in the copy folder could have been updated since the last transfer date.

# set the date to the last transfer date to determine if the file was updated after that
$lastTransferDate = (Get-Date).AddDays(-10)  # just for demo 10 days ago

# set the paths for the rootfolder of the originals and the rootfolder to where everything was copied to
$originalPath = 'D:\OriginalStuff'
$copyPath     = 'E:\TransferredStuff'

# loop through the files and folders of the originals
Get-ChildItem -Path $originalPath -Recurse | ForEach-Object {
    # create the full path where the copied file of folder is to be found
    $copy = Join-Path -Path $copyPath -ChildPath $_.FullName.Substring($originalPath.Length)
    # test if this object can be found
    if (Test-Path -Path $copy) {
        $item = Get-Item -Path $copy
        # test if the item has not been updated since the last transfer date
        if ($item.LastWriteTime -le $lastTransferDate) {
            # set the timestamp the same as the original
            $item.LastWriteTime = $_.LastWriteTime
        }
    }
}

Upvotes: 3

shadow2020
shadow2020

Reputation: 1361

Great job with what you've done so far.

Just put what you have into a foreach statement.

Foreach($item in (gci 'C:\Users\usernamehere\Desktop\folder123' -recurse)){

    (Get-Item $item.FullName).LastWriteTime = (Get-Item "C:\Users\usernamehere\Desktop\folderabc\RandomFile.txt").LastWriteTime

}

We wrap the Get-Childitem command with the -recurse flag into parenthesis so that the command executes on it's own and becomes a collection for our foreach command to traverse. $item is the current item in the loop. We will want to use the .FullName property to know the full path to the file for the current item. With that said you will use $item.FullName together for the files you are going to set the date on.

Upvotes: 2

Related Questions