ディレクトリを再帰的に作成・削除

Contents [hide]

出力結果

Succeeded in creation of ./example_directoy/perl
Succeeded in removal of ./example_directoy/perl

コード例

JavaScript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const fs = require("fs");
const fsExtra = require('fs-extra');
const root_dir = './example_directoy';
const lang_dir = 'js';
const dirpath = [root_dir, lang_dir].join("/");
 
fs.mkdir(dirpath, { recursive: true }, (err) => {
  if (err) {
    throw err;
  }
  else{
    console.log("Succeeded in creation of " + root_dir);
    if(root_dir.match(/[a-zA-Z\d_\-]/)) {
      fsExtra.remove(root_dir, (err) => {
        if (err) {
          throw err;
        }
        else{
          console.log("Succeeded in removal of " + root_dir);
        }
      });
    }
  }
});

PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
$root_dir = './example_directoy';
$lang_dir = 'php';
$dirpath = implode('/', [$root_dir, $lang_dir]);
if( !is_dir($dirpath) ) {
  mkdir($dirpath, 0777, true);
}
if( is_dir($dirpath) ) {
  print "Succeeded in creation of ".$dirpath."\n";
  if(preg_match('/[a-zA-Z\d_\-]/', $root_dir)) {
    delTree($root_dir);
  }
  if( !is_dir($root_dir) ) {
    print "Succeeded in removal of ".$root_dir."\n";
  }
}
 
function delTree($dir) {
  $files = array_diff(scandir($dir), array('.','..'));
   foreach ($files as $file) {
     (is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file");
   }
   return rmdir($dir);
 }

Perl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use File::Path qw(make_path remove_tree);
 
my $root_dir = './example_directoy';
my $lang_dir = 'perl';
my $dirpath = join('/', $root_dir, $lang_dir);
unless(-d $dirpath){
  make_path($dirpath);
}
if(-d $dirpath) {
  print "Succeeded in creation of ".$dirpath."\n";
  if($root_dir=~ m![a-zA-Z\d_\-]!){
    remove_tree($root_dir);
  }
  if(-d $root_dir) {
    die("Failed to remove ".$root_dir)
  }
  else{
    print "Succeeded in removal of ".$dirpath."\n";
  }
}
else{
  print "Failed to mkdir -p ".$dirpath."\n";
}

Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import os
import re
import shutil
 
root_dir = './example_directoy'
lang_dir = 'python'
dirpath = '/'.join([root_dir, lang_dir])
if not os.path.isdir(dirpath):
  os.makedirs(dirpath)
 
if os.path.isdir(dirpath):
  print("Succeeded in creation of " + dirpath)
  if re.search(r"[a-zA-Z\d_\-]", root_dir):
    shutil.rmtree(root_dir)
    if not os.path.isdir(root_dir):
      print("Succeeded in removal of " + root_dir)

Ruby

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
require 'fileutils'
root_dir = './example_directoy'
lang_dir = 'ruby'
dirpath = [root_dir, lang_dir].join("/")
 
FileUtils.mkdir_p(dirpath)
 
if Dir.exist?(dirpath)
  print("Succeeded in creation of " + dirpath + "\n")
  if root_dir.match("[a-zA-Z\d_\-]")
    FileUtils.remove_dir(root_dir)
    if !Dir.exist?(root_dir)
      print("Succeeded in removal of " + root_dir + "\n")
    end
  end
end

Shell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
ROOTDIR='./example_directoy';
LANGDIR='python';
DIRPATH=$ROOTDIR'/'$LANGDIR;
mkdir -p $DIRPATH;
 
if [ -d $DIRPATH ]
then
  echo "Succeeded in creation of ".$DIRPATH;
fi
 
if [[ $DIRPATH =~ [a-zA-Z\d_\-] ]]
then
  rm -r $ROOTDIR;
fi
 
if [ ! -d $ROOTDIR ]
then
  echo "Succeeded in removal of ".$ROOTDIR;
else
  echo "Failed in removal of ".$ROOTDIR;
fi

プログラミング言語比較サイトProgrammingLang.comでは、同じ問題を複数のプログラミング言語がそれぞれどのような記述で解決できるのかの例を提供。
複数の言語を比較し、貴方の問題を解決するのに最適な言語の選択と、その言語での解法を得る事を手助けします。
全問題カバー: JavaScript Perl PHP Python Ruby | 一部: C C# C++ Go Java Rust Shell
 
問題解法大分類(50音順)
Class | 時間 | 数値 | System | Database | Test | Network | 配列 | ファイルシステム | 変数 | 文字列
その他役立ちコンテンツ

※当サイトではアフィリエイトプログラムを利用して商品を紹介しています。