[FIX] How to preserve file and folder TIMESTAMPS!

Discuss new features and functions
Posts: 14
Joined: 17 Nov 2021

SonicRings

I just signed up so I can make this post.

I know this has been asked a lot, and is a feature I have been wishing for myself for the longest time.

Seeing that it hasn't been implemented yet, I decided to finally take matters into my own hands.

For each job, you can assign a command to be run on completion:

Image

So, I wrote a Robocopy script that would update the destination's file and folder timestamps based on the source's. The batch file (which you can use outside of FreeFileSync) is below:
ROBOCOPY "Source" "Destination" /S /COPY:AT /DCOPY:T /TIMFIX

:: Copy timestamps and attributes from files in source to destination without actually copying any files or folders

:: /S copies non-empty subfolders

:: /COPY:AT only copies attributes and timestamps, not data
:: Since no data is being copied, all subfolders are therefore empty

:: Using /E would still copy empty subfolders, but since the goal is to
:: simply sync attributes and, more importantly, timestamps, after a
:: FreeFileSync job, /S is used so it only runs on files that have been copied
:: to prevent possible exclusions from having their folders copied over anyway

:: /DCOPY:T copies timestamps of directories

:: /TIMFIX fixes file times on all files, even skipped files

pause
The comments describe what each command does.

I'm a noob with robocopy, so if this can be improved upon, please let me know. I would prefer not having to hardcode the source and destination folders in the command itself for use with FFS, but I'm not sure how to go about doing that. It's obviously mandatory to have those when running this as a batch file however.

Anyway, what you need to paste in the command box in FFS is the following:
ROBOCOPY "Source" "Destination" /S /COPY:AT /DCOPY:T /TIMFIX
Make sure the source and destination matches that of the actual job.

I currently don't know how to go about doing this for jobs with multiple directories in an elegant way, but hopefully this is useful for those wishing for this functionality.

And hey, if this can finally be implemented in FFS itself so we wouldn't even need to do this, that would be great. :)
Posts: 1037
Joined: 8 May 2006

therube

