?
Solved

Searching file function

Posted on 1999-12-08
6
Medium Priority
?
182 Views
Last Modified: 2013-12-03
I need a function in C that takes a directory as a parameter , and processes in some way ( for example deletes , copies or other - doesn't matter ) , each file or directory it finds under the tree given , no matter the deep of the directory given .

Thanks
0
Comment
Question by:WxW
  • 3
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 150 total points
ID: 2266018
void CheckFiles ( char* pszPath,    char*   pszBase)
{
WIN32_FIND_DATA w32fd;
HANDLE hFind;
DWORD dwAtt;
char acPath [ MAX_PATH];
char acBase [ MAX_PATH];

if ( '.' == * (pszPath + lstrlen ( pszPath) - 1))
                return;

if  (   pszBase)
    sprintf (   acPath, "%s\\%s",   pszBase,    pszPath);
else
    lstrcpy ( acPath, pszPath);

lstrcpy ( acBase, acPath);

dwAtt = GetFileAttributes ( acPath);

if ( 0xffffffff == dwAtt)
{
 // error ...
}

if ( FILE_ATTRIBUTE_DIRECTORY & dwAtt)
{
    if  (   '\\'    ==  acPath  [   lstrlen (   acPath) -   1])
            lstrcat (   acPath, "*.*");
     else
            lstrcat (   acPath, "\\*.*");
}
 


hFind = FindFirstFile ( acPath, &w32fd);

if ( INVALID_HANDLE_VALUE == hFind)
{
 // error

 return;
}

// recurse if directory...
if ( FILE_ATTRIBUTE_DIRECTORY == w32fd.dwFileAttributes)
{
     CheckFiles ( w32fd.cFileName,  acBase);
}
else
 HandleFile ( &w32fd);

while ( FindNextFile ( hFind, &w32fd))
{
    // recurse if directory...
    if ( FILE_ATTRIBUTE_DIRECTORY == w32fd.dwFileAttributes)
    {
     CheckFiles ( w32fd.cFileName,  acBase);
    }
    else
      HandleFile ( &w32fd);
}

if ( ERROR_NO_MORE_FILES != GetLastError())
{
 // error
}
FindClose ( hFind);
}

void HandleFile ( WIN32_FIND_DATA* pw32fd)
{
 // do what you want to do ;-)
}

Use it like

#include <windows.h>
#include <stdio.h>

void HandleFile ( WIN32_FIND_DATA* pw32fd);
void CheckFiles ( char* pszPath,      char*      pszBase);
void main ( int argc, char** argv)
{
      if      (      argc      !=      2)      return;

      CheckFiles ( *( argv + 1),      NULL);
}

Let me know if you encounter problems!
0
 
LVL 23

Expert Comment

by:chensu
ID: 2266131
Simply use the SearchTreeForFile function.
0
 
LVL 23

Expert Comment

by:chensu
ID: 2266135
If you want to copy or delete a directory, use the SHFileOperation function.
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
LVL 86

Expert Comment

by:jkr
ID: 2266150
Hmm, both APIs have the advantage to be 'built-in' (thought of them, too), but don't offer the flexibility that is achieved by what you can do in 'HandleFile()' - just think of renaming each file in the subtree...
0
 
LVL 23

Expert Comment

by:chensu
ID: 2266307
Right, it depends on what you are doing.
0
 
LVL 6

Author Comment

by:WxW
ID: 2266348
jkr : thanks .

chensu : I need to process every file in the list , not just find one file in a tree . Thanks anyway -)

0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Suggested Courses
Course of the Month3 days, 21 hours left to enroll

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question