[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Windows Scripting: FileSystemObject properties inconsistent with Windows Explorer values

Posted on 2010-01-04
8
Medium Priority
?
573 Views
Last Modified: 2013-11-25
I have  some VBA code that loops through the files in a folder and copys a file to another location if it meets date criteria. I am finding that the DateCreated and DateLastModified of the files as returned by FileSystemObject (file.DateCreated, file.DateLastModified) disagree with the created and modified dates displayed in Windows Explorer. This is the case for all of the files I have compared in the folder I am working with. For example, for one of the files,

                      Explorer                    FileSystemObject
Created         1/4/2010                   12/31/2009
Modified         1/4/2010                   9/3/09

The values shown by Explorer are correct - I have no idea where the FileSystemObject values are coming from.

Does anyone have any idea what's going on here?

<snip>
If Weekday(Date) = vbMonday Then
  CompareDate = DateAdd("d", -3, Date)
Else
  CompareDate = DateAdd("d", -1, Date)
End If
For Each f In fldr.Files
  If f.DateLastModified >= CompareDate Then
    FileCopy f, "c:\Test\" & f.Name
  End If
Next f
<snip>

Open in new window

0
Comment
Question by:dgmoore1
  • 5
  • 3
8 Comments
 
LVL 59

Expert Comment

by:Bill Prew
ID: 26171551
Might need to see more of your code, I can the following VBS script and it matched the dates in both CMD and Explorer.

sSourceFolder = "c:\temp"
sExt = LCase(".txt")
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder(sSourceFolder)
Set oFiles = oFolder.Files
If oFiles.Count > 0 Then
  For Each oFile in oFiles
    If sExt = LCase(Right(oFile.Name, 4)) Then
      Wscript.Echo oFile.Name & ", " & oFile.DateCreated & ", " & oFile.DateLastModified
    End If
  Next
End If
Set oFiles = Nothing
Set oFolder = Nothing
Set oFile = Nothing

~bp
0
 

Author Comment

by:dgmoore1
ID: 26171606
That's pretty much all there is to the code. Here's the whole thing (VBA):

Dim fso As New FileSystemObject
Dim fldr As Folder
Dim f As File
Dim CompareDate As Date
Dim TotFiles As Integer, I As Integer
Dim RetVal

Set fldr = fso.GetFolder("H:\")
TotFiles = fldr.Files.Count

If Weekday(Date) = vbMonday Then
  CompareDate = DateAdd("d", -3, Date)
Else
  CompareDate = DateAdd("d", -1, Date)
End If

For Each f In fldr.Files
  If f.DateLastModified >= CompareDate Then
    FileCopy f, "c:\Test\" & f.Name
  End If
  I = I + 1
  RetVal = SysCmd(acSysCmdSetStatus, "Processed file " & I & " of " & TotFiles & "...")
Next f

RetVal = SysCmd(acSysCmdClearStatus)

err_Copy:
If Err <> 0 Then
  MsgBox Err & " " & Err.Description
  Resume
End If
0
 

Author Comment

by:dgmoore1
ID: 26171627
When I look at the properties of file f.Name in Windows Explorer the Created and Modified dates are different from the dates returned by FileSystemObject
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 59

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 26172026
Agreed, your code is straight forward.  Not sure what the cause is, but I'll throw out a few more thoughts, questions.

How are you "displaying" the dates that the script sees for those files, with a msgbox, or some other way?  

Are you absolutely sure you are comparing the same file's dates in Explorer versus VBA (I know, I felt bad even asking it, but trying to check all possibilities)?

I assume H:\ is a network share drive?  If so, maybe that is part of the puzzle.  Have you tried doing a test against a file on say the C: drive to see if the same problem exists there?

Also, if H:\ is a network drive, could you try the code right on that server that hosts the file, as well as check it's timestamps with Explorer right on the box.

As another data point, try checking the timestamps from a CMD prompt as well to see what it agrees to.  The /T switch let's you see the various dates one by one, so you could do:

DIR /TC h:\testfile.txt
DIR /TW h:\testfile.txt

~bp
0
 

Author Comment

by:dgmoore1
ID: 26172653
Good suggestions - I'll try them out. To display the dates FileSystemObject is returning I have used:

 -MsgBox
 -Hover cursor over variable in code
 -Print file DateCreated and DateLastModified to a text file

All 3 of these return the same values for each file, which are inconsistent with what I see in the file properties when I right click the corresponding file name in Windows explorer.

H: is a Sharepoint folder on a network drive. I haven't done a comparison directly on my c: drive - I'll give that a try as well.

I'll get back with results, but it may be a day or so before I can get this all done.  Many thanks.
0
 

Author Comment

by:dgmoore1
ID: 26173736
I found the problem - the server drive is mapped to a logical H: drive using WebDrive. Turns out WebDrive maintains a cache that is not automatically synced with the server unless set to do so. I have changed the cache setting and now all is well.

Your troubleshooting suggestions were excellent, so the points are yours. Many thanks!
0
 

Author Closing Comment

by:dgmoore1
ID: 31672402
Although I found the solution myself, billprew's suggestions were excellent.
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 26174067
Excellent, glad you tracked it down, and glad my thoughts were helpful, thanks for the points.

~bp
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Loops Section Overview

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question