KnowledgeBase
PERL
Create directory structure using PERL
Create directory structure using PERL
This Perl script generates one or more arrays of empty directories under a common root.
#-----------------------------------------------------------------------------
# Create Folder Structure
# -----------------------
# Author: Mark Dowd, DowdTec (07785-925914)
# Purpose: To create multiple skeleton folder structures having a common root
# Usage: perl.exe create_folders.pl <rootpath> [<subroot> <subroot>...]
# Rootpath can be relative or absolute. If it doesn't exist, it is
# created. If no subroots are specified then structure is created
# directly under the rootpath, otherwise it goes under each subroot.
# Subroots are named <rootpath>_<subroot>.
#-----------------------------------------------------------------------------
use strict;
use Cwd;
use File::Path;
# Default directory set
my @dirs = ("ALS", "Analysis", "AP", "BISI", "Broker", "CI", "Gateway", "MTS");
my $rootpath;
for (my $i = 0; $i < @ARGV; $i++) { # Process all parameters
if (not $i) { # First one is the root path
$rootpath = $ARGV[$i];
$rootpath =~ s/\\/\//g; # Fix 'wrong' slash
mkpath("$rootpath", {verbose => 1});
chdir "$rootpath";
# Extract the 'final' directory name
my @path = split /\//, $rootpath;
$rootpath = $path[@path - 1];
# If there are no subroots...
if (@ARGV == 1) {
# ...then fill it with the directories in the array
for (@dirs) {
mkpath("$_", {verbose => 1});
}
}
}
else { # Subsequent ones are subroots
my $subrootpath = "$rootpath\_$ARGV[$i]";
# Create the subroot
mkpath("$subrootpath", {verbose => 1});
# ...then fill it with the directories in the array
for (@dirs) {
mkpath("$subrootpath/$_", {verbose => 1});
}
}
}