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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort by Date

Status
Not open for further replies.

M626

Programmer
Mar 13, 2002
299
I am using the following to output a file listing the contents of a directory. Is there anyway i can modify this to output by date created or date modifed?

#!perl

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

# Configuration:

$internal_path = "r:/";
$list_directories = "true";
$list_files = "true";
$list_bytes_total = "true";

@Directory_Parent = $internal_path;

while (@Directory_Parent)
{
$directory = shift (@Directory_Parent);
opendir(DIR, $directory) || next;
while (defined($child = readdir(DIR)))
{
if ((-d "$directory/$child") && ($child ne ".") && ($child ne ".."))
{
push(@Directory_Parent, "$child");

if ($list_directories eq "true")
{ push (@Directory_List, "$child"); }
{ push (@File_List, "<br>"); }
}

if ((-f "$directory/$child") && ($list_files eq "true"))
{ push (@File_List, "$child"); }
{ push (@File_List, "<br>"); }

if ((-f "$directory/$child") && ($list_bytes_total eq "true"))
{
$filename = "$directory/$child";
$bytes = $bytes + (-s $filename);
}
}

closedir(DIR);

if ($bytes)
{
push (@Bytes, " $directory : $bytes");
$bytes = 0;
}
}

open(OUTFILE, "> C:/LIST.txt") or die "can't open outfile : $!\n";
# @Directory_List = sort { $a cmp $b } (@Directory_List);

# @File_List = sort { $a cmp $b } (@File_List);

print "Parent Directory Of:\n $internal_path\n\n";

$" = "\n";

if (@Directory_List)
{ print "Child Directories Found:\n @Directory_List\n\n"; }

if (@File_List)
{ print "Files Found:\n@File_List\n\n"; }
{ print OUTFILE "FileLIST:\n@File_List\n\n"; }


$" = " ";

exit;
 
use the stat() function or a file test operator

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I would probably want to use 'ctime' in the stat function but how would i sort them. I would probably need to obtain that info on all the files first...??
 
I probably want 'mtime' not 'ctime'. I am looking for some examples on how i can use this.
 
What are you trying to do with your script? It appears to be doing a rather simple thing the hard (and probably slow) way. You can use File::Find to list all the folders and files in a directory and the preprocess function to sort them however you want. But I am not quite sure what the goal of your script is.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
The goal is to look at a directory and write the contents to a file sorted by date modified or date created.

 
if ur in a *nix env; can u user `ls -ltr` or `ls -lt`
 
Or Dir /od on win.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
uh-oh...here come the shell commands.....

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top