Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7830533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:16:33+00:00 2026-06-02T11:16:33+00:00

I am creating a console app that will delete pictures from a directory every

  • 0

I am creating a console app that will delete pictures from a directory every 30 minutes. Problem is that its being populated by files every minute or so. So if I go and delete files in that directory then it may cause an error trying to delete a file thats being created just then or opened.

I currently have this code to copy the files to another directory and then delete them from the source directory.

Dim f() As String = Directory.GetFiles(sourceDir)

    For i As Integer = 0 To UBound(f)
        'Check file date here in IF statement FIRST...

        File.Copy(f(i), destDir & f(i).Replace(sourceDir, ""))

        If File.Exists(f(i)) = True Then
              File.Delete(f(i))
        End If

        Debug.Print(f(i) & " to >>> " & destDir & f(i).Replace(sourceDir, ""))
    Next

How can I use:

File.GetCreationTime(f(i))

in an IF statement checking IF the currently file its on is newer than 30 seconds ago?

OR

Is there a way of only populating:

 Dim f() As String = Directory.GetFiles(sourceDir)

with only those files that are more than 30 seconds old?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T11:16:35+00:00Added an answer on June 2, 2026 at 11:16 am

    There isn’t a reliable way to detect if a file is locked or not. Even if you did find out (it is technically possible), it could be locked before you tried to delete it. There are other reasons a delete may fail. In your case, I don’t think it matters what the reason was.

    The only way is to put the call to delete in a try/catch and trap IOException, and then retry if you want.

    You need to use a FileInfo object to get the CreatedTime and compare to Now. You can also use LastAccessTime or LastWriteTime, but since these are all new files being written then, you don’t need to.

    Private Sub DeleteFiles()
        Dim files = From f In Directory.GetFiles("c:\temp")
                    Let fi = New FileInfo(f) 
                    Where fi.Exists AndAlso fi.CreationTime <= DateTime.Now.AddSeconds(-30)
    
        For Each f In files
            Try
                f.Delete()
            Catch ex As Exception
                If TypeOf ex Is IOException AndAlso IsFileLocked(ex) Then
                    ' do something? 
                End If
                'otherwise we just ignore it.  we will pick it up on the next pass
            End Try
        Next   
    End Sub
    
    Private Shared Function IsFileLocked(exception As Exception) As Boolean
        Dim errorCode As Integer = Marshal.GetHRForException(exception) And ((1 << 16) - 1)
        Return errorCode = 32 OrElse errorCode = 33
    End Function
    

    IsFileLocked function lifted from this other thread on SO

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to create an app that will read in some info from a
I am creating a desktop app that will sit on a PC and occasionally
I'm creating a little console application that creates a Lucene index from an Sql
I am creating a small console app that needs a progress bar. Something like...
I am creating an HTML5 app that will display a bunch of shapes in
I've been assigned the project of creating a simple console app. that models brownian
I am creating console app, involving lots of services. I want to display a
I am creating a Console Application, in that I need to Call a Method,
I'm creating a v8 shell based console, I took the sample code that cames
I have a fairly simple console app that monitors an exchange mailbox, picks particular

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.