Recursive path deletion function in Linux

Source: Internet
Author: User

Notes:

  1. The main function is to traverse the path. If you know how to traverse the path, you can delete it.
  2. At that time, the macro definition of the file type was not found, so I had to traverse the output type first. Note that in Linux, the path is considered as a special file. 8 is a common file, 4 is a path, and 4 is the type of "." and "...". Make a difference.


Void remove_dir (char * sz_dir)
{
Char subname [256];
Dir * pdir = NULL;
Struct dirent * pdirent = NULL;

Pdir = opendir (sz_dir );
If (pdir ){
// Printf ("into dir % s/n", sz_dir );
While (pdirent = readdir (pdir ))! = NULL ){
// Printf ("type is % d/N", pdirent-> d_type );
// Printf ("name is % s/n", pdirent-> d_name );
If (pdirent-> d_type = 8 ){
Sprintf (subname, "% S/% s", sz_dir, pdirent-> d_name );
Remove (subname );
}

If (pdirent-> d_type = 4
& Strcmp (pdirent-> d_name ,".")! = 0
& Strcmp (pdirent-> d_name ,"..")! = 0 ){
Sprintf (subname, "% S/% s", sz_dir, pdirent-> d_name );
Remove_dir (subname );
}
}
Closedir (pdir );
Remove (sz_dir );
}
Else {
Printf ("error in reading % s: % s/n", sz_dir, strerror (errno ));
}
}

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.