A small code

Source: Internet
Author: User

<?PHPEcho"<pre>";//define the directory where you want to delete the video$path=Str_replace(' \ \ ', '/', ' F:\program\utorrent2.02\downloads '));//Change the work pathchdir($path);/**function name:del; * @param path, delete root directory; * @param depth, (recursive depth)*/functionDel$path,$depth= 0){    //Open Directory, get file handle    $dir _handle=Opendir($path); //Start Traversal     while(false!==$file=Readdir($dir _handle)) {        //ignore '. ' With the '.. '        if($file= = '. ' | |$file= = ' ... ') {            Continue; }        //determines whether the current file is a directory, is recursive        if(Is_dir($path.‘ /‘.$file) ) {del ($path.‘ /‘.$file,$depth+ 1); }Else{            //is not a file to determine whether it is a video            if(strstr($file, '. Itcast ') | |strstr($file, '. avi ') | |strstr($file, '. mp4 ')) {            //Convert absolute path character encoding (without file name)@$path=Iconv(' Utf-8 ', ' GBK ',$path); //piecing together the absolute path of a file's integrity                $video=$path.‘ /‘.$file; //Delete@unlink($video); }        }        //determine if the video folder is        if($file= = ' Video ' | |$file= = ' Vedio '){            //Yes then delete@rmdir($path.‘ /‘.$file); }        //Output File name        Echo str_repeat(‘-‘,$depth* 5),$file, "<br/>"; }}//calling FunctionsDel$path);

The first two days to see the F disk is about to explode, think of it to clean up, the video of the wisdom of all deleted, suddenly remembered that I learned the recursive traversal directory, as well as delete files and folders, then I can also try to delete the recursive video Ah ~

Well, think about the action immediately, I first put the recursive traversal of the function of the directory, and then because of fear of some unexpected errors, write a small script to try to delete a separate video, but because the video name is Chinese, can not be deleted, I go to the Internet to check information, The discovery needs to use an encoded conversion function iconv (), and then it succeeds. After I deleted the recursive function, I found that the system exploded two errors, saying that my iconv () and unlink () were not used correctly. I've been debugging a lot of times. To this evening, I asked the counselor, the original reason for the error is that I first put the path of the file to be pieced together before the conversion code, but because the directory (UTF-8) and the Chinese file name (GBK) uses the same encoding, so the ICONV () conversion is unsuccessful. The final solution is to first convert the directory to GBK, and then together with the Chinese file name, you can.

Then I think of a word: great programmers are lazy.

A small code

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.