Perl Scripting problem - please help

Discussion in 'Perl Misc' started by Mark, Mar 18, 2005.

  1. Mark

    Mark Guest

    Hello,

    I am new to PERL and I want to do a simple function (in LINUX):

    find all files that have the extension pseudo.pdf and put the file
    names in a file;
    parse the file and change all file names current filename plus date:

    pseudo+date+.pdf

    Move the new file to pseudo directory :

    I have written the code which seems to work without any errors and has
    been accepted by PERL:

    #!/usr/bin/perl -w
    open (pdfs, "/var/www/html/reports/shops/pdflist.txt")
    ||Error('open','file');
    @fnames=<pdfs>;
    close (pdfs);
    ($second,$minute,$hour,$day,$month,$year,$weekday,$dayofyear,$isdst)=localtime(time);
    $cmonth=$month+1;
    $cyear=($year % 100);
    $fdate=$cmonth.'-'.$day.'-'.$cyear;
    $ext='.pdf';
    foreach $pdffile (@fnames2) {
    $pdffile=$pdffile.$fdate.$ext;
    }
    $n=1;
    foreach $fnames (@fnames) {
    $cnt=index($fnames,".pdf");
    $newfile=substr($fnames,0,$cnt);
    $newpdf=$newfile.$fdate.$ext;
    print "fnames: $fnames \n";
    print "new file: $newfile \n";
    print "newpdf: $newpdf \n";
    print "old= /var/www/html/reports/shops/$fnames \n";
    print "new= /var/www/html/reports/shops/$newfile/$newpdf \n";
    rename("/var/www/html/reports/shops/$fnames","/var/www/html/reports/shops/
    $newfile/$newpdf");
    print "\n";

    }


    I cannot get the rename function to work even if I use the same
    directory. I don't get any errors and the file permissions are set
    correctly on the files.

    Anyone know why this does not work?

    Thanks,

    Mark
     
    Mark, Mar 18, 2005
    #1
    1. Advertising


  2. > rename("/var/www/html/reports/shops/$fnames","/var/www/html/reports/shops/
    > $newfile/$newpdf");
    >
    > I cannot get the rename function to work even if I use the same
    > directory. I don't get any errors and the file permissions are set
    > correctly on the files.
    >
    > Anyone know why this does not work?


    You would get errors if you checked for them:

    rename("/var/www/html/reports/shops/$fnames","/var/www/html/reports/shops/
    $newfile/$newpdf") or die "rename failed: $!";

    If what you posted is your exact code, I think the problem is in the
    nicely formatted call to rename: the indentation in front of the second
    line will be part of the argument.

    In that case, this is better:

    rename("/var/www/html/reports/shops/$fnames",
    "/var/www/html/reports/shops/$newfile/$newpdf")
    or die "rename failed: $!";

    The problem may also be that the $newfile directory component does not
    exist. If so, you have to create it before attempting to move files into
    it. See 'mkdir'.
     
    Arndt Jonasson, Mar 18, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark
    Replies:
    3
    Views:
    620
    Joe Smith
    Mar 18, 2005
  2. Replies:
    4
    Views:
    552
    Chris Uppal
    May 5, 2005
  3. KK
    Replies:
    2
    Views:
    664
    Big Brian
    Oct 14, 2003
  4. Ron Stephens
    Replies:
    23
    Views:
    2,980
    Ron Stephens
    Apr 12, 2004
  5. DaveInSidney
    Replies:
    0
    Views:
    446
    DaveInSidney
    May 9, 2005
Loading...

Share This Page