I would prefer not having to hardcode the source and destination folders in the command itself for use with FFS
If FFS could pass the Left/Right directory names (as "Macros", say %LeftDir%, %RightDir%), you could then pass them to your batch file.
ROBOCOPY  %1  %2...
And for multiple pairs, you could set up a loop... (pseudo-code):
for %%i in %1 %2
do ROBOCOPY %1 %2...
-cut- %1 %2, such that the next set of pairs, coinciding with the next %i, what were, say, %3 %4, become %1 %2...
next %%i
(That "-cut-" part, can't recall how to do that offhand. Maybe it was in UNIX where it was easy, but still should be able to be done in Windows.)
Posts: 14
Joined: 17 Nov 2021

SonicRings

I would prefer not having to hardcode the source and destination folders in the command itself for use with FFS
If FFS could pass the Left/Right directory names (as "Macros", say %LeftDir%, %RightDir%), you could then pass them to your batch file.
ROBOCOPY  %1  %2...
therube, 17 Nov 2021, 17:30
I was wondering about that. Is that actually possible?
Posts: 14
Joined: 17 Nov 2021

SonicRings

Another improvement I'd like to see made to my solution, if possible, is to only run the command on the files that have actually transferred.

As it stands, it runs on every single file that is available in the source, even if only one folder was added to the destination. In my case, I'm backing up my music library with over 130,000 files. This takes several minutes to complete.

I do appreciate very how the task will continue running even after FFS is fully closed, but I feel that if this were to be implemented in FFS in the first place, it would be a lot more refined as it would be trivial to make it only run on the exact files that were transferred, not on everything.
Posts: 6
Joined: 30 Nov 2022

time305s

Hey, thanks for bringing up the use of Robocopy. I followed the documentation (https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy) and tried as many command/flag combinations as possible but could not get date accessed to copy from a folder in the source folder to the copied folder. Do you have any tips or is this just not possible? I have also made my own post recently about another small program that can copy over date created, date modified, and date accessed. In terms of performance consistency, it has some very minor limitations but at least it can do date accessed for folders better than Robocopy (viewtopic.php?t=9842).
Posts: 4
Joined: 23 May 2023

StefanM

Robocopy cannot copy Timestamps without Data.

I had the following issue.
A is a Local Drive by me.
B is a webdav Drive which dosnt support Timestamps settings.

After FileSync does its job, by the next run, it can see many "new" files on the B: (WebDav) as the datetimes was not set correctly. I cant change thenm at all, as WEbdav doesnt support it.

Therefor I use the Powershell script to set time Timestamps according to the latest sync on the local site.
After that, FileSyanc sees on both sites the same timestamp and can sync perfectly.

BUT:
- The Filedate doesn't show then the "Last modified" date, but the "Last modified/synced" date

I dont care about that, as I need a working sync at least.

But the solution....
I did it with powershell:
$sourceDirectory = "B:\your_daten\source\"
$destinationDirectory = "A:\your_daten\destination\"

$sourceFiles = Get-ChildItem -Path $sourceDirectory -File -Recurse

foreach ($file in $sourceFiles) {
    $destinationFilePath = $file.FullName.Replace($sourceDirectory, $destinationDirectory)
    if (Test-Path $destinationFilePath) {
        $destinationFile = Get-Item -Path $destinationFilePath

        $updateRequired = $false

        if ($destinationFile.LastWriteTime -ne $file.LastWriteTime) {
            $destinationFile.LastWriteTime = $file.LastWriteTime
            $updateRequired = $true
        }
        if ($destinationFile.CreationTime -ne $file.CreationTime) {
            $destinationFile.CreationTime = $file.CreationTime
            $updateRequired = $true
        }
        if ($destinationFile.LastAccessTime -ne $file.LastAccessTime) {
            $destinationFile.LastAccessTime = $file.LastAccessTime
            $updateRequired = $true
        }

        if ($updateRequired) {
            Write-Host "- File: $($file.FullName) B: $($file.LastWriteTime) A: $($destinationFile.LastWriteTime)"
            $destinationFile.SetAttributes($file.Attributes)
        }
    }
}

Write-Host "File times synchronized successfully!"
save that as ps1 file.

within the FreeFileSync you can add the following command:
powershell.exe -ExecutionPolicy Bypass -File "A:\where\filesync_dates.ps1"
Posts: 6
Joined: 30 Nov 2022

time305s

I did it with powershell:
$sourceDirectory = "B:\your_daten\source\"
StefanM, 23 May 2023, 08:33
Hello again! Do you think you can make a script that can sync date created, date modified and date accessed for both files AND folders?
Posts: 14
Joined: 17 Nov 2021

SonicRings

I did it with powershell:
$sourceDirectory = "B:\your_daten\source\"
StefanM, 23 May 2023, 08:33
Hello again! Do you think you can make a script that can sync date created, date modified and date accessed for both files AND folders? time305s, 05 Sep 2024, 22:45
This is crazy: I visited the forum today to make a new post on an unrelated matter, and noticed I had 2 notifications of unread replies to this thread. I looked at them and figured it's already been 2 years so you probably already figured it out or moved on.

Now I see you posted a reply mere hours after this. What a huge coincidence!

Anyway, I see you're asking about timestamps in folders, but my post in the OP already explains how to copy the timestamps of folders even if they are empty. Does this not work for you? Here is the command again:
ROBOCOPY "Source" "Destination" /S /COPY:AT /DCOPY:T /TIMFIX
Posts: 6
Joined: 30 Nov 2022

time305s

No, as stated before though hard to catch since the quoting/reply system is a bit wonky here, Robocopy does not copy over date accessed, I think, for folders. This does not matter too much, but I was hoping that the script by StefanM can be modified to do so.
Posts: 4
Joined: 23 May 2023

StefanM

My script which I posted, you can attach at the free file sync application. Yes, that checks all dates of the files and applies them on the other side. Did work well for me. Luckly the official provider of my cloud did fix there app with many files and its now working. So I dont use freefilesync anymore. :)

Regards
Posts: 4
Joined: 23 May 2023

StefanM

But to update my script, to also modify the folders:
$sourceDirectory = "B:\wuk_daten\"
$destinationDirectory = "A:\wuk_daten\"

