473,473 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Searching a folder for certain files

13 New Member
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 2415
Dököll
2,364 Recognized Expert Top Contributor
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
spazzo6281
13 New Member
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 Recognized Expert Expert
See here. The getfiles() method supports searching for files matching a wildcard specification.
Feb 27 '08 #4
basti42
18 New Member
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
spazzo6281
13 New Member
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 Recognized Expert Expert
Just concatenate another "*" before find.
Mar 11 '08 #7
spazzo6281
13 New Member
Just concatenate another "*" before find.
Thanks, that worked.
Mar 11 '08 #8
Killer42
8,435 Recognized Expert Expert
Thanks, that worked.
Excellent! :)

Glad we could help.
Mar 11 '08 #9
spazzo6281
13 New Member
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 Recognized Expert Expert
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
spazzo6281
13 New Member
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
spazzo6281
13 New Member
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 Recognized Expert Expert
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
spazzo6281
13 New Member
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 Recognized Expert Expert
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.