Vba 获取文件上次修改日期(资源管理器值不是cmd值)
我已经编写了一些Excel VBA代码,将文件名、版本和上次修改的日期/时间添加到工作表中。代码似乎工作正常,但有时文件的Vba 获取文件上次修改日期(资源管理器值不是cmd值),vba,excel,last-modified,Vba,Excel,Last Modified,我已经编写了一些Excel VBA代码,将文件名、版本和上次修改的日期/时间添加到工作表中。代码似乎工作正常,但有时文件的上次修改日期的时间部分与我在资源管理器窗口中看到的时间正好相差1小时 我注意到,如果执行dir命令,代码返回的值与cmd窗口中显示的修改日期/时间相同 例如,如果我在system32文件夹中查找dbghelp.dll文件: C:\Windows\System32>dir dbghelp.* Volume in drive C has no label. Volum
上次修改日期
的时间部分与我在资源管理器窗口中看到的时间正好相差1小时
我注意到,如果执行dir
命令,代码返回的值与cmd窗口中显示的修改日期/时间相同
例如,如果我在system32文件夹中查找dbghelp.dll文件:
C:\Windows\System32>dir dbghelp.*
Volume in drive C has no label.
Volume Serial Number is 16E8-4159
Directory of C:\Windows\System32
21/11/2010 04:24 1,087,488 dbghelp.dll
1 File(s) 1,087,488 bytes
0 Dir(s) 60,439,101,440 bytes free
C:\Windows\System32>
但是浏览器窗口中的同一个文件显示了修改后的时间,即2010年11月21日03:24的时间,该时间早于1小时
我编写的代码返回cmd窗口时间,而我需要Explorer窗口时间:
Sub GetFileDetails()
Dim path As String
Dim objFSO As Object
Dim objFile As Object
Dim objFolder As Object
Dim loopCount As Integer
Dim pathCheck As Boolean
'Prompt for directory path
path = InputBox(Prompt:="Enter file path", Title:="Enter file path", Default:="")
If (path = "" Or path = vbNullString) Then
MsgBox ("Invalid path - exiting")
Exit Sub
End If
'Required for interacting with filesystem
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(path)
'1st row for path title, 2nd row for column headings
loopCount = 3
For Each objFile In objFolder.Files
Range("A" & loopCount).Value = objFile.Name
Range("B" & loopCount).Value = objFSO.GetFileVersion(objFile)
Range("C" & loopCount).Value = objFile.DateLastModified
'Combine Version and Modified
If Range("B" & loopCount).Value <> "" Then
Range("D" & loopCount).Value = Range("B" & loopCount).Value & ", " & Range("C" & loopCount).Value
Else
Range("D" & loopCount).Value = Range("C" & loopCount).Value
End If
loopCount = loopCount + 1
Next
'Set up headings
Range("A" & 1).Value = (loopCount - 3) & " files found in " & path
Range("A" & 2).Value = "FileName"
Range("B" & 2).Value = "Version"
Range("C" & 2).Value = "Modified"
Range("D" & 2).Value = "Version & Modified"
End Sub
Sub-GetFileDetails()
将路径设置为字符串
作为对象的Dim objFSO
Dim objFile作为对象
将文件夹变暗为对象
Dim循环计数为整数
作为布尔值的Dim路径检查
'提示输入目录路径
路径=输入框(提示:=“输入文件路径”,标题:=“输入文件路径”,默认值:=”)
如果(path=”“或path=vbNullString),则
MsgBox(“无效路径-正在退出”)
出口接头
如果结束
'与文件系统交互时需要
设置objFSO=CreateObject(“Scripting.FileSystemObject”)
设置objFolder=objFSO.GetFolder(路径)
'路径标题的第一行,列标题的第二行
循环计数=3
对于objFolder.Files中的每个objFile
范围(“A”&loopCount).Value=objFile.Name
Range(“B”和loopCount).Value=objFSO.GetFileVersion(objFile)
范围(“C”&loopCount).Value=objFile.DateLastModified
'合并版本和修改
如果范围(“B”和loopCount).Value为“”,则
范围(“D”和循环计数)。值=范围(“B”和循环计数)。值&“,”和范围(“C”和循环计数)。值
其他的
范围(“D”和循环计数)。值=范围(“C”和循环计数)。值
如果结束
loopCount=loopCount+1
下一个
"设题",
范围(“A”&1).Value=(loopCount-3)和“在中找到的文件”&path
范围(“A”&2).Value=“文件名”
范围(“B”和“2”).Value=“版本”
范围(“C”和“2”).Value=“已修改”
范围(“D”和“2”).Value=“版本和修改”
端接头
如果有人能对这个问题有所了解,我们将不胜感激
==编辑===
这是我想出的代码,它总是为我提供与资源管理器窗口中显示的时间相同的时间:
Sub GetFileDetails()
Dim path As String
Dim objFSO As Object
Dim objFile As Object
Dim objFolder As Object
Dim loopCount As Integer
Dim pathCheck As Boolean
Dim modDate As Date
Dim modHour As Integer
Dim modMin As Integer
'Prompt for directory path
path = InputBox(Prompt:="Enter file path", Title:="Enter file path", Default:="")
If (path = "" Or path = vbNullString) Then
MsgBox ("Invalid path - exiting")
Exit Sub
End If
'Required for interacting with filesystem
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(path)
'1st row for path title, 2nd row for column headings
loopCount = 3
For Each objFile In objFolder.Files
Range("A" & loopCount).Value = objFile.Name
Range("B" & loopCount).Value = objFSO.GetFileVersion(objFile)
Range("D" & loopCount).Value = objFile.Name
'The date modified time for files made in Summer Time are correct, whereas Winter Time will be 1 hour forward
If (IsItSummerTime(objFile.DateLastModified) = True) Then
Range("C" & loopCount).Value = objFile.DateLastModified
Else
modDate = Format(objFile.DateLastModified, "DD-MM-YYYY")
modHour = Hour(objFile.DateLastModified)
modMin = Minute(objFile.DateLastModified)
modHour = modHour - 1
If (modHour < 10) Then
If (modMin < 10) Then
Range("C" & loopCount).Value = modDate & " 0" & modHour & ":0" & modMin
Else
Range("C" & loopCount).Value = modDate & " 0" & modHour & ":" & modMin
End If
Else
If (modMin < 10) Then
Range("C" & loopCount).Value = modDate & " " & modHour & ":0" & modMin
Else
Range("C" & loopCount).Value = modDate & " " & modHour & ":" & modMin
End If
End If
End If
'Combine Version and Modified
If Range("B" & loopCount).Value <> "" Then
Range("E" & loopCount).Value = Range("B" & loopCount).Value & ", " & Range("C" & loopCount).Value
Else
Range("E" & loopCount).Value = Range("C" & loopCount).Value
End If
loopCount = loopCount + 1
Next
'Set up headings
Range("A" & 1).Value = (loopCount - 3) & " files found in " & path
Range("A" & 2).Value = "FileName"
Range("B" & 2).Value = "Version"
Range("C" & 2).Value = "Modified"
Range("D" & 2).Value = "FileName"
Range("E" & 2).Value = "Version & Modified"
End Sub
Function IsItSummerTime(inDate As Date) As Boolean
Dim inDateYear As Integer
Dim findFirstSunday As Date
Dim firstSundayDate As Date
Dim startDays As Integer
Dim endDays As Integer
Dim summerStart As Date
Dim summerEnd As Date
'Summer Time starts on the 13th week
'Summer Time ends on the 42nd week
If (IsItALeapYear(inDate) = True) Then
startDays = (12 * 7) + 1
endDays = (42 * 7) + 1
Else
startDays = 12 * 7
endDays = 42 * 7
End If
'Find the date of the first Sunday in the year
inDateYear = Year(inDate)
For i = 1 To 7
findFirstSunday = DateSerial(inDateYear, 1, i)
If (Weekday(findFirstSunday) = 1) Then
firstSundayDate = findFirstSunday
End If
Next i
'Calculate the start and end dates for Summer Time
summerStart = firstSundayDate + startDays
summerEnd = firstSundayDate + endDays
'Compare inDate to Summer Time values and return boolean value
If (inDate >= summerStart And inDate < summerEnd) Then
IsItSummerTime = True
Else
IsItSummerTime = False
End If
End Function
Function IsItALeapYear(inDate As Date) As Boolean
If (Month(DateSerial(Year(inDate), 2, 29))) = 2 Then
IsItALeapYear = True
Else
IsItALeapYear = False
End If
End Function
Sub-GetFileDetails()
将路径设置为字符串
作为对象的Dim objFSO
Dim objFile作为对象
将文件夹变暗为对象
Dim循环计数为整数
作为布尔值的Dim路径检查
将日期改为日期
Dim modHour作为整数
Dim modMin作为整数
'提示输入目录路径
路径=输入框(提示:=“输入文件路径”,标题:=“输入文件路径”,默认值:=”)
如果(path=”“或path=vbNullString),则
MsgBox(“无效路径-正在退出”)
出口接头
如果结束
'与文件系统交互时需要
设置objFSO=CreateObject(“Scripting.FileSystemObject”)
设置objFolder=objFSO.GetFolder(路径)
'路径标题的第一行,列标题的第二行
循环计数=3
对于objFolder.Files中的每个objFile
范围(“A”&loopCount).Value=objFile.Name
Range(“B”和loopCount).Value=objFSO.GetFileVersion(objFile)
范围(“D”&loopCount).Value=objFile.Name
'在夏季制作的文件的日期修改时间是正确的,而冬季时间将提前1小时
如果(IsItSummerTime(objFile.DateLastModified)=True),则
范围(“C”&loopCount).Value=objFile.DateLastModified
其他的
modDate=格式(objFile.DateLastModified,“DD-MM-YYYY”)
modHour=Hour(objFile.DateLastModified)
modMin=min(objFile.DateLastModified)
modHour=modHour-1
如果(modHour<10),则
如果(modMin<10),则
范围(“C”和循环计数)。值=modDate&“0”和modHour&“0”和modMin
其他的
范围(“C”和循环计数)。值=modDate&“0”&modHour&“:”&modMin
如果结束
其他的
如果(modMin<10),则
范围(“C”和循环计数)。值=modDate&“&modHour&“:0”&modMin
其他的
范围(“C”和循环计数)。值=modDate&“&modHour&“:”&modMin
如果结束
如果结束
如果结束
'合并版本和修改
如果范围(“B”和loopCount).Value为“”,则
范围(“E”和循环计数)。值=范围(“B”和循环计数)。值&“,”和范围(“C”和循环计数)。值
其他的
范围(“E”和循环计数)。值=范围(“C”和循环计数)。值
如果结束
loopCount=loopCount+1
下一个
"设题",
范围(“A”&1).Value=(loopCount-3)和“在中找到的文件”&path
范围(“A”&2).Value=“文件名”
范围(“B”和“2”).Value=“版本”
范围(“C”和“2”).Value=“已修改”
范围(“D”和“2”).Value=“文件名”
范围(“E”和“2”).Value=“版本和修改”
端接头
函数IsItSummerTime(inDate作为日期)为布尔值
Dim inDateYear作为整数
Dim findFirstSunday作为日期
Dim firstSundayDate作为日期
Dim startDays为整数
Dim endDays作为整数
开始日期
以日期结束
“夏季从第13周开始
“夏季在第42周结束
如果(IsItALeapYear(inDate)=True),则
开始日期=(12*7)+1
结束日=(42*7)+1
其他的
开始日期=12*7
endDays=42*7
如果结束
'查找一年中第一个星期日的日期
inDateYear=年份(inDate)
对于i=1到7
findFirstSunday=DateSerial(inDateYear,1,i)
如果(工作日(findFirstSunday)=1),则
firstSundayDate=findFirstSunday
'1st row for path title, 2nd row for column headings
loopCount = 3
For Each objFile In objFolder.Files
Range("A" & loopCount).Value = objFile.Name
'use the full path name
Range("B" & loopCount).Value = FileDateTime(objFile_fullpathname)
Range("D" & loopCount).Value = objFile.Name