Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

From a Newbie/help diagnosing an error opening a directory

by nu2thegame (Initiate)
on Oct 31, 2012 at 15:19 UTC ( [id://1001684]=perlquestion: print w/replies, xml ) Need Help??

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

Hello... Perl lovers (one of which I am seeking to be)....I have the following script but it is returning errors..Here is the script:

use strict; use warnings; use File::Copy 'move'; # Determine destination folder for file based on filename. # All destinations are below a fallback destination provided as input. sub make_dest_finder { my $fallback_dest = shift; return sub { my $fnam = shift; # Determine basename to start looking for a destination folder +. my $basename = substr $fnam, 0, rindex $fnam, '.'; # Shorten name while no homonymous folder exists. chop $basename while $basename && ! -d "$fallback_dest/$basena +me"; return "$fallback_dest/$basename" if $basename; return $fallback_dest; }; } my $srcdir = "/tmp/folder"; my $dest_finder = make_dest_finder '/tmp/folder2'; opendir my $dh, $srcdir or die "Can't open $srcdir: $!"; my @files = grep ! /^\.+$/, readdir $dh; close $dh; my $moved = 0; foreach my $file (@files) { my $old = "$srcdir/$file"; my $dest = $dest_finder->( $file ); print STDERR "moving $file to $dest\n"; if ( ! move $old, $dest ) { warn "Move $old -> $dest failed: $!"; last; } $moved++; } print STDERR "\n\n- moved $moved files\n";

The error states that "Can't open /tmp/folder: Not a directory at movingfilesscript.pl line 23" Can anyone please help a newbie... And let me apologize if this is not the right forum or if in some way, I have disrespect the rules of the forum... This is literally my first post ...

Replies are listed 'Best First'.
Re: help diagnosing an error opening a directory
by MidLifeXis (Monsignor) on Oct 31, 2012 at 15:41 UTC

    Do -d and -e, and ls -l /tmp/folder give any idea what may be happening?

    --MidLifeXis

Re: From a Newbie/help diagnosing an error opening a directory
by Kenosis (Priest) on Oct 31, 2012 at 15:46 UTC

    Hi, ne2thegame, and welcome to PerlMonks!

    Your script ran fine for me after removing the leading forward slashes in the paths:

    my $srcdir = "tmp/folder"; my $dest_finder = make_dest_finder 'tmp/folder2';
Re: From a Newbie/help diagnosing an error opening a directory
by tobyink (Canon) on Oct 31, 2012 at 16:04 UTC

    "Can't open /tmp/folder: Not a directory at movingfilesscript.pl line 23"

    The "Not a directory" error usually happens when you try to call opendir on something that exists but is... ummm... not a directory! For example, it may be a file or a FIFO or some other gubbin on the filesystem.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-03-29 15:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found