Four: VC + + under the folder to find files

Source: Internet
Author: User

VC + + Find files under the folder

A: Search for files under the folder

BOOL findallfiles (CString szdir)
{
CFileFind FF;
CString FilePath;
CString FileName;

if (Szdir.right (1)!= "//")
Szdir + = "//";

Szdir + = "*.*";

BOOL res = ff. FindFile (Szdir);
while (RES)
{
res = ff. FindNextFile ();
FilePath = ff. GetFilePath ();
FileName = ff. GetFileName ();
if (!ff. Isdirectory () &&!ff. Isdots ())
{//Isdirectory indicates that this is a directory, isdots indicates that this is. Or ... need to filter out these two
if (filename.right (3) = = "MP3" | | filename.right (3) = = "LRC")
{
do ur things
}
}
}
Ff. Close ();
return true;
}

Method Two: Querying the file of a folder

int Ifindfiles (char *p_pcpath)
{
Win32_find_data Findfiledata;
HANDLE Hfind;
Hfind = FindFirstFile (P_pcpath, &findfiledata);
if (Invalid_handle_value = = hfind)
{
FindClose (hfind);
return 0;
}
FindNextFile (Hfind, &findfiledata);
while (1)
{
Hfind = FindNextFile (Hfind, &findfiledata);
if (strcmp (Findfiledata.cfilename, "...")!= 0)
{
FindClose (hfind);
return 1;
}
}
FindClose (hfind);
return 0;
}

Example: Looking for 4 files, one can not be less, one less, even if the search is unsuccessful

CFileFind FF;
CString FilePath;
CString FileName;
if (trystrroot. Right (1)!= "//")
Trystrroot + = "//";
Trystrroot + = "*.*";

BOOL res = ff. FindFile (Trystrroot);
while (RES)
{
res = ff. FindNextFile ();
FilePath = ff. GetFilePath ();
FileName = ff. GetFileName ();
if (!ff. Isdirectory () &&!ff. Isdots ())
{
if (filename.right () = = "Nprg60x5.hex")
{
J++;//1
}
if (Filename.right = = "Partition.mbn")
{
J++;//2
}
if (Filename.right = = "Oemsbl.mbn")
{
J++;//3
}
if (filename.right (8) = = "Amss.mbn")
{
J++;//4
}
}
}
Ff. Close ();
if (j!=4)
{
MessageBox (_t The path you chose is not correct, please select the right path!), _t ("Path selection Error"));
Return
}

Method Three:BOOL pathfileexists (lpctstr pszpath);

Determines if a file exists.

---detected, this function can detect whether a file or directory exists .

CString Strtargetfile;

CString Strworkpath = getworkpatht ();//Get EXE file path
Strtargetfile.format (_t ("%s//ins_list.ini"), Strworkpath)//Get the Ins_list.ini path under this folder

If it does not exist, first create
if (PathFileExists (strtargetfile))
{
DWORD dwattrs = GetFileAttributes (strtargetfile);
if (Dwattrs & file_attribute_readonly)
{
SetFileAttributes (Strtargetfile, Dwattrs &!) FILE_ATTRIBUTE_READONLY);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.