Solved

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

Posted on 1998-07-10
7
3,411 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get filename and folder into excel 7 89
C++ BOOL WINAPI ReadFile fails on windows 10 when reading from USB cable 9 551
zeroFront challenge 7 136
Not needed 13 134
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

752 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