2

How to use DeleteFile with wildcard?

so c:\myFolder\a*.txt will delete a123.txt and a5555.txt but not b123.txt

2 Answers 2

10
WIN32_FIND_DATAW fd; HANDLE hFind = FindFirstFileW(L"C:\\myFolder\\a*.txt", &fd); if (hFind != INVALID_HANDLE_VALUE) { do { DeleteFileW((wstring(L"C:\\myFolder\\") + fd.cFileName).c_str()); } while (FindNextFileW(hFind, &fd)); FindClose(hFind); } 
Sign up to request clarification or add additional context in comments.

Comments

4

DeleteFile() does not support wildcards.

@user1438233 showed you how to use DeleteFile() in a FindFirstFile() loop to search for files using wildcards.

Another option is to use SHFileOperation() and let the Shell handle the wildcards for you:

SHFILEOPSTRUCTW op = {0}; op.wFunc = FO_DELETE; op.pFrom = L"C:\\myFolder\\a*.txt\0"; op.fFlags = FOF_FILESONLY | FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_NORECURSION; SHFileOperationW(&op); 

2 Comments

yes, another option is to use: system("del C:\\myFolder\\a*.txt")
The documentation doesn't say, whether COM needs to be initialized on the calling thread. Do you happen to know, whether this is the case?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.