Current location: Hot Scripts Forums » Programming Languages » Perl » finding mismatches and deleting?


finding mismatches and deleting?

Reply
  #1 (permalink)  
Old 10-07-06, 12:52 PM
vbsaltydog vbsaltydog is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
finding mismatches and deleting?

I am trying to make a cgi file that can compare the filenames in a directory to a field in a mysql db and if the comparison directory filename is not in the db then I would like to output the found files into a web form and have a delete button for each item.
I know how to connect to the db and I guess I can us a not exists in my if statement to locate the mismatched files and I am fairly certain that unlink will be the method for deleting the files but how can I place the "delete" button in the web form for each found file?

Your help is always appreciated,

-vbsaltydog
Reply With Quote
  #2 (permalink)  
Old 10-07-06, 01:16 PM
Drunken Perl Coder Drunken Perl Coder is offline
Wannabe Coder
 
Join Date: Aug 2006
Posts: 110
Thanks: 0
Thanked 0 Times in 0 Posts
you probably want to use a checkbox instead of a button for each file. Assume you have the names in an array alreay:

Code:
#beginning of form stuff here
foreach my $file (@files) {
   print qq~<input type="checkbox" name="delete" value="$file"> $file <br />\n~;
} 
#end of form stuff here
using the CGI module you get the value of all the checked checkboxes:

Code:
my @list_to_delete = param('delete');
foreach my $files (@list_to_delete) {
   unlink("path/to/$files") or next; 
}
that should get you started. You will want to use Taint mode and make sure you are using the correct path to the files. Deleting files should always be treated as a dangerous thing to do, so make sure everything is correct before actually deleting any files.
Reply With Quote
  #3 (permalink)  
Old 10-07-06, 09:24 PM
vbsaltydog vbsaltydog is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the reply. I am however, having issues getting my results for deletion.

I have created an array of all files in the directory using:

Code:
$imgdir = "/var/../../../product_xlarge";

      opendir(INDIR, $imgdir) or die "Directory Not Found";

      @imgdir_contents = readdir(INDIR);

      closedir(INDIR);
and I have queried my db for the table field results like so:

Code:
$host = "localhost";
$database = "mydb";
$tablename = "myfield";
$user = "myuser";
$pw = "mypassword";

$connect = Mysql->connect($host, $database, $user, $pw);
$connect->selectdb($database);
$myquery = "SELECT product_imgxlg FROM $tablename";
$execute = $connect->query($myquery);

while (@results = $execute->fetchrow()) {

} #End of while statement.

$sth->finish();
$dbh->disconnect();
I need to return the results of the items that exist in @imgdir_contents and do not exist in @results but I dont want the results of the reverse so I dont want results that match @results items that do not exist in @imgdir_contents

I suppose that I could compare the arrays but I can only access the @results array inside of the while loop since I am a noob and dont know how to pass the @results outside of the while code block.

Your help is appreciated.
Reply With Quote
  #4 (permalink)  
Old 10-07-06, 11:22 PM
Drunken Perl Coder Drunken Perl Coder is offline
Wannabe Coder
 
Join Date: Aug 2006
Posts: 110
Thanks: 0
Thanked 0 Times in 0 Posts
see if this helps:

Code:
$host = "localhost";
$database = "mydb";
$tablename = "myfield";
$user = "myuser";
$pw = "mypassword";

$connect = Mysql->connect($host, $database, $user, $pw);
$connect->selectdb($database);
$myquery = "SELECT product_imgxlg FROM $tablename";
$execute = $connect->query($myquery);

my @results_from_db = ();
while (@results = $execute->fetchrow()) {
   push @results_from_db,@results;
} #End of while statement.

$sth->finish();
$dbh->disconnect();

print "$_\n" for @results_from_db;
see what gets printed.
Reply With Quote
  #5 (permalink)  
Old 10-07-06, 11:23 PM
Drunken Perl Coder Drunken Perl Coder is offline
Wannabe Coder
 
Join Date: Aug 2006
Posts: 110
Thanks: 0
Thanked 0 Times in 0 Posts
make sure to print an HTTP header first if you are running that from a browser.
Reply With Quote
  #6 (permalink)  
