Файловая система Windows: время создания файла не меняется, когда while удаляется и создается заново

У меня есть следующий сценарий:

  • 1: Создать кучу файлов

  • 2: Вызвать какое-нибудь внешнее приложение, которое обрабатывает все файлы с разными время создания с момента последнего снимка

  • 3: Удалить файлы

  • 4: goto 1

Оказалось, что Windows не гарантирует, что он изменит время создания, когда пользователь создает файл, удаляет его, а затем создает файл с таким же именем.

Я написал небольшой сценарий PowerShell, который проверяет это:

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

Вывод:

...
...
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**

Если я сплю некоторое время (> 30 секунд) после удаления, все файлы будут иметь правильные метки времени.

Есть ли способ обойти это? Какой-то вызов winapi, который удаляет файл навсегда?

15
задан Klark 10 January 2012 в 13:53
поделиться