Solved

Deleting files from a directory using wildcards in a C++ App.

Posted on 1998-07-10
7
3,463 Views
Last Modified: 2013-11-19
I am writing a function in Visual C++ 5.0  that should delete files from a directory  that begins with a certain prefix.  A related DOS command would be "C:\> del myfiles.*".   I have used the function DeleteFile() to delete files where I know the exact name, such as "DeleteFile("c:\\archive\\myfiles.001")".  But if I substitute the wildcard character "*"
for ".001" it does not work.  Can this function accomplish this task?  If so, please provide an example.  If it can't, what would you recommend?

Thank you.

I think this should be an easy question to answer but if you need more points, feel free
to take what you feel it is worth.
0
Comment
Question by:asavino
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 

Author Comment

by:asavino
ID: 1319042
Edited text of question
0
 

Author Comment

by:asavino
ID: 1319043
Edited text of question
0
 
LVL 4

Accepted Solution

by:
piano_boxer earned 50 total points
ID: 1319044
DeleteFile() does not support wildcards. You need to Use FindFirstFile()/FindNextFile() instead.

I have written the following sample routine for you. Plz not that you can include the path in the filename (ex: WindDeleteFiles("c:\temp\*.*") which will delete all file in the temp dir.

void WildDeleteFiles(LPCTSTR lpszFile)
{
    WIN32_FIND_DATA fd;
    HANDLE hFind;

    hFind = FindFirstFile(s, &fd);
    if(hFind == INVALID_HANDLE_VALUE)
        // No files found
        return;

    do
    {
        DeleteFile(fd.cFileName);
    {
    while(FindNextFile(hFind, &fd));
}  

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:piano_boxer
ID: 1319045
Sorry, s argument to FindFirstFile should be replaced by lpszFile.
0
 
LVL 3

Expert Comment

by:tma050898
ID: 1319046
PMFJI, the open curly before the while should be a closing brace
0
 
LVL 4

Expert Comment

by:piano_boxer
ID: 1319047
HERE IS THE CORRECTED ANSWER
****************************

DeleteFile() does not support wildcards.
You need to Use FindFirstFile()/FindNextFile() instead.

I have written the following sample routine for you.
Plz note that you can include the path in the filename
(ex: WildDeleteFiles("c:\temp\*.*") which will delete all
file in the temp dir.

void WildDeleteFiles(LPCTSTR lpszFile)
{
    WIN32_FIND_DATA fd;
    HANDLE hFind;

    hFind = FindFirstFile(lpszFile, &fd);
    if(hFind == INVALID_HANDLE_VALUE)
        // No files found
        return;

    do
    {
        DeleteFile(fd.cFileName);
    }
    while(FindNextFile(hFind, &fd));
    FindClose(hFind)  // <<<<<<<<<<<<<<<<
}
0
 

Author Comment

by:asavino
ID: 1319048
Thank you for your help, problem is solved. One note though, I found that the file name returned does not include the directory path name where the file was found.  But, it was no big deal.  All I did was re-append the path name to the file name returned and then issued the 'DeleteFile()'.  Thank you again.

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

631 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