Old 10-08-06, 08:37 PM
vbsaltydog vbsaltydog is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
The code that you suggested outputs nothing to the browser. I am printing the content type as html.

-vbsaltydog
Reply With Quote
  #7 (permalink)  
Old 10-09-06, 01:08 AM
Drunken Perl Coder Drunken Perl Coder is offline
Wannabe Coder
 
Join Date: Aug 2006
Posts: 110
Thanks: 0
Thanked 0 Times in 0 Posts
are you getting any results from your DB query?
Reply With Quote
  #8 (permalink)  
Old 10-09-06, 11:18 AM
vbsaltydog vbsaltydog is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
I dont get any printout from your code but yes, the query yields results when run at the mysql prompt.

When I run your code with the -c option it says syntax is OK but when I run it without the -c it prints the http header and then says

Cant call method "finish" on an undefined value at myfile line 41.

Here is the entire script currently.

Code:
use DBI;
use Mysql;


print "Content-type: text/html\n\n";

$imgdir = "/var/www/../../product_xlarge";

      opendir(INDIR, $imgdir) or die "Directory Not Found";

      @imgdir_contents = readdir(INDIR);

      closedir(INDIR);

#      foreach $file(@imgdir_contents) {
#$cbcimg = $file;
#print "$cbcimg<br>";
#      } #End of Foreach.


# MYSQL CONFIG VARIABLES
$host = "localhost";
$database = "mydb";
$tablename = "product";
$user = "myuser";
$pw = "mypassword";

$connect = Mysql->connect($host, $database, $user, $pw);
$connect->selectdb($database);
$myquery = "SELECT product_imgxlg FROM $tablename";
$execute = $connect->query($myquery);

my @results_from_db = ();
while (@results = $execute->fetchrow()) {
   push @results_from_db,@results;
} #End of while statement.

$sth->finish();
$dbh->disconnect();

print "$_\n" for @results_from_db;
Thanks again for helping.
Reply With Quote
  #9 (permalink)  
Old 10-09-06, 04:18 PM
Drunken Perl Coder Drunken Perl Coder is offline
Wannabe Coder
 
Join Date: Aug 2006
Posts: 110
Thanks: 0
Thanked 0 Times in 0 Posts
You need to double-check all your mysql stuff. You are mixing up objects. These two objects are never defined for example:

$sth->finish();
$dbh->disconnect();

probably should be:

$connect->finish();
$connect->disconnect();

I am really rusty with the sql stuff so check the module documentation.
Reply With Quote
  #10 (permalink)  
Old 10-10-06, 12:54 AM
vbsaltydog vbsaltydog is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
OK.Your idea worked but it took some tweaking.

Code:
#!/usr/bin/perl


use DBI;

print "Content-type: text/html\n\n";
print "<table width=\"800\" border=\"1\"><tr><td>";


$imgdir = "/var/www/../../product_xlarge";

      opendir(INDIR, $imgdir) or die "Directory Not Found";

      @pre_imgdir_contents = readdir(INDIR);

      closedir(INDIR);

my $dbh = DBI->connect( 'dbi:mysql:my_db',
                        'mydbuser',
                        'mydbpass',
                      ) || die "Database connection not made: $DBI::errstr";

my $sql = qq{ SELECT product_imgxlg FROM product };
my $sth = $dbh->prepare( $sql );
$sth->execute();

my @res = ();

while (@array = $sth->fetchrow_array()) {
push @res,@array;

} #End of while.

@imgdir_contents = grep(/\.jpg/, @pre_imgdir_contents);

%res = map {$_, 1} @res;
@del = grep {!$res {$_}} @imgdir_contents;


foreach $row(@del) {

print "$row<br>";

} #End of foreach.

print "</td><td width=\"50\%\"></td></tr></table>";

$sth->finish();
$dbh->disconnect();
But now that I have the results stored in an array and sent to an HTML page..how can I delete each array element using a form checkbox with an option for "select all" ?

Your continual help is appreciated.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 04:45 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.