Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows filesystem: Creation time of a file doesn't change when while is deleted and created again

I have a following scenario:

  • 1: Create a bunch of files

  • 2: Call some external app that processes all files with different creation time since last snapshot

  • 3: Delete files

  • 4: goto 1

It turned out that windows doesn't guarantee that it will change creation time when user creates a file, deletes it and than creates a file with a same name.

I wrote a small powershell script that verifies this:

ls | Remove-Item

$fileListOld = @{}
foreach($i in 1..1000)
{
    $fname = [string]::Format("{0}.txt", $i)
    "tst" >> $fname    
}

ls | % { $fileListOld[$_.Name] = $_ }
ls | Remove-Item

foreach($i in 1..1000)
{
    $fname = [string]::Format("{0}.txt", $i)
    "tst" >> $fname    
}

$fileListNew = @{}
ls | % { $fileListNew[$_.Name] = $_ }

$count = 0



foreach ($fname in $fileListNew.Keys)
{
    if ($fileListNew[$fname].CreationTimeUtc -eq $fileListOld[$fname].CreationTimeUtc)
    {
        Write-Host Same creation time -ForegroundColor Red
        Write-Host $fname -ForegroundColor Red
        $count++
    }
}

Write-Host $count

Output:

...
...
Same creation time
241.txt
Same creation time
944.txt
Same creation time
908.txt
Same creation time
631.txt
Same creation time
175.txt
Same creation time
798.txt
Same creation time
192.txt
Same creation time
961.txt
Same creation time
476.txt
Same creation time
566.txt
Same creation time
945.txt
Same creation time
681.txt
Same creation time
880.txt
Same creation time
162.txt
Same creation time
634.txt
Same creation time
746.txt
Same creation time
442.txt
Same creation time
35.txt
Same creation time
96.txt
Same creation time
771.txt
Same creation time
787.txt
Same creation time
972.txt
Same creation time
642.txt
Same creation time
495.txt
Same creation time
625.txt
Same creation time
666.txt
Same creation time
660.txt
Same creation time
6.txt
Same creation time
593.txt
Same creation time
549.txt
Same creation time
842.txt
Same creation time
314.txt
Same creation time
148.txt
**1000**

If I sleep for some time (>30s) after deletion all files will have correct time stamps.

Is there any way to get around this? Some winapi call that deletes file for good?

like image 727
Klark Avatar asked Jan 10 '12 13:01

Klark


People also ask

What happens when a file is deleted while a process is writing data to the same file on Linux?

The actual object/file will be removed later by a GC when all references are gone.

How to change data creation date of a file?

Click the Change Time/Attributes button (indicated below). A new window will open with details of what properties you can change. Enable the checkbox next to 'Created' and set the exact date and time that you want to set as the file's creation date.

How to change date modified in Windows file?

You can modify the date created by copying a file. The file's created date becomes the modified date and the current date (when the file is copied) becomes the created date. You can copy a file on your PC to check. Let us know if you need further assistance.


2 Answers

I believe you are encountering a phenomenon in Windows known as filesystem tunneling. This is a feature of NT based systems wherein a new file with the same name as a recently deleted file in the same directory will inherit the creation time of the old file.

You can disable tunneling or alter the length of time for which the old file data is cached. See this Microsoft KB article for details.

Filesystem tunneling was implemented since many applications will delete and recreate files they wish to alter rather than merely update them.

You should be able to use @Jim Rhodes suggestion to counteract this feature.

like image 55
Andrew Lambert Avatar answered Oct 01 '22 02:10

Andrew Lambert


You could use SetFileTime to update the create time as soon as you create the file.

like image 35
Jim Rhodes Avatar answered Oct 01 '22 02:10

Jim Rhodes