Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List files and folders in a different order??

Status
Not open for further replies.

scriggs

IS-IT--Management
Jun 1, 2004
286
GB
I have a script which runs through the directories in a folder (on windows machine) and outputs a link to all the files in the directories.

My directory structure is normally something like:

2002
2003
2004
2005
Corres
Misc

What I need to do is output the directories in a different ORDER, so that it is reverse numeric order, then alpha. So that the script will output:

2005
2004
2003
2003
Corres
Misc

A further enhancment might be to do the last two years, then alpha, then rest:

2005
2004
Corres
Misc
2003
2002

Any help would be really appreciated.

[script]
function CreateLinkToOfficeFile($dir)
{
chdir($dir);
if(!($dp = opendir($dir))) die ("Cannot open $dir");
while($file = readdir($dp))
{
if(is_dir($file))
{
if($file != '.' && $file != '..')
{
echo "<h4>$file</h4><br>\n";
echo "\t<ul>\n";
CreateLinkToOfficeFile("$dir/$file");
chdir($dir);
echo "\t</ul>\n";
}
}
else
{
if($file != '.' && $file != '..')
{
$ext = strtolower(str_replace('.','',strstr($file, '.')));
if(($ext=="doc"||$ext=="dot"||$ext=="xls"||$ext=="xlt"||$ext=="pdf"||$ext=="csv"||$ext=="tif"||$ext=="tiff") && preg_match("/[~$]/", $file) != true)
{
echo "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"> <tr valign=\"middle\">\n";
echo "<td width=\"5%\"><img src=\"images/$ext.gif\" alt=\"$ext\" width=25 height=25></td><td width=\"45%\"><a href=\"$dir/$file\" target=\"_blank\">$file</a></td>\n";
echo "<td width=\"50%\"\n>";
echo "</td>\n";
echo "</tr>\n";
echo " </table>\n";
}
}
}
}
closedir($dp);
}
[/script]
 
I would do this (using your code):
Code:
CreateLinkToOfficeFile("2005");
CreateLinkToOfficeFile("2004");
CreateLinkToOfficeFile("Corres");
CreateLinkToOfficeFile("Misc");
CreateLinkToOfficeFile("2003");
CreateLinkToOfficeFile("2002");
That way you maintain full control over any arbitrary order you wish to supply the top level of information.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Thanks babyjeffy

Simple solution which is always a good idea.

The code is recursive as the directories are different in each case (and some have subfolders inside of them). The 6 folders I listed are the normal, but might have years back to 1990 or another alpha folder called 'Tax' or 'Legal', etc.

I need the script to be automated on every folder, but also sort them on the fly.
 
Then you need to load them into a server-side object (an array would do it) and then come up with some kind of programatic sort criteria to use so that the end result is the "sort" you want.

You need to be specific about the sort order you want to use - you need to come up with a rule (series of rules) that will give you your specific sort order regardless of the dataset.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Thanks Jeff

I have a simple rule which I am happy with - numeric backwards, then alpha forward.

How would I load them into an array and then use the sort?

Thanks
 
taking Jeff's suggestion:

let's assume you have an array called $directories and that the name of each directory was in the array.

then i'd suggest some code like the following
Code:
<?
$directories = array("2005",  "alpha", "beta","2003");
$numeric_directories = array();
$alpha_directories = array();

foreach ($directories as $directory):
	$first = ord(substr($directory,0,1));
	if ($first < 58 && $first > 47):
		$numeric_directories[] = $directory;
	else:
		$alpha_directories[] = $directory;
	endif;
endforeach;
rsort ($numeric_directories);
sort ($alpha_directories);
$directories = array_merge($numeric_directories, $alpha_directories);
print_r($directories);
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top