944,131 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6261
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
May 19th, 2005
0

Help with Searching files in Directories

Expand Post »
I am trying to search for files in Directories.
like, i have 10 directories..... and consider directory 1, which has 2 files..
this is same with all the files...
i want to get a string from each of these files from 10 directories... to be written to a seperate file, in C++.
I have been trying to do this.... any help will be greatly appreciated.
Thank you
Reputation Points: 10
Solved Threads: 0
Newbie Poster
usausa is offline Offline
10 posts
since May 2005
May 19th, 2005
0

Re: Help with Searching files in Directories

Can you get a string from one file? If so then can you get a string from more than one file in the same directory? Do you have to search for the files according to some pattern, or do you have a list of filenames? I see this question a lot, and nobody gives enough detail. I need to know what you already know how to do. If you don't know how to open and read a file then my answering the question assuming your problem is with reading from multiple directories wouldn't help much. If you don't know how to open and read from different directories then I need to know what operating system and compiler you use because there's no portable solution for that problem.
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005
May 19th, 2005
0

Re: Help with Searching files in Directories

Yes, i can get the string i want from one file, and from multiple files in one directory.
i have to search for the files, based on one String, and then get the data from those files.

i worked with files before, but no with searching for files in directories and arranging them in an array.
coz, there is parent directory, which has subfolders.these subfolders has files.
the OS is Winxp, VC++ compiler.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
usausa is offline Offline
10 posts
since May 2005
May 19th, 2005
0

Re: Help with Searching files in Directories

In that case the Win32 API would probably be your easiest solution. You can start browsing from here.
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005
May 20th, 2005
0

Re: Help with Searching files in Directories

how about this function... any further help plzz

C++ Syntax (Toggle Plain Text)
  1. private void GetFileInDirectory()
  2. {
  3. textfile = new string[200];
  4. try
  5. {
  6. string [] files;
  7. string path = currentPictureFile.Substring(0, currentPictureFile.LastIndexOf("\\") + 1);
  8.  
  9. if ( Directory.Exists(path) )
  10. {
  11. files = Directory.GetFiles(path);
  12. int i = 0;
  13. foreach ( string fileName in files )
  14. {
  15. int lastIndex = fileName.LastIndexOf(".");
  16. if ( fileName.Length - lastIndex - 1 == 3 )
  17. {
  18. string ext = fileName.Substring(lastIndex + 1, 3).ToUpper();
  19. if ( (ext.Equals("txt" ) )
  20. {
  21. textfile[i++] = fileName;
  22. }
  23. }
  24.  
  25. }
  26.  
  27. }
  28. }
  29.  
  30. catch (IOException ioE)
  31. {
  32. MessageBox.Show("Error - wrong path");
  33. }
  34.  
  35. catch (UnauthorizedAccessException unE)
  36. {
  37.  
  38. }
  39. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
usausa is offline Offline
10 posts
since May 2005
May 20th, 2005
0

Re: Help with Searching files in Directories

Uh, that's not C++, it's Java.
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005
May 20th, 2005
0

Re: Help with Searching files in Directories

yeah... right...

i was trying to do it in C++... just found this piece of code from my frnd...

I have never worked with file search and directory structure

am very Stuck at this point... it wont let me further...

any help will be appreciated....

Thnx alotttttttttt in advance
Reputation Points: 10
Solved Threads: 0
Newbie Poster
usausa is offline Offline
10 posts
since May 2005
May 20th, 2005
0

Re: Help with Searching files in Directories

In another forum I posted a link to an FAQ: "Accessing a directory and all the files within it". Did you follow the link?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
May 20th, 2005
0

Re: Help with Searching files in Directories

yes sir, i did that,...... but that doesnt work with VC++

i tried those... plz help
Reputation Points: 10
Solved Threads: 0
Newbie Poster
usausa is offline Offline
10 posts
since May 2005
May 20th, 2005
0

Re: Help with Searching files in Directories

Another option if MSDN scares you is to download the Boost::filesystem library. If you can figure out how to build it and link with it then writing your program would be a breeze. But I still recommend using the Win32 API, and my link is a great start.
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Skeleton for a game.
Next Thread in C++ Forum Timeline: Can't set memory!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC