473,440 Members | 1,643 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,440 software developers and data experts.

Searching a folder for certain files

Hello, is there a way to search a folder for certain files containing a wildcard or certain text entered into a text box. Right now all i can get is the whole list of files from a folder using the following code:

lstbox.additems.addrange(IO.directory.getfiles("c: \"))

How can i just return only files with matching text as whats in the text Box. Any help will be greatly appreciated. Thanks.

Matt
Feb 26 '08 #1
15 2414
Dököll
2,364 Expert 2GB
Hello, is there a way to search a folder for certain files containing a wildcard or certain text entered into a text box. Right now all i can get is the whole list of files from a folder using the following code:

lstbox.additems.addrange(IO.directory.getfiles("c: \"))

How can i just return only files with matching text as whats in the text Box. Any help will be greatly appreciated. Thanks.

Matt
Hello spazzo6281!

Here is what I pulled up at microsoft:

Expand|Select|Wrap|Line Numbers
  1. Dim MyFile, MyPath, MyName
  2. ' Returns "WIN.INI"  if it exists.
  3. MyFile = Dir("C:\WINDOWS\WIN.INI")   
  4.  
  5. ' Returns filename with specified extension. If more than one *.ini
  6. ' file exists, the first file found is returned.
  7. MyFile = Dir("C:\WINDOWS\*.INI")
  8.  
  9. ' Call Dir again without arguments to return the next *.INI file in the 
  10. ' same directory.
  11. MyFile = Dir
  12.  
  13. ' Return first *.TXT file with a set hidden attribute.
  14. MyFile = Dir("*.TXT", vbHidden)
  15.  
  16. ' Display the names in C:\ that represent directories.
  17. MyPath = "c:\"   ' Set the path.
  18. MyName = Dir(MyPath, vbDirectory)   ' Retrieve the first entry.
  19. Do While MyName <> ""   ' Start the loop.
  20.    ' Ignore the current directory and the encompassing directory.
  21.    If MyName <> "." And MyName <> ".." Then
  22.       ' Use bitwise comparison to make sure MyName is a directory.
  23.       If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
  24.          Debug.Print MyName   ' Display entry only if it
  25.       End If   ' it represents a directory.
  26.    End If
  27.    MyName = Dir   ' Get next entry.
  28. Loop
  29.  
http://msdn2.microsoft.com/en-us/library/aa262726.aspx

Try it, see if that works for ya;-)

In a bit!
Feb 26 '08 #2
That didn't work, I'm just going to have to keep looking. Thanks for the help though.
Feb 27 '08 #3
Killer42
8,435 Expert 8TB
See here. The getfiles() method supports searching for files matching a wildcard specification.
Feb 27 '08 #4
basti42
18
you can try this

dirs = Directory.GetFiles("C:\\folders", "*" + txtSeek.Text + "*" + ".csv");
foreach (string dir in dirs)
{
listBox1.Items.Add(dir.Substring(34));
strFound = dir;
Directory.CreateDirectory("C:\\save_to);
File.Copy(strFound, "C:\\save_to\\, true);
}
Feb 27 '08 #5
I was finally able to get most of the code figured out with the following:

Dim find As String = txtSearch.Text

For Each foundfile As String In My.Computer.FileSystem.GetFiles("t:\documents", FileIO.SearchOption.SearchAllSubDirectories, find + "*.pdf", find + "*.doc")
lstBox.Items.Add(foundfile)
Next

it works to find files that start with a given letter or the given first work but i won't find items if the word is second or third in the file name. if anyone can help me figure out how to do that I would greatly appreciate it. Thanks for that that helped on my first post.
Mar 11 '08 #6
Killer42
8,435 Expert 8TB
Just concatenate another "*" before find.
Mar 11 '08 #7
Just concatenate another "*" before find.
Thanks, that worked.
Mar 11 '08 #8
Killer42
8,435 Expert 8TB
Thanks, that worked.
Excellent! :)

Glad we could help.
Mar 11 '08 #9
Thanks to everyone that helped with this problem. Now that it's working all the found files come back like "t:\photos\*.jpg" does anybody out there know of a way to remove "t:\photos\" and still have the "process.start(lstbox.text)" function work. I've tried everything that i could think of, and I guess I don't even know if it's possible. If anyone has any ideas I would appreciate it. Thanks.
Mar 12 '08 #10
Killer42
8,435 Expert 8TB
This one should be relatively simple. Just have a look at the different properties available from the File object. I believe you'll find that one provides the name without the path. At present you're just using the default property, which apparently must be the full path.
Mar 13 '08 #11
This one should be relatively simple. Just have a look at the different properties available from the File object. I believe you'll find that one provides the name without the path. At present you're just using the default property, which apparently must be the full path.
I've looked and tried everything i could think of and it still doesn't work. I can use the trimstart feature but then the "process.start(lstbox.text)" doesn't work. I'm currently using "lstbox.items.addrange(IO.directory.getfiles("t:\p hotos"))" to get the list of files but i still can't get path to know show up and have process.start still work. Thanks for trying.
Mar 20 '08 #12
I've looked and tried everything i could think of and it still doesn't work. I can use the trimstart feature but then the "process.start(lstbox.text)" doesn't work. I'm currently using "lstbox.items.addrange(IO.directory.getfiles("t:\p hotos"))" to get the list of files but i still can't get path to know show up and have process.start still work. Thanks for trying.
The path to not show up. sorry for the miss type.
Mar 20 '08 #13
Killer42
8,435 Expert 8TB
How about using the FileSystem.GetDirectories method to return the collection of directory names. Then for each of those, do a FileSystem.GetFiles and concatenate the directory name at the start of each returned filename.
Mar 20 '08 #14
How about using the FileSystem.GetDirectories method to return the collection of directory names. Then for each of those, do a FileSystem.GetFiles and concatenate the directory name at the start of each returned filename.
The program is finally doing what i wanted it to do. The concat method really helped. Thanks to everyone that helped me with my problems.
Mar 23 '08 #15
Killer42
8,435 Expert 8TB
Glad we could help out. :)
Mar 25 '08 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: John | last post by:
Hi everyone ! This is a first time I post a message here. If I post my message in a wrong group. Please ignore it. I am trying to build a website which allows users (can be thousands of...
4
by: Les Juby | last post by:
Can anyone please help with some VBScripting that would enable me to search thru Word documents (Word 2000 format) and return the filenames to use in a archive search on a legal website.? TIA ...
1
by: PHPGB | last post by:
While I know putting a app into a folder was chiseled out of fine scottish granite and carried upto the top of ben nevis by php newbies many years ago - I dont think a folder is the best way to do...
29
by: jaysherby | last post by:
I'm new at Python and I need a little advice. Part of the script I'm trying to write needs to be aware of all the files of a certain extension in the script's path and all sub-directories. Can...
1
by: halcyon943 | last post by:
have 4 folders that I watch and need to move files from to another location. Three constraints: -Finish time. Make sure the program stops transferring files at a specific time -Number of...
5
by: bir | last post by:
I have a folder which contains the xml files with the word CICM in the filename (can also contain some other files) I need to search for all these xml files and create a single zip file for all...
8
by: jcopeland38053 | last post by:
I have a third party program that runs several scheduled batch routines every night. Each type of batch routine creates a folder in the C:\Temp directory. Inside the folders are error reports that...
0
by: =?Utf-8?B?QnJ5YW4=?= | last post by:
Hello group. I've migrated from Win 2003 server to Win 2008 server. I've been banging my head agaist a wall for several days now trying to figure this out. I have the following script that will...
1
by: coolheadash | last post by:
Hi all, I have a folder named "folder" having multiple files inside different nested folders. Many files under it have a certain text that i am looking for. Before this line, I have to add a line...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.