Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

create directory with input

by darkblackblue (Novice)
on Dec 03, 2017 at 11:08 UTC ( #1204788=perlquestion: print w/replies, xml ) Need Help??

darkblackblue has asked for the wisdom of the Perl Monks concerning the following question:

hi , I want to create directory with input but ıt create random name?
#!/use/perl/bin use strict; print "Enter directory name :"; my $directory_name = <STDIN>; my $dir = "C:\Users\darkblack\Desktop\A\.$directory_name"; # It is proble , I cant write true format in up line mkdir( $dir ,0755) or die "Couldn't create $dir directory, $!"; print "Directory created successfully\n";

Replies are listed 'Best First'.
Re: create directory with input
by LanX (Sage) on Dec 03, 2017 at 11:33 UTC
    \backslashes escape inside Perl strings and things like "\U" mean uppercase the following characters.

    Maybe try to escape the backslashes


    or use Unix style paths.


    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Wikisyntax for the Monastery

      It didnt work when I used
      my $dir = "C:\\Users\\darkblack\\Desktop\\".$directory_name; or my $dir = "C:\\Users\\darkblack\\Desktop\\.$directory_name";
        It didnt work

        What was the error message,"Invalid argument" ?. Try adding chomp

        #!perl use strict; use warnings; my $cwd = 'C:/Users/darkblack/Desktop'; print "Enter directory name > "; chomp(my $dir = <STDIN>); $dir = "$cwd/$dir"; mkdir( $dir , 0755 ) or die "Could not create $dir : $!"; print "Directory $dir created successfully\n";
Re: create directory with input
by AnomalousMonk (Archbishop) on Dec 03, 2017 at 17:35 UTC

    To the specific root-cause solutions offered by others, I would add that enabling warnings along with its good friend strict will let Perl at least give you some hints about problems like this:

    c:\@Work\Perl\monks>perl -le "use warnings; use strict; ;; my $directory_name = 'foobar'; my $dir = \"C:\Users\darkblack\Desktop\A\.$directory_name\"; print qq{'$dir'}; " Unrecognized escape \d passed through at -e line 1. Unrecognized escape \D passed through at -e line 1. Unrecognized escape \A passed through at -e line 1. 'C:SERSDARKBLACKDESKTOPA.FOOBAR'
    Also, see Quote and Quote-like Operators in perlop for double-quotish escapology info.

    Give a man a fish:  <%-{-{-{-<

Re: create directory with input
by CountZero (Bishop) on Dec 04, 2017 at 11:04 UTC
    File::Spec provides a convenient interface to constructing directory names and filenames and it works in many operating systems:
    $path = File::Spec->catfile( @directories, $filename );

    In the present case it would go like this:

    use Modern::Perl qw/2017/; use File::Spec; my $directory_name = 'testdir'; my $path = File::Spec->catfile( qw/C: Users darkblack Desktop A/, $directory_na +me ); say $path;
    Output (in Windows): C:\Users\darkblack\Desktop\A\testdir

    But running under Unix it will return C:/Users/darkblack/Desktop/A/testdir

    File::Spec::Functions provides an even easier functional interface.


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: create directory with input
by kcott (Archbishop) on Dec 04, 2017 at 07:38 UTC

    G'day darkblackblue,

    This looks like you're running into a backslashitis nightmare. I think I'd aim for something closer to:

    my $path_base = 'C:\Users\darkblack\Desktop\A\.'; ... my $dir = $path_base . $directory_name;

    I don't have an MSWin box to test this on; however, the basic concept works fine on my system:

    $ pwd /Users/ken/tmp $ ls -al .fred ls: cannot access '.fred': No such file or directory $ perl -e 'my $x = q{/Users/ken/tmp/.}; my $y = q{fred}; mkdir $x . $y +' $ ls -al .fred total 0 drwxr-xr-x 2 ken staff 68 Dec 4 18:27 . drwxr-xr-x 19 ken staff 646 Dec 4 18:27 ..

    — Ken

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1204788]
Front-paged by haukex
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2023-06-09 17:48 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (36 votes). Check out past polls.