Gossamer Forum
Home : General : Perl Programming :

reading/deleting file names from a directory

Quote Reply
reading/deleting file names from a directory
Hi,

I have developed a simple script to allow a user to post information from a form to a file. I want to develop a companion script to read file titles from a directory and give the user the ability to delete a file. I have done some searching for examples or tutorials of this kind of thing, but haven't been successful in finding one - any ideas, pointers, or snippets would be greatly appreciated!

Thanks,

Mark
Quote Reply
Re: [perlbie] reading/deleting file names from a directory In reply to
Hi,

I found an example of how to delete a file:

#!/usr/bin/perl

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

$delete_this_one="/home/www/testfile";

if (unlink $delete_this_one) {

print "The file was successfully deleted.\n\n";
exit;

} else {

print "The file was not successfully deleted.\n\n";

}

exit;


I'm still looking for a way to read a directory, display it, and then let a user choose a file name to pass to be deleted...

Thanks,

Mark
Quote Reply
Re: [perlbie] reading/deleting file names from a directory In reply to
Would you be interested in using PHP? I know how to do it in PHP Cool

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [perlbie] reading/deleting file names from a directory In reply to
use CGI;

my $x = new CGI;

my $dir = $x->param('directory');

if ($dir =~ /[a-z]/){

opendir(DIR, $dir);

@files = readdir(DIR);

shift @files; shift @files;

closedir(DIR);

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

print "<ul>";

foreach $file (@files){

print "\n<li><a href=?action=delete&file=$file>$file</a>";

}

if ($action eq "delete"){

unlink($file);

print "File was removed";

}





kind of. You should do file tests on the files to see if it is a directory or a binary file etc, and then change the icon or link appropriately.





From Learning Perl:



Table 10.1: File Tests and Their Meanings[/url]

File Test

Meaning [/url]-r

File or directory is readable [/url]-w

File or directory is writable [/url]-x

File or directory is executable [/url]-o

File or directory is owned by user [/url]-R

File or directory is readable by real user, not effective user (differs from -r for setuid programs) [/url]-W

File or directory is writable by real user, not effective user (differs from -w for setuid programs) [/url]-X

File or directory is executable by real user, not effective user (differs from -x for setuid programs) [/url]-O

File or directory is owned by real user, not effective user (differs from -o for setuid programs) [/url]-e

File or directory exists [/url]-z

File exists and has zero size (directories are never empty) [/url]-s

File or directory exists and has nonzero size (the value is the size in bytes) [/url]-f

Entry is a plain file [/url]-d

Entry is a directory [/url]-l

Entry is a symlink [/url]-S

Entry is a socket [/url]-p

Entry is a named pipe (a "fifo") [/url]-b

Entry is a block-special file (like a mountable disk) [/url]-c

Entry is a character-special file (like an I/O device) [/url]-u

File or directory is setuid [/url]-g

File or directory is setgid [/url]-k

File or directory has the sticky bit set [/url]-t

isatty() on the filehandle is true [/url]-T

File is "text" [/url]-B

File is "binary" [/url]-M

Modification age in days [/url]-A

Access age in days [/url]-C

Inode-modification age in days
Quote Reply
Re: [Donald Rumsfeld] reading/deleting file names from a directory In reply to
you will realise that everything has been shifted around a bit in the above table.
Quote Reply
Re: [Donald Rumsfeld] reading/deleting file names from a directory In reply to
Hey Donald,

Thanks for your code and explanation and reference - I went out and got the perl programming book. I'm just starting. Would you be up for a 15-20 minute conversation (on my dime) or IM chat to help explain the code a little. I don't get the my thing, the param thing, the shift shift thing, and the action thing.

Thanks,

Mark
Quote Reply
Re: [perlbie] reading/deleting file names from a directory In reply to
you im details aren't specified, so i'll post the details here.

------------------
the my thing
------------------


you don't actually need this to start off with. You can ignore it. It's to do with a module called stict which checks everything and helps when running really big programs.

the param thing

this is really useful. If you put

use CGI;

at the top of the script,and then run the program you can feed variables into the program like you always do:

http://www.thing.com/...ram.pl?myname=donald

you can then do a $name = $x->param('myname'); and it will return donald.

that's basically all the action thing is about.

hth.

Donald Rumseld.
Quote Reply
Re: [Donald Rumsfeld] reading/deleting file names from a directory In reply to
the shift shift thing

when you read the directory listings into an array using readdir then you also get the . and .. files - which mean same dir and up a dir - you can get rid of these by doing two shifts.
Quote Reply
Re: [Donald Rumsfeld] reading/deleting file names from a directory In reply to
btw i hope you realise that i am not actually donald rumsfeld and you know who he is etc. my im details are not the ones listed..ack.