|
-
Dec 31st, 2011, 10:18 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Read File Extensions in a FileListBox
VB6 Code
Is it possible to read the file extensions in a FileListBox.
The FileListBox when it displays files from a folder there can be 2 file extension in the folder *.mp3 and *.txt. The *.txt extension do not appear in all folders.
So what I would like to do as I scroll though the folders.
Display in the FileListBox all the file extension available but when a *.txt extension is present disable the FileListBox Click Event make the Delete Button Enabled = True delete the *.txt entry then reverse the events and continue.
-
Dec 31st, 2011, 10:26 AM
#2
Frenzied Member
Re: Read File Extensions in a FileListBox
here is some theory:
a file listbox is a listbox that shows the files in a given folder. you can check for file names and extensions when the folder is changed
once you find the required extension (*.txt), you set a flag in click event to ignore everything and then enable the delete button.
once the delete button is clicked, recheck the files and if none of the .txt files exists, set the flag in the click event to stop ignoring.
hope that is clear enough to get you started.
-
Dec 31st, 2011, 10:42 AM
#3
Thread Starter
Hyperactive Member
Re: Read File Extensions in a FileListBox
Hi mebhas
What is the File1.xxxxx that will allow me to check the extension, need more help please.
Set a Flag to stop the click event not sure how to do that.
I did look at your posted links but not see any code that explained how to start what I want to do.
-
Dec 31st, 2011, 04:23 PM
#4
Re: Read File Extensions in a FileListBox
Wouldn't a simpler approach be to
1) Set the filelistbox filter to show only *.txt files. Then loop thru and do what you want with them
2) Remove/Change the filter and refresh the listbox
Also, if your goal is to delete all the .txt files, why not just do that before the filelistbox is displayed? Maybe more details on what you are doing could help enlighten us?
-
Jan 1st, 2012, 03:28 AM
#5
Thread Starter
Hyperactive Member
Re: Read File Extensions in a FileListBox
Hi LaVolpe
Happy New Year
In answer to the questions Yes I do want to delete all of the *.txt entries in the music files these *.txt files are created when I make playlists so it would have to be done about once a week. Would I want to run this every time I opened the the project to create a list I am not sure as I create lists 3-4 times a week. Thinking about it maybe if I had a separate form and some type of calendar reminder it would be OK.
I did try and select only the *.txt files first but I could not seem to solve the problem of how to move to the next folder when no *.txt files were present at the moment I use the File1_Click event to move to the next folder.
I hope this explains what I want to do which will allow you to help me. This project would be dead in the water without your help anyway, so if you can help me further I will be very grateful.
-
Jan 1st, 2012, 10:09 AM
#6
Frenzied Member
Re: Read File Extensions in a FileListBox
is the file structure fixed from a root point
i.e.
root
+dir1
+dir2
-dir 3
file1.mp3
file1.txt
file2.mp3
...
+dir4
..
+dirN
names different of course
-
Jan 1st, 2012, 10:54 AM
#7
Thread Starter
Hyperactive Member
Re: Read File Extensions in a FileListBox
Hi Incidentals
Happy New Year
In answer to your question regarding Structure I am unsure how to tell you.
The Project consists of 1 Form and 3 Modules the *.mp3 Folders are kept on a Hard Drive Alpha "G"
The total collection is in a Sub-Folder called "My Music" with each Artists Name as a Sub-Folder these Sub-Folders can contain one *.mp3 file or 30 - 50 *.mp3 files.
When I create PlayLists some of those Artists Sub-Folders get *.txt files added to them.
In order to keep the size of My Music Folder down and because I have no interest in them I want to delete them.
Hence my problem, the Project I have works OK and does what I want but in order to clear all of the *.txt files I have to Click a Button 953 times (at the moment adding Artists all the Time) to complete the job.
To improve the Project if I could detect when a sub-folder has a *.txt file this would reduce the problem by about 45%.
Or as LaVolpe has suggested use File1.Pattern to list only the *.txt files delete them then display the *.mp3 ones.
Good idea but I could not find a way to keep position in the sub-folder list and move to the next Artists folder without using the File1_Click Event which
remains static if no files are present, stuck again.
Does this information help?
-
Jan 2nd, 2012, 11:56 AM
#8
Thread Starter
Hyperactive Member
Re: Read File Extensions in a FileListBox
Hi
Can anybody else help me with this post or is what I wanted to do not possible
-
Jan 2nd, 2012, 01:23 PM
#9
Re: Read File Extensions in a FileListBox
Suggestion.
1) Test if there are any .txt files: Dir(yourfolder & "\*.txt", vbArchive or vbReadOnly) for example
2) If the result is not a zero length string, then delete them all: Kill yourfolder & "\*.txt"
Note that if any are read only, you'll get an error because Kill won't delete read-only files. If this is the case, you'll want to use either APIs to delete all the .txt files, in one call, or loop thru them, resetting the read-only attribute with SetAttr() and then delete them all
-
Jan 2nd, 2012, 01:24 PM
#10
Re: Read File Extensions in a FileListBox
Something to play with, perhaps
Code:
'
' dr is a Drive Control
' Dir is a DirListBox
' fl is a FileListBox
'
Private Sub cmdDelete_Click()
Call DeleteTxt
End Sub
Private Sub Dir_Change()
fl.Path = Dir.Path
End Sub
Private Sub dr_Change()
Dir.Path = dr.Drive
End Sub
Private Sub fl_PathChange()
Dim boTxt As Boolean
'
' Check for .txt files in the directory
'
boTxt = CheckTxt
If boTxt Then
'
' There are some so don't allow user to navigate away
'
Dir.Enabled = False
dr.Enabled = False
fl.Enabled = False
cmdDelete.Enabled = True
End If
End Sub
Private Function CheckTxt() As Boolean
Dim intPos As Integer
Dim intI As Integer
'
' Check if there are any .txt files in this folder
'
Do
intPos = InStrRev(fl.List(intI), ".")
If intPos > 0 Then
If UCase(Mid$(fl.List(intI), intPos + 1, 3)) = "TXT" Then
CheckTxt = True
End If
End If
intI = intI + 1
Loop Until intI > fl.ListCount Or CheckTxt = True
End Function
Private Sub DeleteTxt()
Dim intPos As Integer
Dim intI As Integer
Dim lngRet As Long
'
' Ask user if it's ok to delete all the .txt file
'
lngRet = MsgBox("Delete all .txt Files ?", vbYesNo, "Housekeeping")
If lngRet = vbYes Then
Do
intPos = InStrRev(fl.List(intI), ".")
If intPos > 0 Then
If UCase(Mid$(fl.List(intI), intPos + 1, 3)) = "TXT" Then
'kill Dir.Path & fl.list(inti)
Debug.Print "Would have deleted "; Dir.Path & fl.List(intI)
End If
End If
intI = intI + 1
Loop Until intI > fl.ListCount
End If
'
' Re-enable navigation, disable Delete Button
'
Dir.Enabled = True
dr.Enabled = True
fl.Enabled = True
cmdDelete.Enabled = False
End Sub
You perhaps need to set the 'home' drive and path in the form load event
Last edited by Doogle; Jan 2nd, 2012 at 01:36 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|