Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Creating folders using strings

by leaninperl (Initiate)
on Nov 22, 2012 at 14:54 UTC ( #1005140=perlquestion: print w/replies, xml ) Need Help??
leaninperl has asked for the wisdom of the Perl Monks concerning the following question:

Very new to perl, but good with a few other programming languages. I have a text file with names in it. I am trying to build a folder for each name in the text file. Here is what I have so far:
#!/usr/local/bin/perl open (MYFILE, 'C:\Users\64R92\perl\cmd\site_list.txt'); while (<MYFILE>) { chomp; $line = $_; $directory = mkdir("$line") || die "Unable to create directory <$!>\n"; print "$_\n"; } close (MYFILE);
When I run this, it puts the new folder on my desktop. I'd like to put it in 'C:\Users\64R92\perl\cmd\', the same place where I'm reading the text file from. I've searched all over the web and can't find how to name that per entry and nothing I've tried has worked. Thanks in advance.

Replies are listed 'Best First'.
Re: Creating folders using strings
by choroba (Chancellor) on Nov 22, 2012 at 15:16 UTC
    Either chdir into the working directory before entering the loop, or specify the full path for mkdir as mkdir "$fullpath/$line"
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Creating folders using strings
by 2teez (Priest) on Nov 22, 2012 at 15:39 UTC

    Hi learninPerl,
    choroba has given you in my considered opinion, the best answer. However, if you don't mind, there are some good practice you may develop as you program in Perl.

    1. use warnings and strict in your script
    2. Avoid bareword as filehanles, use lexical variable in your open function.
      use 3 argument open function and always check, if that fails. The same goes for close function.
      like so:
      open my $fh,'<', $filename or die "can't open file: $!"; ... close $fh or die "can't close file: $!";
      or use autodie qw(open close); Then, is no need to check.

    In addition, you can actually run your script from the location, where you wanted the folders to be created ( but that will be 'restricting' yourself script ) :).

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Creating folders using strings
by karlgoethebier (Prior) on Nov 23, 2012 at 08:51 UTC addition a variation on this theme:

    #!c:/perl/bin/perl.exe use IO::All; use strict; use warnings; use autodie qw(io); my $io = io(shift); for my $line($io->getlines){ print qq(->$line); # and do the stuff }

    More details here. Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: Creating folders using strings
by Tommy (Chaplain) on Nov 23, 2012 at 13:12 UTC

    Have you tried:

    my $dir = 'C:\Users\64R92\perl\cmd'; mkdir("$dir\\$line") or die "Unable to create directory <$!>\n";
    $ perl -MMIME::Base64 -e 'print decode_base64 "YWNlQHRvbW15YnV0bGVyLm1lCg=="'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1005140]
Front-paged by Arunbear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2017-06-24 10:42 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (557 votes). Check out past polls.