Solved

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

Posted on 1998-07-10
7
3,277 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 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Separate into files by filename 12 75
VBA "SendKeys" Syntax for Multiple Keystrokes 7 103
either24  challenge 19 90
zeroMAx challenge 20 87
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

910 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now