List Searched files in Checked List Box

Ja2984

Member
Joined
Nov 23, 2007
Messages
13
Programming Experience
Beginner
Hi i'm currently planning on developing a program that needs to do a wildcard find/search for files of all types (*.lst *.pdf *.tmt ...etc) based on user input.

So e.g.

User input = 12345

Display all files containing that number in the name:

2D12345.pdf
TF12345.lst
TF12345_1.tmt
etc..

I need to search in several different locations on a network and then list the files in a checked list box displaying full file path.

I need the option to then select all or some of the files in the list and then delete those selected.

I'd appreciate any ideas from any one knows a good way to approach this program.
 
Last edited:
I made a new project and added a CheckedListBox to it, then in the Load event I added:
VB.NET:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim fbd As New FolderBrowserDialog
        With fbd
            .ShowNewFolderButton = False
        End With
        If fbd.ShowDialog(Me) <> Windows.Forms.DialogResult.Cancel Then
            With CheckedListBox1
                .DisplayMember = "Name"
                .ValueMember = "FullName"
                .DataSource = New IO.DirectoryInfo(fbd.SelectedPath).GetFiles("*" & InputBox("Search criteria", "Search criteria") & "*", IO.SearchOption.AllDirectories)
            End With
        End If
    End Sub
I'm using a FolderBrowserDialog to get the directory path, then I use an InputBox (Not recommended, I only used it for a quick example) to get the search criteria, and the results of the GetFiles() method on the directory gets assigned to the checkedlistbox, and I've set it so only the filename is displayed, but each item in there has the entire path. You'll of course need to tweak the above code to have things displayed correctly for your project.

To delete only the selected (Checked) files in the checkedlistbox simply loop through it's checkeditems:
VB.NET:
        For Each Item As FileInfo In CheckedListBox1.CheckedItems
            Try
                If Item.Exists = True Then Item.Delete()
            Catch : End Try
        Next Item
Of course, when they've been deleted you should refresh the checkedlistbox so the user knows they're gone.

Last but not least, to check or uncheck all of the items in the checkedlistbox, simply loop through all of the items and use the checkedlistbox's SetItemChecked() method:
VB.NET:
    Private Sub chklstCheckItems(ByVal isChecked As Boolean)
        For Counter As Integer = 0I To ScenariosCheckedListBox.Items.Count - 1I
            CheckedListBox1.SetItemChecked(Counter, isChecked)
        Next Counter
    End Sub
I put that into a simple sub so all you need to do is pass a True or a False to the sub and it will check or uncheck all the items from there. Very easy to just call that sub from a button or menu item.
 
Hi Juggalo,

I've spent the past few days developing my program based on your helpful code suggestions. :D

Just 1 or 2 things left that i cant quite nut out on my own.

After i've searched for files and i have them listed in my check list box, i want to then rename the files i've checked/selected.

I've got the following code but i am having troubles. I need to rename the files by either changing the extension to say *.Rev or rename to have "_rev" on the end of the name.

e.g. original name of file is : 2D12345.prt

Renamed file to be : 2D12345_rev.prt OR 2D12345.rev

VB.NET:
' ##### Rename Selected CadKey #####
    Private Sub RenamePrtButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RenamePrtButton.Click

        Dim renamePrtDialogBox As System.Windows.Forms.DialogResult
        Dim renamePrtResponseMessageString As String

        renamePrtResponseMessageString = "Rename all Selected CadKey Part Files?"
        renamePrtDialogBox = MessageBox.Show(renamePrtResponseMessageString, _
        "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, _
        MessageBoxDefaultButton.Button2)

        If renamePrtDialogBox = Windows.Forms.DialogResult.Yes Then

            For Each Item As IO.FileInfo In Me.prtCheckedListBox.CheckedItems
                Try
                    If Item.Exists = True Then Item.MoveTo("RenamedFile")
                Catch : End Try
            Next Item

            Call SearchButton_Click(sender, e)
        End If
    End Sub

1 last thing, i want to also list some pdf files in a list box and then open them up in acrobat reader when i double click on them in the list. Is this possible? I can use a button to open the selected item if thats easier.

Thanks once again for your help, and quick help i might add:D
 
If Item.Exists = True Then Item.MoveTo("RenamedFile")

Are you renaming the file in its same location or moving/copying to a new directory with new name?

'Rename File
My.Computer.FileSystem.RenameFile(strOriginalFile, strNewName)

'Leave original file, create copy with new name in another directory
My.Computer.FileSystem.CopyFile(strSourceFile, strDestinationFileName)

'Move & rename file
My.Computer.FileSystem.MoveFile(strSourceFile, strDestinationFileName)

And yes you can trigger an even when double clicking an item in a list box.

Private Sub ListBox1_DoubleClick()
'Code Here
End Sub
 
