vba 如何使用VBA检查文本文件的时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10823444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:20:58  来源:igfitidea点击:

How to check timestamp of a text file using VBA

vbaexcel-vbatimestampexcel-2003excel

提问by MrB

I'm trying to write a code in Excel 2003 VBA (Windows XP) to find out if outside TXT file has different timestamp, so I can 'import' it if it changed.

我正在尝试在 Excel 2003 VBA (Windows XP) 中编写代码,以确定外部 TXT 文件是否具有不同的时间戳,因此如果更改,我可以“导入”它。

Is there any function in VBA that can save me?

VBA中有什么功能可以拯救我吗?

回答by Siddharth Rout

I think you want the modified date. If yes, then see this

我想你想要修改日期。如果是,那么请看这个

Debug.Print FileDateTime("C:\Sample.txt")

The format of the date and time displayed is based on the locale settings of your system.

显示的日期和时间格式基于系统的区域设置。

Edit

编辑

Using FileSystemObject

使用 FileSystemObject

Option Explicit

Sub Sample()
    Dim oFS As Object
    Dim sFile As String

    sFile = "C:\MyFile.txt"

    Set oFS = CreateObject("Scripting.FileSystemObject")

    '~~> Created Date
    Debug.Print "Created Date : "; oFS.GetFile(sFile).DateCreated

    '~~> Modified Date
    Debug.Print "Modified Date : "; oFS.GetFile(sFile).Datelastmodified

    Set oFS = Nothing
End Sub