I
I
i3core2021-04-11 03:26:01
PowerShell
i3core, 2021-04-11 03:26:01

Copying file attributes (creation date, modification date) from original to copy?

Tell me a powershell script that would change the dates of creation and modification of copy files, taking this information from the attributes of other files (originals), the names are the same, the directories are naturally different, or another situation - the copies have only a different extension, but are located in the same folder as originals.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MaxKozlov, 2021-04-11
@MaxKozlov

Copying the date is just
$item2.LastWriteTime = $item1.LastWriteTime
the same with CreationTimeand LastAccessTime
But matching $item1and $item2is a separate task.
In the simplest version (without subfolders), you can create a hashtable by key, as a key - the name of a
type file

$path1 = 'D:\1'
$items = %{}
Get-ChildItem -Path $path1 | Foreach-Object { $items[$_.basename] = $_ }
$path2 = 'D:\2'
Get-ChildItem -Path $path2 | Foreach-Object {
    if ($items.ContainsKey($_.basename)) {
       $_.LastWriteTime = $items[$_.basename].LastWriteTime
    }
}

A
Alexey Dmitriev, 2021-04-11
@SignFinder

There is no standard cmdlet for your conditions in Powershell, but I won’t google for you and look for a ready-made one, if it exists.
1. Form a list of duplicate files by type names like this

spoiler
Get-ChildItem -Recurse -File |
Group-Object -Property Directory,BaseName |
Where-Object Count -gt 1 |
Select-Object Name

There are other options here - https://community.spiceworks.com/topic/1354430-get...
2. Do Get-ACL from one file and Set-ACL on the second
https://www.tutorialspoint.com/how- to-copy-ntfs-pe...
It is clear that this is a simplified structure, since the task is not clearly described.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question