Link to home
Start Free TrialLog in
Avatar of raaj4354
raaj4354

asked on

Script to check double quotes in files under directories

I have two folders A and B. Folder A has files with extension .sql and folder B has the files with extension .pdc. The script should take the path to these folders as a parameter. The script should check if the files exist in that folder and then
look for strings with double quotes in the files for both folders. If any strings are found with double quotes echo something like Error :file has double quoted strings.
Avatar of FishMonger
FishMonger
Flag of United States of America image

What have you tried thus far?

What errors/warnings are you receiving?
Avatar of raaj4354
raaj4354

ASKER

I am new to perl. Tried reading the file and print the strings with quotes did not get too far.!
SOLUTION
Avatar of whosbetterthanme
whosbetterthanme
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This may need a slight tweak, but is the direction I'd take.

#!/usr/bin/perl

use strict;
use warnings;

my %dir = (
    dirA => '.sql',
    dirB => '.pdc',
);


while (my ($dir,$ext) = each %dir) {
    opendir my $dir_handle, $dir or die "failed to open directory $dir $!";
    
    FILE:
    while (my $file = grep(/$ext$/, readdir $dir_handle)) {
        open my $fh, $file or die "failed to open '$file' $!";
        while (my $line = <$fh>) {
            if ($line =~ /".+"/) {
                print "Error: found double quoted string in $file\n";
                close $fh;
                next FILE;
            }
        }
    }
}

Open in new window

FishMonger:

Put the script in the same directory as the folders A and B. Gave the path to folders and ran the script .

Error: No such file or directory at the path/to/the script
Did you use a relative or absolute path?

Please post the adjusted script you ran and the exact wording of the complete error message.
So the folders name here are sql and pdc. These are under a directory called code. The sql folder has files with extension .sql and same with pdc as .pdc. I substituted dirA and dirB with the sql and pdc. I put the script on desktop and ran it as script.pl c:/path/to/directory/code

Error:failed to open '518' No such file or directory at c:/apth/to/script/desktop/script.pl
The script isn't setup to accept command line arguments.

If you need to pass the directory path as an arg, then we'll need to adjust the script.  Or, you could hard code the path in the hash key.
We also need to adjust the open statement so that it includes the full path.  As is, it only uses the filename without the path, which will fail with the "No such file or directory" error.
I modfied the script to give directory as parameters .did not work. I really appreciate if any help to complete it..!!


#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;          # get options from command line

my %dir = (sql => '.sql',
           pdc => '.pdc',);


while (my ($dir,$ext) = each %dir) {
    opendir my $dir_handle, $dir or die "failed to open directory $dir $!";
   
    FILE:
    while (my $file = grep(/$ext$/, readdir $dir_handle)) {
        open my $fh, $file or die "failed to open '$file' $!";
        while (my $line = <$fh>) {
            if ($line =~ /".+"/) {
                print "Error: found double quoted string in $file\n";
                close $fh;
                next FILE;
            }
        }
    }
}
{
      print(
            "Manditory:
\t -d <directory which has the files>
\n"      );
}
       
MAIN:
{
      #  get the user input from the command line
      GetOptions( "d|directory=s" );

      # unless we have some key information, tell the user the expected format
      unless ( ($opt_d ) ) {
            usage();
            exit 1;
      }       
      $dir = $opt_d;
      }
open my $fh, "<$dir/$file" or die "failed to open '$dir/$file' $!";
Saying "did not work" does not tell us anything.  Please be specific.

As I previously mentioned, the open statement needed to be adjusted/corrected to include the full path, which hasn't been done.

The getopt code need to come before processing of the files, since that code relies on the path that getopt retrieves.

#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Long;

GetOptions( "d|directory=s" > \my $path );
-d $path or die usage();

my %dir = (
        sql => '.sql',
        pdc => '.pdc',
);

while (my ($dir, $ext) = each %dir) {
    
    opendir my $dir_handle, "path/$dir"
        or die "failed to open directory '$path/$dir' $!";
   
    my @files = grep(/$ext$/, readdir $dir_handle);
    closedir $dir_handle;
    
    FILE:
    foreach my $file (@files) {
        $file = "$path/$dir/$file";
        
        open my $fh, '<', $file or die "failed to open '$file' $!";
        while (my $line = <$fh>) {
            if ($line =~ /".+"/) {
                warn "Error: found double quoted string in $file\n";
                close $fh;
                next FILE;
            }
        }
    }
}


sub usage {

    print "usage: $0 -d base_directory";

}

Open in new window

I ran the script above as it is above..!!....below.is the output error message. Ora is the directory which has the sql and pdc folders.

C:\Documents and Settings\user\Desktop>find-quotes.pl -d ora
Argument "d|directory=s" isn't numeric in numeric gt (>) at C:\Documents and Set
tings\user\Desktop\find-quotes.pl line 7.
Unknown option: d
Use of uninitialized value $path in -d at C:\Documents and Settings\user\Desk
top\find-quotes.pl line 8.
1 at C:\Documents and Settings\user\Desktop\find-quotes.pl line 8.
usage: C:\Documents and Settings\user\Desktop\find-quotes.pl -d base_directory
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The script works fine after the modification above. But I am having issues if the folder has files with more than one extension.I have a folder spp which has files with extension .sps,.spb,.hps and .hpb. I gave

spp => '.sps',
spp => '.spb',
spp => '.hps',
spp => '.hpb',

but it only takes the first one. ..  .sps and ignores the files with other extensions.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks very much...!!!