Results 1 to 7 of 7

Thread: [RESOLVED] Deleting files older than x [Resolved]

  1. #1

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945

    Resolved [RESOLVED] Deleting files older than x [Resolved]

    I have the following code:
    VB Code:
    1. Dim fs, f
    2.     Set fs = CreateObject("Scripting.FileSystemObject")
    3.    
    4.     For i = 0 To CInt(NUMFOLDERS)
    5.         While Dir(FARR(i) & "\*.BAK") <> ""
    6.             filespec = Dir(FARR(i) & "\*.BAK")
    7.             If filespec <> "" Then
    8.                 Set f = fs.GetFile(FARR(i) & "\" & filespec)
    9.                 mdate = f.DateLastModified
    10.                 If mdate < DateAdd("d", -14, Format(Now, "mm/dd/yyyy")) Then
    11.                     txtLog.Text = txtLog.Text & "delete: " & filespec & vbCrLf
    12.                 End If
    13.             End If
    14.         Wend
    15.     Next

    I'm just dumping the output to at textbox at the moment for testing, but the problem is that it never moves to the next file... it keeps running the same file in the while loop. Why doesn't it move to the next one?
    Last edited by ober0330; Aug 29th, 2005 at 09:09 AM.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  2. #2
    Fanatic Member space_monkey's Avatar
    Join Date
    Apr 2005
    Location
    神と歩くこと
    Posts
    573

    Re: Deleting files older than x

    Never mind


    THT
    Using VB6 or VB.net 2008 with .net 3.5
    "Life... death... either way I'll be confined to a small cubicle!" - Hermes Conrad

  3. #3

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945

    Re: Deleting files older than x

    "i" is incremented in the for loop. I'm not worried about that. The Dir function is where my problem is.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  4. #4
    Fanatic Member space_monkey's Avatar
    Join Date
    Apr 2005
    Location
    神と歩くこと
    Posts
    573

    Re: Deleting files older than x

    Yeah i didn't see that for loop till i had already posted

    How are you breaking the while loop?
    Using VB6 or VB.net 2008 with .net 3.5
    "Life... death... either way I'll be confined to a small cubicle!" - Hermes Conrad

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Deleting files older than x

    The syntax looks about right. Try Dir$ instead.

  6. #6
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796

    Re: Deleting files older than x

    If you use Dir() with a wild card, it will return the first matching file every time i.e. always the same file.

    You need to call it repeatedly without any parameters to get subsequent files. After it has done the last one it will return null.

    Try this:

    VB Code:
    1. Dim fs, f
    2.     Dim sFileName As String
    3.     Set fs = CreateObject("Scripting.FileSystemObject")
    4.    
    5.     For i = 0 To CInt(NUMFOLDERS)
    6.         sFileName = Dir$(FARR(i) & "\*.BAK")
    7.         While sFileName <> ""
    8.  
    9.             'Do what you need
    10.  
    11.             sFileName = Dir$()
    12.         Wend
    13.     Next
    Last edited by BrianHawley; Aug 29th, 2005 at 08:37 AM.
    Brian
    (Fighting with the RightToLeft bugs in VS 2005)

  7. #7

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945

    Re: Deleting files older than x

    Ahh... thanks Brian. That's kinda what I was thinking. It doesn't really matter anymore anyways, because what I was trying to do was keep my SQL Server backups deleted (< 2 or 3 weeks) and I figured out how to do it in my server maintenance plan.

    Thanks anyways.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

Posting Permissions

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



Click Here to Expand Forum to Full Width