Loop Through Files in Folder and check DateLastModified

Important Notice


Please note that on 14th December 2023 users will experience an expected outage whilst we make upgrades to our network. We anticipate this process may take a couple of hours and so we apologise in advance for any inconvenience.

  • Hi All


    I am at a loss.


    I have a piece of code that loops through files in a given folder, and does stuff.


    However, before any of that I would like to Loop through the same folder and check that the DateLastModified date (Month only) is the same as the current month.


    If a file is not (ie it is older) then I do not want any of the other loop to work (for any of the files), that is there will be no changes. All files need to have a datelastmodified of current month. (An all or nothing idea).


    My Current Code is:


    Thanks

  • Re: Loop Through Files in Folder and check DateLastModified


    A simple function to loop through the folder using the FileDateTime function along with the DIR function to get a list of all files to make sure everything is dated this month should do it. Pseudo-code (in that it was typed freehand, is untested and may not be syntactically correct) ...



    You add a check before the main loop

    Code
    If not AllFilesDatedThisMonth() then Exit sub '// (Or whatever)
  • Re: Loop Through Files in Folder and check DateLastModified


    Hi Cytop
    Thanks for that, I managed to get the following:



    I think this works too.

  • Re: Loop Through Files in Folder and check DateLastModified


    It'll work - my method is just me preferring to rely on inbuilt VBA functions rather than an external library. One less layer of complication to break.

  • Re: Loop Through Files in Folder and check DateLastModified


    Hi Cytop


    I have just been doing some testing, and I am not sure my code is working as I expected.


    I have modified my code


    So i have removed all the current files, and created a new one (therefore should continue to "Do if Check Ok"), but it does the failing code.


    Also, if there is more than one file, and say the second file does not have a Date Modified of Current Month (Sept) then code (which I have removed)


    Code
    MsgBox MyFile & " Has Failed", vbinformation


    gives me the name of the first file in the folder, not the failed one.


    Thanks

  • Re: Loop Through Files in Folder and check DateLastModified


    I think the error is datelastmodified.


    I have just created a new workbook test, that gives me the Date modified of all Files.


    While Windows Explorer shows a Date modified of "09", the message I get says "08"


    According to Windows Explorer the Date Created is also "09"

  • Re: Loop Through Files in Folder and check DateLastModified


    Of course, me saying "it will work" was on the same basis as my code - untested, but in general it would work (except for the logic errors :))


    Never mind... going back to my preferred method using native VBA functions, the following will list all files in a directory where the month is not equal to the current month. Optionally, you can pass it a File Spec ("*.XLS?") to limit the checking to just Excel workbooks, for example.


    It has expanded a little, but that's only to do with listing the 'invalid' files. It will list up to 15 'invalid' files. If there are more, it will append an "and x other(s)..." message to the output



    You call it using something like

    Code
    If Not AllFilesDatedThisMonth("c:\temp") Then Exit Sub

    where you pass the directory to check.


    If you want to limit the checking to a certain file mask, then use

    Code
    If Not AllFilesDatedThisMonth("c:\temp", "*.XLS?") Then Exit Sub
  • Re: Loop Through Files in Folder and check DateLastModified


    Thanks.


    My question is though - why would


    Code
    Set f = fs.getfile(MyPath & MyFile)
    filemoddate = f.datelastmodified


    not return the date according to Windows Explorer?


    If I am honest, I am not sure how to impliment your first Function into My Original Code Also, I need it to look at DateLastModified, as the file is overwritten each month (therefore the Date Created does not change)


    Thanks

  • Re: Loop Through Files in Folder and check DateLastModified


    Ok,


    After searching the excellent OzGrid, I have come up with this:



    I believe this to work, on my new test sheet. Now to impliment onto live (GULP!!!)


    Thanks again

  • Re: Loop Through Files in Folder and check DateLastModified


    Hi..


    Give this a try just for fun... i 'think' it does the job..


    Code
    Private Sub CommandButton1_Click()
        Dim smonth As Date, myPath As String
        myPath = "C:\Text\"
        smonth = CDate(1 * DateSerial(Year(Date), Month(Date), 1))
        If UBound(Filter(Split(CreateObject("wscript.shell").exec("cmd /c forfiles /P " & myPath & " /D -""" & smonth & """").stdout.readall, vbCrLf), ".")) <> -1 Then
            MsgBox "No Good": Exit Sub
        End If
    End Sub

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!