Home  >  Article  >  Web Front-end  >  How can I quickly delete files?

How can I quickly delete files?

零下一度
零下一度Original
2017-06-26 10:06:321982browse

Summary:

Are you still worried about slow file deletion? The powerful tool dlf is here to help you. As a front-end developer, the most common one is node_modules. If there are many dependencies, it is okay to delete them on osx system, but it will be in trouble for Windows users. This article shares a command line file and folder deletion tool.

Installation and usage:

This tool uses node.js, so make sure your computer has node.js installed first.

npm install -g dlf

Delete file

dlf file

Delete folder

dlf directory

 

Address:

Welcome to fork or star

Principle:

Mainly uses the node.js file operation method

fs.existsSync(path) Returns true if the file exists, otherwise returns false
fs.statSync(dir) Returns the related attributes of dir
fs.readdirSync(dir) Returns a file that does not include '.' and '..' array of file names
fs.unlinkSync(file) Delete file
fs.rmdirSync(dir) Delete folder

Main code:

if( fs.existsSync(dir) ) {
    if(fs.statSync(dir).isDirectory()) {
        files = fs.readdirSync(dir);
        files.forEach(function(file,index){
            var curPath = path.join(dir,file);
            if(fs.statSync(curPath).isDirectory()) {
                run(curPath);   
            } else { 
                fs.unlinkSync(curPath);
            }
        });
        fs.rmdirSync(dir);
    } else {
        fs.unlinkSync(dir);
    }
}

 

Features to be improved:

1. Prompt to confirm before deleting
2.Support deletion filtering, you can Filter out the ones you don’t want to delete
3. Support deleting files that only have administrator permissions

The above is the detailed content of How can I quickly delete files?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn