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
WinExec("COMMAND.COM /C del *.txt", SW_HIDE);
Download/install the latest Platform SDK. Then, using Tools/Options/Directories in the Visual Studio menu, add the SDK include and lib paths to the top of their respective lists. Rebuild All.
if you happen to have a broad band connection that allows you to down load a 300+ meg file and also happen to have the extra gig or so free hd space to compile the sdk...
Breakdown and use the winexec call, one line no fuss no muss.
You're overstating the case.
I have the SDK installed, including many of the samples, and it takes 500 Meg, not 1 Gig. Nothing needs to be compiled in the SDK in order to use it. Just put the include and lib directories under your Options settings.
And some things are only defined in the SDK, such as FOF_NORECURSION.
So, yes, WinExec could be used in this case, but having the Platform SDK installed could end up being required if you need to use certain functions or COM interfaces.
Briefly, I used SetCurrentDirectory to go to the folders and kept pFrom with the 8.3 format. I then added the still current flags for auto confirming and pop-up silence and all worked.
I thought I fully conformed to using the no long filename requirements, but apparently not. I don't think I misswd any \" escapes to pass, but anyway it now all performs as expected with the shell operation functions.
The 1026 message pops up with any discrepency such as not having a file to delete.
Thanks for assistance.
Remove "spamnot" to use e-mail
try
call it as follows:Code:void DeleteFiles(const TCHAR* tszFullPathFileNamePattern)
{
HANDLE hFile = NULL; // Handle to file
WIN32_FIND_DATA FileInformation; // File information
BOOL br = FALSE;
CString strFileName;
CString strFolderName;
CString strTemp = tszFullPathFileNamePattern;
int index = strTemp.ReverseFind('\\');
if(index>=0)
{
strFolderName = strTemp.Mid(0, index+1);
}
hFile = ::FindFirstFile(tszFullPathFileNamePattern, &FileInformation);
if(hFile != INVALID_HANDLE_VALUE)
{
do
{
if(!(FILE_ATTRIBUTE_DIRECTORY & FileInformation.dwFileAttributes))
{
strFileName = strFolderName + FileInformation.cFileName;
br = DeleteFile(strFileName);
}
} while(::FindNextFile(hFile, &FileInformation));
::FindClose(hFile);// Close handle
}
}
Code:// Delete files Crystal Reports leaves in the Temp Directory
TCHAR* tszTempDir = _tgetenv("TEMP");
CString strTempDir = tszTempDir;
DeleteFiles(strTempDir+_T("\\CPE*.tmp"));
DeleteFiles(strTempDir+_T("\\~DFB*.tmp"));
DeleteFiles(strTempDir+_T("\\ACR*.tmp"));