Results 1 to 9 of 9

Thread: what about this 4 line of code

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2014
    Posts
    169

    what about this 4 line of code

    friends;
    i write this samall line of code to delete files in history less than 10 days from current computer date
    is this code good

    Code:
    Dim dirs As New System.IO.DirectoryInfo("C:\Myfolder")
    For Each file In dirs.GetFiles()
    If file.CreationTime.Date < DateTime.Today.AddDays(-10) Then
    file.Delete()
    End If
    Next

  2. #2
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,612

    Re: what about this 4 line of code

    There's nothing obviously wrong with it. Have you tested it? What are your concerns?
    You can depend upon the Americans to do the right thing. But only after they have exhausted every other possibility - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2014
    Posts
    169

    Re: what about this 4 line of code

    Quote Originally Posted by FunkyDexter View Post
    There's nothing obviously wrong with it. Have you tested it? What are your concerns?
    FunkyDexter
    thanks for the answer
    i compare between 2 date using < i want to insure that I'm in save side

  4. #4
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,612

    Re: what about this 4 line of code

    Yes, you're fine with that. Dates (including their time element) have a valid concept of greater than, less than and equality. You could also use the DateDiff function for something like this, if you prefer, but you're way is perfectly valid.
    You can depend upon the Americans to do the right thing. But only after they have exhausted every other possibility - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,394

    Re: what about this 4 line of code

    Quote Originally Posted by FunkyDexter View Post
    Yes, you're fine with that. Dates (including their time element) have a valid concept of greater than, less than and equality. You could also use the DateDiff function for something like this, if you prefer, but you're way is perfectly valid.
    what about modifying the collection in a For Each loop?

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,235

    Re: what about this 4 line of code

    Yeah, that should cause a "Collection was changed" exception. You have to use a For...Next loop if you are iterating through a collection and removing items. Furthermore, you have to iterate from the last to the first:
    Code:
    Dim fileSet = dirs.GetFiles()
    
    For x= fileSet.Length-1 To 0 Step -1
     If fileSet(x).CreationTime.Date < DateTime.Today.AddDays(-10) Then
       fileSet(x).Delete()
     End If
    Next
    My usual boring signature: Nothing

  7. #7
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,282

    Re: what about this 4 line of code

    Are you changing the collection, though? A file is deleted from the hard drive, but I'd not expect that to cause its entry to be removed from the array of FileInfos. The collection isn't linked to the drive in real time.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,394

    Re: what about this 4 line of code

    Quote Originally Posted by Inferrd View Post
    Are you changing the collection, though? A file is deleted from the hard drive, but I'd not expect that to cause its entry to be removed from the array of FileInfos. The collection isn't linked to the drive in real time.
    You're right. But I had to test it to be sure

  9. #9
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,612

    Re: what about this 4 line of code

    what about modifying the collection in a For Each loop?
    I didn't even consider that which is why I asked the OP what his concerns were. My answer was purely to do with his use of < against a date.

    I wouldn't have had a clue whether this would have constituted modifying the collection without testing for it either but I'm glad it turned out I was right by default
    You can depend upon the Americans to do the right thing. But only after they have exhausted every other possibility - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

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