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.
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.
ASKER
I am new to perl. Tried reading the file and print the strings with quotes did not get too far.!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
}
}
}
}
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
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.
Please post the adjusted script you ran and the exact wording of the complete error message.
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
Error:failed to open '518' No such file or directory at c:/apth/to/script/desktop/
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.
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.
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;
}
#!/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.
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";
}
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-qu otes.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
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-qu
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
usage: C:\Documents and Settings\user\Desktop\find
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks very much...!!!
What errors/warnings are you receiving?