# Funktion zum Synchronisieren der Zeitstempel und Attribute
function Sync-ItemProperties($sourceItem, $destinationItem) {
    $updateRequired = $false

    if ($destinationItem.LastWriteTime -ne $sourceItem.LastWriteTime) {
        $destinationItem.LastWriteTime = $sourceItem.LastWriteTime
        $updateRequired = $true
    }
    if ($destinationItem.CreationTime -ne $sourceItem.CreationTime) {
        $destinationItem.CreationTime = $sourceItem.CreationTime
        $updateRequired = $true
    }
    if ($destinationItem.LastAccessTime -ne $sourceItem.LastAccessTime) {
        $destinationItem.LastAccessTime = $sourceItem.LastAccessTime
        $updateRequired = $true
    }

    if ($updateRequired) {
        Write-Host "- Item: $($sourceItem.FullName) B: $($sourceItem.LastWriteTime) A: $($destinationItem.LastWriteTime)"
        Set-ItemProperty -Path $destinationItem.FullName -Name Attributes -Value $sourceItem.Attributes
    }
}

$sourceItems = Get-ChildItem -Path $sourceDirectory -Recurse

foreach ($item in $sourceItems) {
    $destinationPath = $item.FullName.Replace($sourceDirectory, $destinationDirectory)
   
    if (Test-Path $destinationPath) {
        Write-Host "."
        $destinationItem = Get-Item -Path $destinationPath -Force
        Sync-ItemProperties $item $destinationItem
    }
    else {
        if ($item.PSIsContainer) {
            New-Item -Path $destinationPath -ItemType Directory -Force | Out-Null
        }
        else {
            Copy-Item -Path $item.FullName -Destination $destinationPath -Force
        }
    }
}

Write-Host "Datei- und Verzeichniszeiten wurden erfolgreich synchronisiert!"
Please test it.
Posts: 6
Joined: 30 Nov 2022

time305s

It seems to be working for the folders and files I tested it on, thanks! I will probably try to modify it myself when I have more time.
Posts: 6
Joined: 19 Oct 2024

utz

Hello,
I'm new here and a computer dummy.
First of all, many thanks to SonicRings and StefanM for their great work on adjusting the folder date.
I tried SonicRings and StefanM methods and both don't work for me with Freefilesync.

For me it looks like this:
Source folder: C:\tt\documents
Destination folder: D:\tt\documents

How do I have to change StefanM's script for my source and target folders and what does the command in the synchronization settings of Freefilesync look like?

What do you have to do when you're in one Freefilesync session wants to synchronize 3 folders at once:
Source folders: C:\tt\documents
C\:tt\downloads
C:\tt\pictures

Destination folders: D:\tt\documents
D:\:tt\downloads
D:\tt\pictures

Thousand thanks you in advance for your help.
User avatar
Posts: 4055
Joined: 11 Jun 2019

xCSxXenon

Hello,
I'm new here and a computer dummy.
First of all, many thanks to SonicRings and StefanM for their great work on adjusting the folder date.
I tried SonicRings and StefanM methods and both don't work for me with Freefilesync.

For me it looks like this:
Source folder: C:\tt\documents
Destination folder: D:\tt\documents

How do I have to change StefanM's script for my source and target folders and what does the command in the synchronization settings of Freefilesync look like?

What do you have to do when you're in one Freefilesync session wants to synchronize 3 folders at once:
Source folders: C:\tt\documents
C\:tt\downloads
C:\tt\pictures

Destination folders: D:\tt\documents
D:\:tt\downloads
D:\tt\pictures

Thousand thanks you in advance for your help. utz, 19 Oct 2024, 15:18
I would just change $SourceDirectory and $DestinationDirectory to "C:\tt\" and "D:\tt\", respectively
This does mean the script will "waste" time by scanning extra subfolders of 'tt\' instead of just those three, but it simplifies what you have to do. You could have three copies of the script, each dedicated to one of those folders, or modify the script to change the source and destination variables and run the conditionals again
Posts: 4
Joined: 23 May 2023

StefanM

My Script is fine for that. Change only the first to lines:

$sourceDirectory = "C:\tt\documents\"
$destinationDirectory = "D:\tt\documents\"

