why won't this short script run? 
Author Message
 why won't this short script run?

Hi all.  I've been banging my head against the table with this puny little
script that is supposed to append an incrementing number to the beginning
of each filename in a directory.  It terminates just after entering the
foreach loop and I can't pinpoint why it is doing that. It does not warn or
die, it just stops running. I've used the de{*filter*}, read up in all of my
perl books and I just can't figure this one out.  Please help :(

SCRIPT
==============
#!/usr/bin/perl -w

use strict;

my($dir, $newname);
print "Enter directory: ";
chomp($dir=<STDIN>);

open(DH, $dir) || die "Cannot open $dir: $!";

close(DH);

my($oldname, $fileno);
# initialize the file counter
$fileno=1;

    $oldname=$_;
    $newname=sprintf("%03d_%s",$fileno, $oldname);
    next if (-e "$dir/$_");
    if(! rename "$dir/$oldname", "$dir/$newname") {
        warn "Could not rename $oldname to $newname: $!"
    } else {
        print("Renamed $oldname to $newname\n");
    }
    # increment the file counter
    $fileno++

Quote:
}



Sun, 16 Jan 2005 06:47:43 GMT  
 why won't this short script run?

  JJM> Hi all.  I've been banging my head against the table with this
  JJM> puny little script that is supposed to append an incrementing
  JJM> number to the beginning of each filename in a directory.  It
  JJM> terminates just after entering the foreach loop and I can't
  JJM> pinpoint why it is doing that. It does not warn or die, it just
  JJM> stops running. I've used the de{*filter*}, read up in all of my perl
  JJM> books and I just can't figure this one out.  Please help :(


  JJM>     $oldname=$_;

why have $oldname and $_ is use below?


  JJM>     $newname=sprintf("%03d_%s",$fileno, $oldname);
  JJM>     next if (-e "$dir/$_");

well, if the file exists (which it does since it is in the dir), you are
skipping it there. do you mean -d to skip dirs?

  JJM>     if(! rename "$dir/$oldname", "$dir/$newname") {

unless reads better there than if( !

  JJM>         warn "Could not rename $oldname to $newname: $!"
  JJM>     } else {
  JJM>         print("Renamed $oldname to $newname\n");
  JJM>     }
  JJM>     # increment the file counter
  JJM>     $fileno++
  JJM> }

uri

--

----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs  ----------------------------   http://www.*-*-*.com/



Sun, 16 Jan 2005 07:10:16 GMT  
 why won't this short script run?
Jon mused:
Quote:
> Hi all.  I've been banging my head against the table with this puny little
> script that is supposed to append an incrementing number to the beginning
> of each filename in a directory.  It terminates just after entering the
> foreach loop and I can't pinpoint why it is doing that. It does not warn or
> die, it just stops running. I've used the de{*filter*}, read up in all of my
> perl books and I just can't figure this one out.  Please help :(

> SCRIPT
>==============
> #!/usr/bin/perl -w

> use strict;

> my($dir, $newname);
> print "Enter directory: ";
> chomp($dir=<STDIN>);

> open(DH, $dir) || die "Cannot open $dir: $!";

> close(DH);

> my($oldname, $fileno);
> # initialize the file counter
> $fileno=1;

>     $oldname=$_;
>     $newname=sprintf("%03d_%s",$fileno, $oldname);
>     next if (-e "$dir/$_");

               ^^^^^^^^^^^^

All of the files you're testing for there exist. They're the ones
you're looping through. You probably meant to test for something else
(maybe you want to skip directories?).

[...]

Cheers,
Bernard
--
echo 42|perl -pe '$#="Just another Perl hacker,"'



Sun, 16 Jan 2005 07:15:55 GMT  
 why won't this short script run?
On Wed, 31 Jul 2002 05:47:43 GMT,

Quote:
> Hi all.  I've been banging my head against the table with this puny little
> script that is supposed to append an incrementing number to the beginning
> of each filename in a directory.  It terminates just after entering the

You mean prepend?

Quote:
> foreach loop and I can't pinpoint why it is doing that. It does not warn or

It doesn't terminate after entering the loop. I bet that it never even
enters the loop.

Quote:
> die, it just stops running. I've used the de{*filter*}, read up in all of my
> perl books and I just can't figure this one out.  Please help :(

> SCRIPT
>==============
> #!/usr/bin/perl -w

> use strict;

> my($dir, $newname);

Why do you declare $newname here, when you use it much later and could
use it with a limited scope?

Quote:
> print "Enter directory: ";
> chomp($dir=<STDIN>);

So, $dir is a directory, right?

Quote:
> open(DH, $dir) || die "Cannot open $dir: $!";

And open() is for files. opendir() is for directories.

opendir DH, $dir or die "Cannot opendir '$dir': $!\n";

This will not only give you the "files", but also the subdirectories,
including . and .. (if your system has those, and many do).

I am actually a bit surprised that Perl doesn't warn about this (5.6.1
doesn't). It really should warn when you try to use a handle in
readdir() that hasn't been opened with opendir() before. Looks a bit
like a bug-a-boo to me.

