Archived

This topic is now archived and is closed to further replies.

How to search folders for files?

This topic is 5011 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hail fellow programmers, I wrote a programming tool that opens a spefic file and changes all instances of one word with another word. It''s really useful when I wish to change an obscure variable name or change the style of my programming. Its works great but I must manually enter in the file name each time I want the program to edit a file. How can I arrange it so I can get my program to go search through a directory and its subdirectors and use its word exchange functions on, lets say all *.h, and and all *cpp. files in a named directory. I''m using msdev 6.0 and programming in c++. I''ve searched the forums for related topics, but to avail. I''m looking for an answer that uses some l337 c++ stream/file hack , or just the right win32 api call. Please no mfc! Thanks in advance

Share this post


Link to post
Share on other sites
You can do this with FindFirstFile and FindNextFile. Here''s an example of how to list all files matching a given string (such as "c:\\something\\*.txt").

std::vector<std::string> findFiles(const std::string& match)
{
WIN32_FIND_DATA fileData;
HANDLE hFind;
std::vector<std::string> result;

hFind = FindFirstFile(match.c_str(),&fileData);
if(hFind != INVALID_HANDLE_VALUE)
{
do
{
if((fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
result.push_back(fileData.cFileName);
}
while(FindNextFile(hFind,&fileData));
FindClose(hFind);
}
return result;
}
This doesn''t go into subdirectories, but it''s possible to modify it to do so. Good luck!

Share this post


Link to post
Share on other sites
actually if your using msdev, you can just hit ctrl-shif-h to file and replace in all your project files. or sctrl-shift-f to search throught them.

the key mappings might be diffrent from vc.net but its all there. check it!

Share this post


Link to post
Share on other sites
quote:
Original post by xorjesus
Thanks Beer Hunter By the way, are you using the STL in your example? I''m not familier with std::vector(..).
Yes, it''s from the STL. std::vector represents a dynamic array (in this case, of strings). The push_back function increases the size by 1 and puts the given item (the filename) at the end. To read items from it, you''d do something like:

std::vector<std::string> files = findFiles("data\\*.txt");
for (int i = 0; i != files.size(); ++i)
{
std::cout << files[i] << std::endl;
}

// OR


for (std::vector<std::string>::const_iterator i = files.begin(); i != files.end(); ++i)
{
std::cout << *i << std::endl;
}
This article explains more about std::vector.

Share this post


Link to post
Share on other sites