递归创建和删除目录

Contents

输出结果

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


程式码


JavaScript

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

<?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

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

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

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

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