Q48090: Using a Wildcard Argument with the remove() Function
Article: Q48090
Product(s): See article
Version(s): 5.10 | 5.10
Operating System(s): MS-DOS | OS/2
Keyword(s): ENDUSER | S_QuickC S_QuickASM | mspl13_c
Last Modified: 16-JAN-1990
Question:
I'm using the remove() function to delete my files, but when I pass
the wildcard as an argument, the function does not delete any files.
Is there any why I can use the remove() function to delete all the
files in the directory when I specify it to?
Response:
Yes; use the _dos_findfirst and _dos_findnext functions to search for
each file and then use the remove() function to delete each file. The
following example demonstrates how to write a code equivalent to
remove("*.*"):
Code Example
------------
#include <stdio.h>
#include <dos.h>
#include <io.h>
main ()
{
struct find_t c_file;
char fn[12];
printf ("Enter file to delete: ");
scanf ("%s", fn);
/* This code section will delete all the files in the directory. */
if (strcmp(fn, "*.*") == 0) {
_dos_findfirst ("*.*", _A_NORMAL, &c_file);
do {
remove (c_file.name);
} while (_dos_findnext (&c_file) == 0);
}
else
/* This section will delete only one file. */
if (remove (fn) == -1)
printf ("File not found\n");
else
printf ("File successfully deleted\n");
}
THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.
Copyright Microsoft Corporation 1986-2002.