Creating a file list

How would I accomplish the following:

Create an array containing the names of all files in a directory.
Delete each file contained in that array from the directory.
Keep track of the progress with a progress bar.

I can guess on the last one but creating a list of files is giving me some troubles.




Answer this question

Creating a file list

  • grysauto2002

    Hi - it's me again

    How about this

    Dim filearray() As String

    Dim iCount As Integer = 0

    For Each file As String In My.Computer.FileSystem.GetFiles("file_path")

    ReDim Preserve filearray(iCount)

    filearray(iCount) = file

    iCount += 1

    Next

    You can then loop through the array and delete the files with the

    My.Computer.FileSystem.DeleteFile

    method which will allow you to use various options depending upon how aggressively you wish to delete the files, e.g. ask the user first, send them to the recycle bin or not etc.

    Be aware that there are lots of exceptions which can be generated using these methods so it would be worthwhile having a good read of the help file before trying these.

    Dave


  • fcastell

    I'm going to pass on that one. I'm not aware of any method of determining which process has a file locked and even less idea of how you would shut it down. That's not to say such methods don't exist.

    As a matter of principle I would think interfering with another process like this would not be considered best practice. If the process is under your control you should be able to communicate with it to release the file. If it isn't you shouldn't interfere.

    Dave


  • Yuriy T

    The problem here is that although you appear to have a loop which will add each directory in turn to your list box what in fact is happening is that the call to GetDirectories is only made once. That method then returns a collection of strings and the loop then operates on that collection.

    Until the GetDirectories method returns nothing else will happen, which is what you are seeing.

    Also if the method generates an exception then the whole method fails and there is nothing for the loop to operate on.

    Short of writing your own method for reading the directory list my best suggestion would be to split it down into smaller chunks by just reading the top level directories first. You could then iterate through that collection and collect the sub directories for each top level directory. This would also give you the opportunity to display some sort of progress indication.

    You will still have the problem though that an exception could be generated which would cause the whole thing to fail. I'm not sure at the moment how that could best be handled. Maybe someone else has an idea.

    Dave


  • Bumba

    I understand some of the exceptions like maybe the file is in use and sometimes you can't delete a certain file, but all the files to be deleted were created by me. Also, if a file is in use then is there a way to end the process that is using it, well, safely end it Thanks.


  • Raviatr

    I really can't get this to work. Can somebody help me


  • dvferretm

    With the BackgroundWorker make sure WorkerReportsPrgoress property is set to true.

    Here's an quick overview of how it works:

    1. You can RunWorkerAsync() on the backgroundworker object, and whatever is in it's DoWork event handler executes in another thread. You can put some data in e.Result, which will be passed to RunWorkerCompleted event later on. Note that you can also pass arguments to RunWorkerAsync.

    2. If WorkerReportsPrgoress is enabled and you call ReportProgress(), ProgressChanged event is fired and it's event handler executes on the main thread(good chance for you to update UI)

    3. After DoWork finishes, RunWorkerCompleted event fires, again with the event handler executing on the main thread, and you can do whatever you need to finalize your process.



  • Dr.9

    I've never used the background worker so can't help there but if you were to put an Application.DoEvents inside the loop you may find you don't need it.

    Dave


  • Tellek Liberty

    Protected icount As Integer
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    bgwork.WorkerReportsProgress = True
    bgwork.RunWorkerAsync()
    End Sub

    Private Sub bgwork_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwork.DoWork
    Dim filearray() As String
    Dim icount As Integer = 0

    For Each file As String In My.Computer.FileSystem.GetFiles("d:\")
    ReDim Preserve filearray(icount)
    filearray(icount) = file : icount += 1

    Next
    bgwork.ReportProgress(ProgressBar1.Value)
    End Sub

    Private Sub bgwork_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgwork.ProgressChanged
    total.Text = icount
    End Sub

    where: bgwork is a BackgroundWorker, total is a label, ProgressBar1 is a progress bar.

    I am having trouble understanding the backgroundworker. I looked through help and it just confused me more. For some reason total.text won't update everytime a file is counted. infact it won't update at all. I have to use a background worker because if I don't then the form won't update until it is done counting. Unless I am missing something.



  • baty

    Ok, I have this so far:

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Me.Show()

    My.Application.DoEvents()
    Try
    For Each file As String In My.Computer.FileSystem.GetDirectories _
    ("c:\", FileIO.SearchOption.SearchAllSubDirectories)
    My.Application.DoEvents()
    total.Text += 1
    ListBox1.Items.Add(file)
    Next
    Catch ex As Exception

    End Try
    End Sub

    Now the Application.DoEvents() works but only after the program has searched all the directories. Before the search is complete it just sits and does nothing. As soon as the search is complete then it lists the directories (or files). Is there a reason it does this And is there a way to prevent this Also, when an exception is thrown (when it tries to search the System Volume Information folder, for example) and I catch the exception the program stops. Is there a way to make it keep going even though an exception is thrown.



  • Creating a file list