Gossamer Forum
Home : General : Perl Programming :

delete some files

Quote Reply
delete some files
i want to delete some files in a directory, not all files. i have code so far that lists the files with checkboxes so i can select the ones i want to delete. but i don't know what to do next.
Code:
sub list_csv_files {
my (@files, $file, $output, $found, $flag);

$page_title = "List CSV Files";
&html_page_top;

$submit_button = "Delete Files";
$reset_button = "Reset Form";

opendir (CSVDIR, "$db_csv_path") || &cgierr("unable to open directory in csv_list: $db_csv_path. Reason: $!");
@files = readdir(CSVDIR); # Read in list of files in directory..
closedir (CSVDIR);

FILE: foreach $file (@files) {
next if ($file =~ /^\./); # Skip "." and ".." entries..
next if ($file =~ /^index/); # Skip index.htm type files..

$output = gmtime((stat("$db_csv_path/$file"))[9]+3600*$time_offset);
my (@fixdate) = split' ',$output;
$output = $fixdate[0] . ' ' . $fixdate[1] . ' ' . $fixdate[2] . ', ' . $fixdate[4];
$found = 1;
if (!$flag) {
$flag = 1;
print qq|
<form action="$db_script_url" METHOD="POST">
<input type=hidden name="db" value="$db_setup">
<input type=hidden name="uid" value="$db_uid">
<table><tr><th>Delete</th><th></th><th></th></tr>|;
}


print qq|<tr><td valign="top"><INPUT TYPE=CHECKBOX NAME="$file" VALUE="delete">&nbsp;|;
print qq|<td valign="top"><a href="$db_csv_url/$file" target="_export" name="Export" title="Open file">$file</a><td valign="top">&nbsp;&nbsp;$output</td></tr>|;

}

if (!$found) { print qq|<p>no files found</p>|; }
if ($flag) {
print qq|</table>|;

print qq|
<p>Check which records you wish to delete and then press "Delete Files"</p>
<center><INPUT TYPE="SUBMIT" name="csv_cleanup" VALUE="$submit_button">&nbsp;
<INPUT TYPE="RESET" VALUE="$reset_button"></center>
</form>
|;
}
&html_admin_footer;
&html_page_bottom;

}

right now the csv_cleanup routine deletes all files in the directory. i don't know how to pass the files i want to delete to the routine. thanks!
Quote Reply
Re: [delicia] delete some files In reply to
Hi,

Personally, I'd do it slightly different. Instead of:

Code:
print qq|<tr><td valign="top"><INPUT TYPE=CHECKBOX NAME="$file" VALUE="delete">&nbsp;|;

..try:

Code:
print qq|<tr><td valign="top"><INPUT TYPE=CHECKBOX NAME="files_to_delete" VALUE="$file">&nbsp;|;

Then, in your function that does the deleting - use something like:

Code:
my @files_to_delete = $IN->param('files_to_delete');
foreach (@files_to_delete) { unlink($_) }

Hope that helps

Cheers

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] delete some files In reply to
thanks! i'll try that later today.