Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

mkdir in a loop?

by Anonymous Monk
on Apr 05, 2006 at 09:30 UTC ( #541313=perlquestion: print w/replies, xml ) Need Help??

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

hi monks,

I have a list of filenames, and for each one I want to create a directory with that filename. However, I keep getting directories with '?' on the end that don't actually seem to exist. Can anyone advise? Thanks!

open (FILE, "all_controls.txt") or die "can't open file $!"; my @file=<FILE>; foreach my $file (@file) { mkdir "$file"; }

Replies are listed 'Best First'.
Re: mkdir in a loop?
by ambrus (Abbot) on Apr 05, 2006 at 09:31 UTC
Re: mkdir in a loop?
by murugu (Curate) on Apr 05, 2006 at 09:46 UTC
    open (FILE, "all_controls.txt") or die "can't open file $!"; my @file=<FILE>; chomp @file; foreach my $file (@file) { mkdir "$file"; }

    Regards,
    Murugesan Kandasamy
    use perl for(;;);

Re: mkdir in a loop?
by wand3ringscript3r (Initiate) on Apr 05, 2006 at 15:01 UTC
    Check "all_control.txt" for any extra formatting characters. #!/usr/bin/perl use strict; my $file = "all_controls.txt"; open(FILE, "<$file") || die "Unable to open $file: $!\n"; my @file_contents = <FILE>; foreach (@file_contents) { chomp; $_ =~ /^(.*?)\..*?$/; # remove filename extension mkdir ($1, 0755) || die "Unable to create directory: $!\n"; print "Created directory: $1\n" if (! $@); } # end
Re: mkdir in a loop?
by wand3ringscript3r (Initiate) on Apr 05, 2006 at 14:54 UTC
    I noticed the "?" mark too. Check the "all_control.txt" file to see if it has any extra formatting characters. Then the following should work fine.

    #!/usr/bin/perl use strict; my $file = "all_controls.txt"; open(FILE, "<$file") || die "Unable to open $file: $!\n"; my @file_contents = <FILE>; foreach (@file_contents) { chomp; $_ =~ /^(.*?)\..*?$/; # remove filename extension after '.' mkdir ($1, 0755) || die "Unable to create directory: $!\n"; print "Created directory: $1\n" if (! $@); } # end

    Edit: g0n - code tags

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://541313]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2022-01-20 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (56 votes). Check out past polls.

    Notices?