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.
Perl

Avatar of undefined
Last Comment
raaj4354

8/22/2022 - Mon
FishMonger

What have you tried thus far?

What errors/warnings are you receiving?
raaj4354

ASKER
I am new to perl. Tried reading the file and print the strings with quotes did not get too far.!
SOLUTION
whosbetterthanme

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
FishMonger

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

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
raaj4354

ASKER
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
FishMonger

Did you use a relative or absolute path?

Please post the adjusted script you ran and the exact wording of the complete error message.
raaj4354

ASKER
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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
FishMonger

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.
FishMonger

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.
raaj4354

ASKER
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;
      }
Your help has saved me hundreds of hours of internet surfing.
fblack61
ozo

open my $fh, "<$dir/$file" or die "failed to open '$dir/$file' $!";
FishMonger

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

raaj4354

ASKER
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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
ozo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
raaj4354

ASKER
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
FishMonger

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
raaj4354

ASKER
Thanks very much...!!!