How to delete files from folder NOT listed in listbox?

louisryko 21 Reputation points
2023-01-22T21:49:21.6233333+00:00

Hi all,

Been toying around with this for a long time to no avail...

I have a listbox of items generated by a folder contents (location x, they're all *.mp3).

I have another listbox of items generated by a different folders contents (location y, they're all *.txt files)

(The listboxes are not necessary, I'm just a newbie and these help me wrap my head around the problem by breaking it down into manageable pieces - if it can be done without them I'm happy to oblige)

I want to delete any *.txt files in the folder that DON'T correspond to the names of the mp3 files.

Ie; I have song1.mp3, song2.mp3, song3.mp3 in folder x

and

song1.txt, song2.txt, song3.txt, song4.txt in folder y.

I want to delete song4.txt

Any help much appreciated :)

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,496 questions
0 comments No comments
{count} votes

Accepted answer
  1. LesHay 7,106 Reputation points
    2023-01-22T22:52:59.8766667+00:00

    Hi

    Here is some code that does it. This example uses 'hard coded' file paths (from a CopyAsPath), but you could very easily incorporate a folder browser to allow selection to be made.

    This code can delete files so TEST it with some test files (I just copied some random .txt files and renamed to .mp3 and .txt in appropriate folders (see code at top for my test paths)

    The actual DELETE function is commented out in the code, and only when you have set up a test environment, uncomment the line. I have also put a STOP in the code. When testing for first time, the STOP will allow you to check the values being used, in particular, the variable 'Name' which would be a file about to be deleted. Once happy with all of that, then stop the execution and remove the STOP command and allow the deletion on next run.

    As you said, I have not bothered to add any Lists or anything in the UI, but that too is easily added if needed.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim mp3Path As String = "C:\Users\lesha\Desktop\MP3 Folder"
      Dim txtPath As String = "C:\Users\lesha\Desktop\TXT Folder"
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        ' create a collection of mp3 files
        Dim mp3s() As String = IO.Directory.GetFiles(mp3Path, "*.mp3")
        ' create a collection of txt files
        Dim txts() As String = IO.Directory.GetFiles(txtPath, "*.txt")
    
        ' loop through txt file names and
        ' check if in mp3 collection
        For Each txtFile As String In txts
          ' create a mp3 file name to search
          ' with (needs to be mp3 filename with
          ' a .mp3 extendion
          Dim name As String = IO.Path.Combine(mp3Path, IO.Path.GetFileNameWithoutExtension(txtFile) & ".mp3")
    
          If Not mp3s.Contains(name) Then
            ' no file match in mp3 folder so
            ' can delete this file (the .txt one)
    
            ' this next line would do the delete
            ' but first, try out the code on
            ' some test files to ensure it does
            ' what it is supposed to do! Be careful
            ' to not mix up the paths.
    
            '  IO.File.Delete(txtFile)
            Stop
          End If
        Next
      End Sub
    End Class
    
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. louisryko 21 Reputation points
    2023-01-23T11:00:12.9566667+00:00

    Ok, thanks again. I managed to solve my own question (a comment above) but would still like to know how to write text to the text file when I create it.

    I simply need the text file to read "0" if I create it. I've managed to create them with the IO.File.Create(txtFile) - I reverse engineered your code to some success :)

    Thanks again!

    0 comments No comments

  2. LesHay 7,106 Reputation points
    2023-01-23T12:45:30.1733333+00:00

    Hi

    For your second question. (please put new questions in their own new thread to avoid filling a thread with multiple questions)

    As you will see below, the code is very much like the first code. So much so, that you can quite easily incorporate the 2 code examples into one.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim mp3Path As String = "C:\Users\lesha\Desktop\MP3 Folder"
      Dim txtPath As String = "C:\Users\lesha\Desktop\TXT Folder"
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' what to write to new file (with
        ' a ctlf - delete if not needed)
        Dim texttowrite As String = "0" & vbCrLf
    
        ' create a collection of mp3 files
        Dim mp3s() As String = IO.Directory.GetFiles(mp3Path, "*.mp3")
        ' create a collection of txt files
        Dim txts() As String = IO.Directory.GetFiles(txtPath, "*.txt")
    
        ' loop through mp3 file names and
        ' check if in txt collection
        For Each txtFile As String In mp3s
          ' create a txt file and write the
          ' text from above to it.
          Dim name As String = IO.Path.Combine(txtPath, IO.Path.GetFileNameWithoutExtension(txtFile) & ".txt")
          If Not txts.Contains(name) Then
            ' create a new file with contents
            IO.File.WriteAllText(name, texttowrite)
          End If
        Next
      End Sub
    End Class
    
    
    0 comments No comments