Search code examples
perlsubdirectorycreation

Create directory tree with forward slash in directory name


Trying to create a directory tree in which one of the directories has a forward slash (/) in the name. See the $artist variable for the artist name and my attempts at creating the directory.

#!/usr/bin/perl

use warnings;
use strict;
use File::Path qw(make_path);
my $srcpath = '/home/<username>;/music';
my $artist = "";
my $album = 'somealbum';

# Using single quotes

#t1
$artist = 'AC/DC';
make_path("${srcpath}/t1/${artist}/${album}/");

#t2
$artist = 'AC//DC';
make_path("${srcpath}/t2/${artist}/${album}/");

#t3
$artist = 'AC\/DC';
make_path("${srcpath}/t3/${artist}/${album}/");

# Using double quotes

#t4
$artist = "AC/DC";
make_path("${srcpath}/t4/${artist}/${album}/");

#t5
$artist = "AC//DC";
make_path("${srcpath}/t5/${artist}/${album}/");

#t6
$artist = "AC\/DC";
make_path("${srcpath}/t6/${artist}/${album}/");

#t7
$artist = "AC\\/DC";
make_path("${srcpath}/t7/${artist}/${album}/");

Directory tree I want (5 folders):
/ -> home -> <username> -> music -> AC/DC -> somealbum

Directory tree I get (6 folders):
/ -> home -> <username> -> music -> AC -> DC -> somealbum


Solution

  • The slash character is an illegal character for a file or directory name. You will need to substitute some other character, like the dash character.