Windows 文件系统:删除并再次创建文件时,文件的创建时间不会改变 [英] Windows filesystem: Creation time of a file doesn't change when while is deleted and created again

查看:27
本文介绍了Windows 文件系统:删除并再次创建文件时,文件的创建时间不会改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下场景:

  • 1:创建一堆文件

  • 1: Create a bunch of files

2:调用一些外部应用程序来处理所有不同的文件自上次快照以来的创建时间

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

3:删除文件

4:转到 1

事实证明,当用户创建文件、删除文件和创建同名文件时,Windows 不保证它会更改创建时间.

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.

我写了一个小的 powershell 脚本来验证这一点:

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

输出:

...
...
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 秒),所有文件都会有正确的时间戳.

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

有什么办法可以解决这个问题吗?某些会永久删除文件的 winapi 调用?

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

推荐答案

我相信您在 Windows 中遇到过一种现象,称为 文件系统隧道.这是基于 NT 的系统的一个特性,其中与同一目录中最近删除的文件同名的新文件将继承旧文件的创建时间.

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.

您可以禁用隧道或更改缓存旧文件数据的时间长度.请参阅此 Microsoft 知识库文章详情.

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.

您应该能够使用@Jim Rhodes 的建议来抵消此功能.

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

这篇关于Windows 文件系统:删除并再次创建文件时,文件的创建时间不会改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