Create directory tree with forward slash in directory name [closed]

自作多情 提交于 2020-01-14 03:42:06

问题


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.)



来源:https://stackoverflow.com/questions/6631499/create-directory-tree-with-forward-slash-in-directory-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!