Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 1998-07-10
7
Medium Priority
?
3,641 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
  • 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 150 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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 …
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.
Loops Section Overview

963 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