+ Reply to Thread
Results 1 to 4 of 4

If any file in repository contains string...

Hybrid View

  1. #1
    Forum Contributor JP Romano's Avatar
    Join Date
    10-09-2008
    Location
    Princeton, NJ
    MS-Off Ver
    2010
    Posts
    500

    If any file in repository contains string...

    Good day... seems like I ask for more help than I can offer when I visit the forum...but I have another one.

    I've got a macro that kills all the files in a repository before proceeding (the macro creates files and dumps them there, and I want it to be clean at the start). The problem is, other people are going to be using the macro, and may not check the repository before they start.

    I'd like to have a bit of code that reads the content of the directory and kills the files if they were created any day prior to the current date. If any of you have anything like that handy, I'd appreciate having a look at it. I'm a pretty low level vba guy, but I may be able to make modifications...

    Thank you for any help.

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: If any file in repository contains string...

    Hi there,

    See if the following code does what you want:

    
    
    Sub ClearOldFiles()
    
        Dim bFileExists As Boolean
        Dim sFolderName As String
        Dim vaFileName  As Variant
    
        sFolderName = "W:\"
    
        bFileExists = True
    
        vaFileName = Dir$(sFolderName, vbNormal)
    
        While bFileExists = True
    
            If vaFileName = vbNullString Then
    
                  bFileExists = False
    
            Else
    
                  If Int(FileDateTime(sFolderName & vaFileName)) < Date Then
                      Kill sFolderName & vaFileName
                  End If
    
                  vaFileName = Dir
    
            End If
    
        Wend
    
    End Sub
    Change the highlighted value to suit your own requirements.

    Strictly speaking, you don't need this part of the above code, but I had already included it because I wanted to extract the Date part of the value for testing.

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  3. #3
    Forum Contributor JP Romano's Avatar
    Join Date
    10-09-2008
    Location
    Princeton, NJ
    MS-Off Ver
    2010
    Posts
    500

    Re: If any file in repository contains string...

    Greg - thank you! I made one small change so that if it finds ANY files from prior day, it deletes ALL the files.
     
    If Int(FileDateTime(sFolderName & vaFileName)) < Date Then
                      Kill sFolderName & "*.*"
                      End
                  End If
    I won't be able to run it to completion until tomorrow, but I'm pretty sure it will work perfectly, or only need minor tweaks.

    Thank you again!

  4. #4
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: If any file in repository contains string...

    Hi again,

    Many thanks for your feedback and also for the Reputation increase

    Hope it works correctly for you tomorrow - if not, let me know if there's anything I can help you with.

    Best regards,

    Greg M

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 06-24-2015, 11:32 AM
  2. Excel as a Repository of inromation
    By jpjavilinar in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-19-2014, 12:57 PM
  3. Replies: 1
    Last Post: 04-25-2013, 05:07 PM
  4. Linking Code to a separate repository
    By Turtleman10 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-22-2013, 10:50 AM
  5. Document Repository/Tracker Scorecard
    By neox in forum Excel General
    Replies: 0
    Last Post: 01-30-2013, 07:03 AM
  6. Replies: 1
    Last Post: 08-04-2009, 06:42 AM
  7. [SOLVED] excel as a repository
    By aaron.kempf@gmail.com in forum Excel General
    Replies: 16
    Last Post: 06-16-2005, 11:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1