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

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

问题描述

我有以下情况:
$ b $ ul

  • 1:创建一堆文件

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

  • 删除文件
  • 4:转到1




  • 事实证明,Windows并不保证当用户创建文件时,它会改变创建时间,删除它,并创建一个同名文件。



    I写了一个小的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


    $ b foreach($ filename $ filename)键)
    {
    if($ fileListNew [$ fname] .CreationTimeUtc -eq $ fileListOld [$ fname] .CreationTimeUtc)
    {
    写主机相同的创建时间-ForegroundColor Red
    Write-Host $ fname -ForegroundColor Red
    $ count ++
    }
    }

    写主机$ count
      ... $ 



    ...
    相同的创建时间
    241.txt
    相同的创建时间
    944.txt
    相同的创建时间
    908.txt
    相同的创建时间
    631.txt
    相同的创建时间
    175.txt
    相同的创建时间
    798.txt
    相同的创建时间
    192.txt
    相同的创建时间
    961.txt
    相同的创建时间
    476.txt
    相同的创建时间
    566.txt
    相同的创建时间
    945.txt
    相同的创建时间
    681.txt
    相同的创建时间
    880.txt
    相同的创建时间
    162.txt
    相同的创建时间
    634.txt
    相同的创建时间
    746.txt
    相同的创建时间
    442.txt
    相同的创建时间
    35.txt
    相同的创建时间
    96.txt
    相同的创建时间
    771.txt
    相同的创建时间
    787.txt
    相同的创建时间
    972.txt
    相同的创建时间
    642.txt
    相同的创建时间
    495.txt
    同一创建时间
    625.txt
    相同的创建时间
    666.txt
    相同的创建时间
    660.txt
    相同的创建时间
    6。 txt
    相同的创建时间
    593.txt
    相同的创建时间
    549.txt
    相同的创建时间
    842.txt
    相同的创建时间
    314.txt
    相同的创建时间
    148.txt
    ** 1000 **

    如果我在睡眠一段时间(> 30s)后删除所有文件将有正确的时间戳。



    有什么办法可以得到在这附近?有些winapi调用会将文件删除好吗?

    解决方案

    我相信你在Windows中遇到了一个叫做 文件系统隧道 。这是基于NT系统的一个功能,其中与同一目录中最近删除的文件同名的新文件将继承旧文件的创建时间。

    您可以禁用隧道或更改旧文件数据缓存的时间长度。有关详细信息,请参阅此 Microsoft知识库文章



    由于许多应用程序将删除并重新创建他们希望改变的文件,而不仅仅是更新它们,因此文件系统隧道化得到了实现。



    您应该可以使用@Jim Rhodes建议来抵消这个功能。

    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?

    解决方案

    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.

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

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