Quote:
> close(DH);

closedir DH;

Quote:
> my($oldname, $fileno);

Don't do this.. Instead use 'my' where you use the variable first.

Quote:
> # initialize the file counter
> $fileno=1;

my $fileno = 1;

Why get all the files in an array and then loop over them?

while (my $oldname = readdir DH)
{
    # loop contents

Quote:
}
>     $oldname=$_;

Why not directly


Quote:
>     $newname=sprintf("%03d_%s",$fileno, $oldname);
>     next if (-e "$dir/$_");
>     if(! rename "$dir/$oldname", "$dir/$newname") {
>         warn "Could not rename $oldname to $newname: $!"
>     } else {
>         print("Renamed $oldname to $newname\n");
>     }
>     # increment the file counter
>     $fileno++
> }

you do realise that if you run this twice, that it'll keep prepending
numbers? Maybe you need to exclude files that already start with
numbers.

I also think that you might be trying to rename the . and .. entries,
which I hardly think would be right.

Martien
--
                        |
Martien Verbruggen      | Never hire a poor lawyer. Never buy from a
Trading Post Australia  | rich salesperson.
                        |



Sun, 16 Jan 2005 07:19:43 GMT  
 why won't this short script run?
Also sprach Jon J. Morin:

Quote:
> Hi all.  I've been banging my head against the table with this puny little
> script that is supposed to append an incrementing number to the beginning
> of each filename in a directory.  It terminates just after entering the
> foreach loop and I can't pinpoint why it is doing that. It does not warn or
> die, it just stops running. I've used the de{*filter*}, read up in all of my
> perl books and I just can't figure this one out.  Please help :(

> SCRIPT
>==============
> #!/usr/bin/perl -w

> use strict;

> my($dir, $newname);
> print "Enter directory: ";
> chomp($dir=<STDIN>);

> open(DH, $dir) || die "Cannot open $dir: $!";

This can't work since you try to open a directory with open(). For that
you need to use opendir(). Oddly enough, the script wont die here nor
will it result in a warning.

opendir DH, $dir or die "Cannot open $dir: $!";

Quote:

> close(DH);

closedir DH;

Quote:
> my($oldname, $fileno);

Ditch that.

Quote:
> # initialize the file counter
> $fileno=1;

>     $oldname=$_;

This is better written as:

    my $fileno = 1

Quote:
>     $newname=sprintf("%03d_%s",$fileno, $oldname);
>     next if (-e "$dir/$_");
>     if(! rename "$dir/$oldname", "$dir/$newname") {
>         warn "Could not rename $oldname to $newname: $!"
>     } else {
>         print("Renamed $oldname to $newname\n");
>     }
>     # increment the file counter
>     $fileno++
> }

Tassilo
--

pam{rekcahbus;})(rekcah{lrePbus;})(lreP{rehtonabus;})(rehtona{tsuJbus!;
$_=reverse;s/sub/(reverse"bus").chr(32)/xge;tr~\n~~d;eval;


Sun, 16 Jan 2005 07:41:49 GMT  
 why won't this short script run?

Quote:

> Hi all.  I've been banging my head against the table with this puny little
> script that is supposed to append an incrementing number to the beginning
> of each filename in a directory.  It terminates just after entering the
> foreach loop and I can't pinpoint why it is doing that. It does not warn
> or die, it just stops running. I've used the de{*filter*}, read up in all of
> my
> perl books and I just can't figure this one out.  Please help :(

> SCRIPT
> ==============
> #!/usr/bin/perl -w

> use strict;

> my($dir, $newname);
> print "Enter directory: ";
> chomp($dir=<STDIN>);

> open(DH, $dir) || die "Cannot open $dir: $!";

> close(DH);

> my($oldname, $fileno);
> # initialize the file counter
> $fileno=1;

>     $oldname=$_;
>     $newname=sprintf("%03d_%s",$fileno, $oldname);
>     next if (-e "$dir/$_");
>     if(! rename "$dir/$oldname", "$dir/$newname") {
>         warn "Could not rename $oldname to $newname: $!"
>     } else {
>         print("Renamed $oldname to $newname\n");
>     }
>     # increment the file counter
>     $fileno++
> }

Thank you all.  I have the script up and running now.  I learned a lot from
your tips.  Guess I need to hit the books a little harder!!! :)

Jon



Sun, 16 Jan 2005 08:13:02 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. WHY won't this work? (short script)

2. Why won't my scripts run?

3. Please Help:My script runs on win32, it won't run on Unix

4. My script runs on win32, it won't run on Unix

5. Please Help: My script runs on win32, it won't run on Unix

6. script won't run...displays script text

7. Why won't this script work???

8. perl 4 script that won't run under perl 5

9. Perl CGI scripts won't run without -w switch (under Apache 1.3.x)

10. Why won't this little script work?

11. Perl scripts won't run on Linux

12. why won't this cgi script work?

 

 
Powered by phpBB® Forum Software