Save that file somewhere on c drive, like C:\where\filesync_dates.ps1

Then you need to normally set the freefilesync up betrween the 2 folders, and then you have an additional option to set "run programm after sync" where you enter (with the corrent file path):

powershell.exe -ExecutionPolicy Bypass -File "C:\where\filesync_dates.ps1"

Then run the sync. Thats it..
Posts: 14
Joined: 17 Nov 2021

SonicRings

Hello,
I'm new here and a computer dummy.
First of all, many thanks to SonicRings and StefanM for their great work on adjusting the folder date.
I tried SonicRings and StefanM methods and both don't work for me with Freefilesync.

For me it looks like this:
Source folder: C:\tt\documents
Destination folder: D:\tt\documents

How do I have to change StefanM's script for my source and target folders and what does the command in the synchronization settings of Freefilesync look like?

What do you have to do when you're in one Freefilesync session wants to synchronize 3 folders at once:
Source folders: C:\tt\documents
C\:tt\downloads
C:\tt\pictures

Destination folders: D:\tt\documents
D:\:tt\downloads
D:\tt\pictures

Thousand thanks you in advance for your help. utz, 19 Oct 2024, 15:18
Hi, for my solution all you need to do is write the following:
ROBOCOPY "C:\tt\documents" "D:\tt\documents" /S /COPY:AT /DCOPY:T /TIMFIX
It should go here:
Image

All the other settings can be set up how you like, since all my solution does is run a command line argument after the folder pair finishes syncing.

It runs invisibly in the background once the file sync completes, so it won't be obvious when it's finished running, but you should see CMD or similar in task manager accessing your drives as it works to copy the date attributes to the destination. Depending on how big the folder is, it could take a little while.

I haven't tried Stephan's script since mine is a one-liner that works perfectly fine with no additional files/scripts you need to worry about, but it only works on folder pairs, as in 1 source and 1 destination. If his works for multiple folders at a time, you can try it if you don't mind trying to figure out how to use powershell scripts.
Posts: 6
Joined: 19 Oct 2024

utz

Hello,
Sorry for not getting back to you, I was on vacation.

xCSxXenon, StefanM and SonicRings many thanks for your help.
I will test your advice over the next few days and write the result here.
Posts: 6
Joined: 19 Oct 2024

utz

Hello,
I have now tested extensively and experimented a bit.
@StefanM: Your powershell script works for me but when I use the script with a filter for the time period of maybe 10 days in freefylesync then freefylesync ignore the 10 days. This then means that all subfolders including the documents they contain and also all files from for example "C:\tt\documents" are copied without taking the time period into target folder. However, the date of the folders that are within the set time period is adopted exactly. All other folders that copied to destination have the current date even though they shouldn't be copied.

@SonicRings: Your Robocopy command works great. But unfortunately you can only enter one source and one destination.
Since I use Windows task scheduling for freefylesync works I would have liked to have done correcting date and time for the three source folders and the three target folders at once by starting a Robocopy batch in the command line of freefylesync.
I tried for the three source folders:
C:\tt\documents
C\:tt\downloads
C:\tt\pictures
and the three destination folders:
D:\tt\documents
D:\:tt\downloads
D: \tt\ pictures
to write a Robocopy batch script.
But unfortunately that doesn't work. I am unfortunately a computer dummy.
I think many program users of freefylesync have the problem with multiple source folders and multiple target folders do at once.

Does anyone have please an idea how to solve this problem?

Sorry for my bad english.
Thank you
Posts: 14
Joined: 17 Nov 2021

SonicRings

Hello,
I have now tested extensively and experimented a bit.
@StefanM: Your powershell script works for me but when I use the script with a filter for the time period of maybe 10 days in freefylesync then freefylesync ignore the 10 days. This then means that all subfolders including the documents they contain and also all files from for example "C:\tt\documents" are copied without taking the time period into target folder. However, the date of the folders that are within the set time period is adopted exactly. All other folders that copied to destination have the current date even though they shouldn't be copied.

