Create directory tree with forward slash in direct

2019-05-31 08:03发布

问题:

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

回答1:

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.



回答2:

It's impossible, not because of Perl but because the OS filesystem layer forbids it. There is no escape character that would allow you to embed a / (or a NUL, for that matter) in a path component.

The usual way this is handled is by an application-specific encoding, such as treating =n as NUL, =s as /, and =e as =. (I don't know of any applications which actually use that particular encoding; it's just an example.)