Does anybody know how I can delete a file using a wildcard. For example, if i want to delete a file with a *.txt extension in a folder.
Printable View
Does anybody know how I can delete a file using a wildcard. For example, if i want to delete a file with a *.txt extension in a folder.
SHFileOperation()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I do not do it for the rating. Oh, wait... Sure, I do!
Use FindFirstFile and FindNextFile with your wildcard template, then delete each file as it is found.
How do you use these functions?
Vladimir is right, SHFileOperation() is a better choice. I had forgotten about that function.
I'm unfamiliar with that function...how can I use it?
Isn't this a VB function? I need to do this in C.
No, it's not a VB function. It's declared in shellapi.h, implemented in shell32.dll.
SHFILEOPSTRUCT sh;
ZeroMemory(&sh, sizeof(SHFILEOPSTRUCT));
sh.hwnd = m_hWnd;
sh.wFunc = FO_DELETE;
sh.pFrom = "D:\\tmp\\*.pdf";
sh.fFlags = FOF_NORECURSION | FOF_SILENT;
int SHResult = SHFileOperation(&sh);
bob, i used the findfirstfile and found the file but now, I want to remove it and the remove function isn't working correctly...
WIN32_FIND_DATA FindFileData;
FindFirstFile("c:\\temp\\*.p7c", &FindFileData);
remove(FindFileData.cFileName);
Two things:
1. check the return value of FindFirstFile() before calling remove() to make sure you found a file.
2. the name in cFileName (of WIN32_FIND_DATA) does not include the full path, just the file name. You'll have to construct the full path string and pass that to remove() if you don't want to use SHFileOperation().
WIN32_FIND_DATA fd;
CString cBase("C:\\temp\\");
HANDLE handle = FindFirstFile( cBase + "*.p7c", &fd);
if ( handle != INVALID_HANDLE_VALUE )
{
CString cFile = cBase + fd.cFileName;
remove((LPCTSTR)cFile);
}
CString fileName;
CFilefind find;
BOOL bWork = find.FindFile(*.txt);
while (bWork)
{
bWork = find.FindNextFile(); //goto next file
fileName= find.GetFilePath();
CFile::Remove(fileName);
}//that's all
rate if it helped, answer if it didn't
>>>>>>>>>>>>>>>>>>> <<<<<<<<<<<<<<<<<<<
Visit: http://www.maxcode.com/ for VC++ samples...
>>>>>>>>>>>>>>>>>>> <<<<<<<<<<<<<<<<<<<
CString fileName;
CFilefind find;
BOOL bWork = find.FindFile(*.txt);
while (bWork)
{
bWork = find.FindNextFile(); //goto next file
fileName= find.GetFilePath();
CFile::Remove(fileName);}//that's all rate if it helped, answer if it didn't
//sorry for adding this again but athorwhise it could be mistaken
>>>>>>>>>>>>>>>>>>> <<<<<<<<<<<<<<<<<<<
Visit: http://www.maxcode.com/ for VC++ samples...
>>>>>>>>>>>>>>>>>>> <<<<<<<<<<<<<<<<<<<
CString fileName;
CFilefind find;
BOOL bWork = find.FindFile(*.txt);
while (bWork)
{
bWork = find.FindNextFile(); //goto next file
fileName= find.GetFilePath();
CFile::Remove(fileName);
}//that's all rate if it helped, answer if it didn't
//sorry for adding this again but athorwhise it could be mistaken
>>>>>>>>>>>>>>>>>>> <<<<<<<<<<<<<<<<<<<
Visit: http://www.maxcode.com/ for VC++ samples...
>>>>>>>>>>>>>>>>>>> <<<<<<<<<<<<<<<<<<<
SHFILEOPSTRUCT sh;ZeroMemory(&sh, sizeof(SHFILEOPSTRUCT));sh.hwnd = m_hWnd;sh.wFunc = FO_DELETE;sh.pFrom = "D:\\tmp\\*.pdf";sh.fFlags = FOF_NORECURSION | FOF_SILENT;int SHResult = SHFileOperation(&sh);
This code is from an earlier thread. The FOF_NORECURSION flag does not appear in my version of the shellapi.h include. It will not compile (VC++6.0 SP3).
If I remove this flag, it compiles but I get a file error 1026 merssage box (WIN98). I searched my MSDN cds but that is not listed as an error code. Code 26 alone looking at the includes appears to be a sharing violation, but these files are not open.
Ideas anyone?
Remove "spamnot" to use e-mail