0
0

More than 3 years have passed since last update.

Node.jsを利用してフォルダ作成を効率化してみる

Last updated at Posted at 2020-10-01

はじめに

「001」「002」「003」・・・「100」のような大量のフォルダを作成する必要があり、手作業でやるのは馬鹿馬鹿しいのでNode.jsで作ってみました。Windowsで動作します。

mkdir.js
const fs = require('fs')

// プログラム実行ディレクトリ配下を指定
const rootPath = process.cwd() + '\\';

for (let i = 1; i <= 100; i++) {
    const num = ('000' + i).slice(-3);
    const directoryPath = rootPath + num;
    fs.stat(directoryPath, (error, stats) => {
        if (error) {
            if (error.code === 'ENOENT') {
                fs.mkdir(directoryPath, (error) => {
                    if (error) {
                        throw error
                    } else {
                        console.log(`${directoryPath}ディレクトリを作成できました。`)
                    }
                });
            } else {
                console.log(error);
            }
        } else {
            console.log(`${directoryPath}ディレクトリは既に存在します。`);
        }
    })
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0