[C Language] delete all files with user-defined extensions

Source: Internet
Author: User

Environment: win7

IDE: DEV-C ++

Compiler: GCC

Compilation result: Success

Running result: Success

 

Instructions for use:

1. Enter the directory to be queried, such as e:

2. Enter the suffix to be deleted, for example, txt.

 

Note: This program uses Remove to delete files, so the deleted files are not moved to the recycle bin.

 

 

Program: http://files.cnblogs.com/IAmBetter/DeleteEverything.rar

Source code:

# Include <stdio. h> # include <direct. h> // _ getcwd (), _ chdir () # include <stdlib. h> // _ MAX_PATH, system () # include <io. h> // _ finddata_t, _ findfirst (), _ findnext (), _ findclose () # include <string. h> # include <windows. h> // Delete total int count = 0; // obtain the current path void GetCurrentPath (void) {char buf [80]; getcwd (buf, sizeof (buf )); printf ("current working directory: % s \ n", buf);} // get the suffix char * substr (const char * str) {char * p Tr, c = '. '; static char stbuf [256]; ptr = strrchr (str, c); // if (ptr = NULL) {return stbuf ;} int pos = ptr-str; // Use Pointer subtraction to obtain the index unsigned start = pos + 1; unsigned end = strlen (str); unsigned n = end-start; strncpy (stbuf, str + start, n); stbuf [n] = 0; // Add 0 return stbuf;} // recursively query the file and delete void findAllFile (char * pFilePath, char * extName) {WIN32_FIND_DATA FindFileData; DWORD dwError; HANDL E hFind = INVALID_HANDLE_VALUE; char DirSpec [MAX_PATH + 1]; strncpy (DirSpec, pFilePath, strlen (pFilePath) + 1); SetCurrentDirectory (pFilePath); strncat (DirSpec, "\ *", 3); hFind = FindFirstFile (DirSpec, & FindFileData); if (hFind = INVALID_HANDLE_VALUE) {printf ("FileName: % s Invalid file handle. error is % u \ n ", pFilePath, GetLastError (); return;} else {if (FindFileData. dwFileAttributes! = FILE_ATTRIBUTE_DIRECTORY) {printf ("FileName: % s \ n", FindFileData. cFileName);} else if (FindFileData. dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY & strcmp (FindFileData. cFileName ,". ")! = 0 & strcmp (FindFileData. cFileName ,"..")! = 0) {char Dir [MAX_PATH + 1]; strcpy (Dir, pFilePath); strncat (Dir, "\", 2); strcat (Dir, FindFileData. cFileName); findAllFile (Dir, extName);} while (FindNextFile (hFind, & FindFileData )! = 0) {if (FindFileData. dwFileAttributes! = FILE_ATTRIBUTE_DIRECTORY) {_ chdir (pFilePath); char * extname2 = substr (FindFileData. cFileName); if (strcmp (extname2, extName) = 0) {printf ("\ nFileName: % s", FindFileData. cFileName); int result = remove (FindFileData. cFileName); if (result = 0) {printf ("Delete Result: % d", result); count ++ ;} else {perror ("remove") ;}} else if (FindFileData. dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY & strcmp (Fin DFileData. cFileName ,".")! = 0 & strcmp (FindFileData. cFileName ,"..")! = 0) {char Dir [MAX_PATH + 1]; strcpy (Dir, pFilePath); strncat (Dir, "\", 2); strcat (Dir, FindFileData. cFileName); findAllFile (Dir, extName) ;}} dwError = GetLastError (); FindClose (hFind); if (dwError! = ERROR_NO_MORE_FILES) {printf ("FindNextFile error. error is % u \ n ", dwError); return ;}}// start displaying part of void Show (char str []) {int I, len; len = strlen (str); for (I = 0; I <len; I ++) {printf ("% c", str [I]); sleep (100) ;}} int main (void) {printf ("Anleb:"); sleep (1000); char string1 [] = "I am Anleb, nice to somthing! \ N "; Show (string1); printf (" Anleb: "); sleep (1000); char string2 [] =" Go, gay! \ N "; Show (string2); printf (" Please Enter the Path: "); char path [128]; gets (path); while (strlen (path) = 0) {printf ("Warning: The Path value is Null! \ N "); printf (" Please Enter the Path: "); gets (path);} if (strcmp (path," exit ") = 0) return 0; printf ("Please Enter the ExtName:"); char extName [10]; gets (extName); while (strlen (extName) = 0) {printf ("Warning: the ExtName value is Null! \ N "); printf (" Please Enter the ExtName: "); gets (extName);} if (strcmp (extName," exit ") = 0) return 0; findAllFile (path, extName); printf ("\ nDelete Count: % d \ n", count); system ("pause"); return 0 ;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.