To rename the files, I think this will do the trick:
VB.NET:
Private Sub RenamePrtButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RenamePrtButton.Click
    If MessageBox.Show("Rename all Selected CadKey Part Files?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
        For Each Item As IO.FileInfo In Me.prtCheckedListBox.CheckedItems
            Try
                If Item.Exists = True Then Item.MoveTo(IO.Path.GetFileNameWithoutExtension(Item.FullName) & ".rev")
            Catch : End Try
        Next Item
        SearchButton.PerformClick()
    End If
End Sub
What it does is uses the FileInfo's MoveTo() method and simply changes the extension from whatever it was to ".rev", so '2D12345.prt' now becomes '2D12345.rev'

As for opening a file by double-clicking on it use Tom's suggestion and to actually open the file use the System.Diagnostic.Process.Start("Listed item's filename here")
 
Thanks guys for your suggestions.

I'm as eager as a little child at christmas to try them but there's that little thing called work that must be done first :rolleyes:

As before i'll check back in with my progress.

Cheers
 
Hi guys i finally found some time to give the renaming a go. SUCCESS:D

I used ur code juggalo and only had to make a minor adjustment cos it was renaming and then moving the files to the debug folder of my project. The code i used:
VB.NET:
Private Sub RenamePrtButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RenamePrtButton.Click

        Dim renamePrtDialogBox As System.Windows.Forms.DialogResult
        Dim renamePrtResponseMessageString As String
        Dim renameprtCount As String

        renameprtCount = Me.prtCheckedListBox.CheckedItems.Count

        Me.PrtProgressBar.Value = 0

        If renameprtCount >= 1 Then

            Dim searchCriteria As String
            Dim FilePathPrt As String
            Dim Location As String
            Dim CommonDrive As String
            Dim FileNoExt As String

            searchCriteria = Me.SearchTextBox.Text.Substring(0, 5)
            CommonDrive = Me.commonDriveComboBox.Text.ToUpper
            Location = searchCriteria.Substring(0, 2)

            If Location < 10 Then
                Location = searchCriteria.Substring(1, 1)
            End If

            FilePathPrt = CommonDrive & ":\Cad\Prt\" & Location & "\"

            renamePrtResponseMessageString = "Rename all Selected CadKey Part Files?"
            renamePrtDialogBox = MessageBox.Show(renamePrtResponseMessageString, _
            "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, _
            MessageBoxDefaultButton.Button2)

            If renamePrtDialogBox = Windows.Forms.DialogResult.Yes Then

                For Each Item As IO.FileInfo In Me.prtCheckedListBox.CheckedItems
                    Try
                        FileNoExt = IO.Path.GetFileNameWithoutExtension(Item.FullName)
                        If Item.Exists = True Then Item.MoveTo(FilePathPrt & FileNoExt & ".rev")
                    Catch : End Try
                Next Item
                SearchButton.PerformClick()
                Me.PrtProgressBar.Value = 100
            End If
        End If

    End Sub

Whether its good code or not it works fine for what i need it for.

I'm yet to have a go at the open pdf from a list box.

Thanks for your help guys.
 
If you use the DoubleClick event:
VB.NET:
Private Sub CheckedListBox1_DoubleClick(...) Handles CheckedListBox1.DoubleClick
  System.Diagnostics.Process.Start(DirectCast(CheckedListBox1.SelectedItem, FileInfo).FullName)
End Sub
If you use a button:
VB.NET:
        For Each Item As IO.FileInfo In Me.prtCheckedListBox.CheckedItems
            If Item.Exists = True Then System.Diagnostics.Process.Start(Item.FullName)
        Next Item
Of course, if you want this to work only for pdf files listed then add a simple check:
VB.NET:
If Item.FullName.ToLower.EndsWith(".pdf") = True Then....
 
Of the 3 different types of people in the world, i'm in the 3rd category.

Hey juggalo sorry to be a pain but I finally finished my project and installed on my workmates computer thinking i'd fully debugged and fix up any little problems only find out that the first number he searched for caused a problem hehe.

I'm having issues when searching for numbers that start with zero. e.g. 01234, 00123, 00012 etc. When stepping through my code in debug mode i noticed it removes all leading zeros from my search criteria leaving just the numbers at the end e.g. 1234, 123, 12.

Is there a way to force the zeros to stay there because 1) i use the first 2 numbers to determine a specific folder to look in when i do my searches and 2) it then will search for all files that inlcude 12 for example, Shows files i'm not really looking for like 01200, 00120, 13312 etc.

e.g.

Search = 01234 ...... looks in folder .../prt/01/
Search = 00123 ...... looks in folder .../prt/00/
Search = 00012 ...... looks in folder .../prt/00/

etc

Hope this dribble makes some kind of sense.

I'm using a text box to type in the search criteria (00012)

I declare this as
VB.NET:
Private searchCriteria As String

The only thing that i think maybe the cause is i've added a try statement in to prevent people typing in non numerical characters.

VB.NET:
searchCriteria = Decimal.Parse(Me.SearchTextBox.Text)

i assume this converts the textbox entry to a decimal, hence the removal of the zeros. Ok maybe i've just answered my own question.

So with all that said is there any easy way round this little problem?

Once again sorry to be a pain and sorry i've written a book :eek:

Thanks in advance,

John
 
If you are using a field that converts and/or stores your value as a number such as an integer, it will remove the leading zeros. You need to ensure it is stored as a string.

It would be more helpful to see exactly where your storing the text value to give more detail of how to fix it.
 

Latest posts

Back
Top