@SonicRings: Your Robocopy command works great. But unfortunately you can only enter one source and one destination.
Since I use Windows task scheduling for freefylesync works I would have liked to have done correcting date and time for the three source folders and the three target folders at once by starting a Robocopy batch in the command line of freefylesync.
I tried for the three source folders:
C:\tt\documents
C\:tt\downloads
C:\tt\pictures
and the three destination folders:
D:\tt\documents
D:\:tt\downloads
D: \tt\ pictures
to write a Robocopy batch script.
But unfortunately that doesn't work. I am unfortunately a computer dummy.
I think many program users of freefylesync have the problem with multiple source folders and multiple target folders do at once.

Does anyone have please an idea how to solve this problem?

Sorry for my bad english.
Thank you utz, 05 Nov 2024, 21:47
The solution to robocopy is simple. Limit each transfer to 1 source and 1 destination. Make 3 separate transfers. Then, schedule the transfers in task scheduler such that the second one is triggered by the completion of the first one, and the third one is triggered by the completion of the second one.
Posts: 6
Joined: 19 Oct 2024

utz

Hello,
Thank you very much SonicRings for your answer, I followed your advice and implemented it according to the link
https://woshub.com/schedule-task-to-start-when-another-task-finishes/
and can now start the second and third tasks after the previous ones.

I only have one problem:
If I want to copy folders directly from C:\ Robocopy doesn't work with the date change. Do you have experience with this?
thank you
Posts: 14
Joined: 17 Nov 2021

SonicRings

Hello,
Thank you very much SonicRings for your answer, I followed your advice and implemented it according to the link
https://woshub.com/schedule-task-to-start-when-another-task-finishes/
and can now start the second and third tasks after the previous ones.

I only have one problem:
If I want to copy folders directly from C:\ Robocopy doesn't work with the date change. Do you have experience with this?
thank you utz, 12 Nov 2024, 19:45
I have many shares that also have root drives as the source folder (eg. D:\, F:\), and those copy timestamps just fine. I suggest making sure that the command you're using for that share is correct.

I will post an example share and command that I use:

Source: D:\
Destination: Q:\FFS\D
Command: ROBOCOPY "D:" "Q:\FFS\D" /S /COPY:AT /DCOPY:T /TIMFIX
Posts: 6
Joined: 19 Oct 2024

utz

Hello,
SonicRings, I can't thank you enough for your help and support.
I have now tried many times to copy directly from the Partition C: folders with FreeFileSync. The Robocopy command is correct. Every time the folders have the current date despite Robocopy.
When synchronizing from for example partition D:, E:, F: etc, the creation date is retained. Only not for folders from the C: partition.
This is probably a Windows problem because it is C:.
Have you ever tried syncing folders directly from C:?
Posts: 14
Joined: 17 Nov 2021

SonicRings

I may very well be that the folders on the root of the C drive require admin rights to fully access. You can test this by running the robocopy command in an elevated command prompt.
Posts: 1037
Joined: 8 May 2006

therube

FWIW: Deleting files without touching a directories date

If I'm remembering correctly...

I used robocopy to backup the timestamps of the /directories/ within a directory tree - ahead of time
ROBOCOPY  %SOURCE%  %COPY%  /E /XF *
then used (UNIX) touch to "re-date" the (changed, so touched) SOURCE directories, based upon the previously backed up COPY (directory dates)
for /d %%i in (*)  do  touch "%source%\%%i"  -r "%copy%\%%i"  --no-create
Posts: 6
Joined: 19 Oct 2024

utz

Thank you my friends for your participation.
Unfortunately, I'm a computer idiot.

@SonicRings Despite admin rights and full access, robocopy does not work with C:. Does this work under C: for you? Have you ever tested ?

@therube Please help me. How should I enter this in FreeFileSync, especially the UNIX code?
Posts: 14
Joined: 17 Nov 2021

SonicRings

Thank you my friends for your participation.
Unfortunately, I'm a computer idiot.

@SonicRings Despite admin rights and full access, robocopy does not work with C:. Does this work under C: for you? Have you ever tested ?

@therube Please help me. How should I enter this in FreeFileSync, especially the UNIX code? utz, Yesterday, 19:50
Sorry you're having trouble still. I've just tested it with my C drive, and it does indeed work. I'm not sure what the issue may be with yours. Please make sure your Robocopy command is correct. As long as it's not giving an error, it should work.