Professional Web Applications Themes

Error in Script - PERL Beginners

Hi, I have wrote a script to search for a pattern and replace it in all files of a directory,that i specified at commandline. I want another one thing is to be done in the script.That's,it should search only for the type of files I specified at commandline. That is,it should get the extension of file name and search only those files in the directory. I have made a attempt in this script,but it goes on asking extension for all the files in directory. Plz help me in getting the things right in the following script. Here I have use ...

  1. #1

    Default Error in Script


    Hi,
    I have wrote a script to search for a pattern and replace it in
    all files of a directory,that i specified at commandline.
    I want another one thing is to be done in the script.That's,it should
    search only for the type of files I specified at commandline.
    That is,it should get the extension of file name and search only those
    files in the directory.

    I have made a attempt in this script,but it goes on asking extension for
    all the files in directory.
    Plz help me in getting the things right in the following script.
    Here I have use Split operator to get the extension of filename.


    #! /usr/bin/perl

    print "Enter a path name: ";
    my $path=<STDIN>;
    chomp($path);
    opendir THISDIR, "$path" or die "serious dainbramage: $!";
    my allfiles = readdir THISDIR;

    # get all files
    foreach $file (allfiles){
    $filetoopen = $path ."/" .$file;

    # filter to check the type of file
    print "Enter the type of extension:";
    my $ext=<STDIN>;
    chomp($ext);
    ($str1,$str2) = split(/./, $filetoopen);
    if($str2 eq $ext)
    {
    print $str2;
    print $filetoopen;
    open(IN, "<$filetoopen") || die "cannot open file\n";
    open(OUT, ">$test") || die "cannot open file\n";
    while (<IN>){
    if (/$com/){
    s/$com/td>\n<\\script>/g;
    }
    if (/$img/){
    s/$img/\n<script>\n<img/g;
    }
    if (/$pattern/){
    s/$pattern/$own/g;
    # print $_;
    }
    if (/img/){
    s/$img/doent.write("<img/g;
    }
    if (/$com/){
    s/$com/td>");/g;
    }
    print OUT $_;
    }
    close (OUT);
    close (IN);
    rename("$test","$filetoopen");
    }}

    Thanks inadvance,
    Prabu.


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.620 / Virus Database: 399 - Release Date: 3/11/2004



    Prabu Guest

  2. #2

    Default Re: Error in Script

    That's 'cause you have put the question inside the foreach loop: 

    Btw, it would be easier to just use the fileglob operator from the start
    (with a pattern of the form "path/*.ext"):

    #! /usr/bin/perl

    print "Enter a glob pattern:";
    my $pattern=<STDIN>;
    chomp($pattern);
    my files = glob ($pattern) ;
    files or die "serious dainbramage: $!";

    foreach my $file (files) {
    print $filetoopen;
    open(IN, "<$filetoopen") || die "cannot open file\n";
    ....

    Martin

    Martin Guest

  3. #3

    Default Re: Error in Script

    Prabu wrote: 

    Hello,
     

    That is because you are asking for the extension inside the loop. Just
    ask before you enter the loop.

     

    use warnings;
    use strict;

     

    perldoc -q vars

     

    Move the previous four lines outside of the foreach loop.

     

    The . character is special in a regular expression, it matches all
    characters except the newline character.

     

    Where did $test come from? You are using $! in your opendir() error
    message, you should use it here as well.

     

    Where did $com, $img, $pattern and $own come from? There is no need to
    do a match before you do a substitution with the same regular
    expression.

     

    You have already removed $img and $com above. What did you think these
    substitutions would accomplish?

     

    perldoc -q vars

    You should verify that rename() succeeded.
     

    ----------------------------------------

    #!/usr/bin/perl
    use warnings;
    use strict;

    print 'Enter a path name: ';
    chomp( my $path = <STDIN> );

    print 'Enter the type of extension: ';
    chomp( my $ext = <STDIN> );

    opendir THISDIR, $path or die "serious dainbramage: $!";
    my allfiles = map "$path/$_", grep /\Q$ext$/, readdir THISDIR;

    # get all files
    foreach my $filetoopen ( allfiles ) {

    print $filetoopen;
    open IN, "<$filetoopen" or die "cannot open file: $!";
    open OUT, ">$test" or die "cannot open file: $!";

    while ( <IN> ) {
    s/$com/td>\n<\\script>/g;
    s/$img/\n<script>\n<img/g;
    s/$pattern/$own/g;
    s/$img/doent.write("<img/g;
    s/$com/td>");/g;
    print OUT $_;
    }
    close OUT;
    close IN;
    rename $test, $filetoopen or warn "Cannot rename $test to
    $filetoopen: $!";
    }

    __END__



    John
    --
    use Perl;
    program
    fulfillment
    John Guest

  4. #4

    Default Re: Error in Script


    You'll need to fix this also 
    It should be:
    ($str1,$str2) = split(/\./, $filetoopen);
    So that split can funcrion as you expect.



    David Guest

Similar Threads

  1. IE Script Error
    By t7b in forum Macromedia Contribute General Discussion
    Replies: 5
    Last Post: April 22nd, 07:28 AM
  2. CloneModelFromCastMember : Script error: Error
    By Webfoot Ben in forum Macromedia Director 3D
    Replies: 0
    Last Post: December 4th, 06:36 AM
  3. Malformed Header from Script error (Error 500)
    By chrisredfield_0 in forum PHP Development
    Replies: 0
    Last Post: September 27th, 06:11 PM
  4. Publisher Error or Script Error
    By rstoney in forum Web Design
    Replies: 1
    Last Post: October 8th, 08:41 PM
  5. Java Script error (error 3) when trying to access Commands
    By Pvt. Grover in forum Macromedia Dreamweaver
    Replies: 2
    Last Post: August 7th, 09:27 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139