PDA

View Full Version : How do I get a list of files in a directory?


donb
04-21-06, 03:04 AM
I need to get a list of files in a directory so that I can process them. I am on empressa 3.96

Thanks

Don B

Dramatic
04-21-06, 04:33 AM
In Miva 3.x you need a little help from some friends: mostly perl

Here's a miva function I use:

<MvCOMMENT>
=============================================
) getdir(path,type)
=============================================
</MvCOMMENT>
<MvFUNCTION NAME="getdir" PARAMETERS="path,type" STANDARDOUTPUTLEVEL="text,html,compresswhitespace">

<MvIF EXPR="{sexists(l.path)}">
<MvASSIGN NAME="l.checkfile" VALUE = "dd_getdir.dat">
<MvIF EXPR="{fexists(l.checkfile)}">
<MvASSIGN NAME="l.ok" VALUE = "{fdelete(l.checkfile)}">
</MvIF>
<MvASSIGN NAME="l.code" VALUE = "{s.time_t $ s.process_id}">
<MvDO FILE="/lib/md5.mv" NAME="g.enc" VALUE="{calcmd5(l.code)}">
<MvEXPORT FILE = "{l.checkfile}" FIELDS = "g.enc" DELIMITER = "">
<MvASSIGN NAME="l.ok" VALUE = "{fchmod(l.checkfile,0755) }">
<MvIF EXPR="{l.type EQ 's'}">
<MvASSIGN NAME="g.dirpath" VALUE = "{'/var/www/html' $ l.path $ '/'}">
<MvELSE>
<MvIF EXPR="{l.type EQ 'f'}">
<MvASSIGN NAME="g.dirpath" VALUE = "{'/var/www/mivadata' $ l.path $ '/'}">
<MvELSE>
<MvASSIGN NAME="g.getdir_error" VALUE = "Incorrect type passed to getdir.">
<MvFUNCRETURN VALUE = "{0}">
</MvIF>
</MvIF>
<MvCALL ACTION = "http://www.foo.net/cgi-bin/getdir.pl"
METHOD = "GET"
FIELDS = "enc,dirpath">
<MvIF EXPR="{callobjecttype EQ 'text'}">
<MvASSIGN NAME = "l.raw" VALUE = "{decodeentities(callvalue)}" >
<MvASSIGN NAME="l.lcount" VALUE = "{l.lcount + 1}">
<MvASSIGN NAME="g.dirlist" INDEX="{l.lcount}" VALUE = "{l.raw}">
</MvIF>
</MvCALL>
<MvIF EXPR="{s.mvcall_error}">
<MvEVAL EXPR="{s.mvcall_error}">
</MvIF>
<MvFUNCRETURN VALUE = "{1}">
<MvELSE>
<MvASSIGN NAME="g.getdir_error" VALUE = "{'The directory <code>' $ l.path $ '</code> does not exist.'}">
<MvFUNCRETURN VALUE = "{0}">
</MvIF>

</MvFUNCTION>


It is important to ensure that unauthorised people can't use the perl script to rummage around in your server - particularly as this function lets you read data directories.

You could probably remove the MD5 requirement - we were using it to make sure we were reading the correct file in the result parser. The parser function will depend on the directory listing format configured in your server OS.


Here's the perl script:

#!/usr/bin/perl

################################################## ###########################################
#Create a directory list and write it to a file
#
# change $dircommand to whatever command works on your server. This work well on UNIX
# change domain to your domain, and directory to and directory you choose.
#
# When run,
# This script will output to a file called "dd_getdir.dat", but you have to create it first!
#
################################################## ###########################################
use CGI;
use CGI::Carp(fatalsToBrowser);

$cgi = new CGI;

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

open INPUT, "</var/www/mivadata/dd_getdir.dat";
@lines = <INPUT>;
close INPUT;
$code = @lines[-1];

for $key ( $cgi->param() ) {
$input{$key} = $cgi->param($key);
}

if ($code == $input{enc}) {

$dircommand = "ls -l -Q $input{dirpath}";

open PIPE, "$dircommand|" || die("Command bad");
while(<PIPE>) {
print "$_<br>";
}
close(PIPE);
}
else { die("Not Authorised");}

exit;

donb
04-21-06, 05:39 AM
That was exactly what I needed.

Thanks