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

Output files to text document

Status
Not open for further replies.

vb89

MIS
Aug 21, 2008
47
US
What I'm trying to do is to create a program that reads through a certain directory and outputs the location of each file(both in the directory and subdirectorys) into a text file. I am completely new to Perl and under a time restriction so any help would be greatly appreciated...thanks in advance
 
What have you tried so far? You could look into File::Find.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Code:
use strict;
MAIN:
{

   open(STDERR, ">&STDOUT");


   chdir $ARGV[0] || die "\ncan't process directory $ARGV[0]\n" ;


   open (fileList, '>FILE_LIST.XML') || die "Can't open FILE_LIST.XML"; 
    
   printf fileList  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> \n <file-list>";
   printf fileList "\n";

   foreach(glob('*.*'))
   { 

      if ($_ =~ /FILE_LIST/)  {next;}

      my($mtime) = (stat($_))[9];
      my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime ($mtime + 3600);
      printf fileList "<file name=\"$_\" year=\"" . ($year + 1900) . 
      "\" month=\"" . ($mon + 1) . 
      "\" date=\"" . $mday .
      "\" hour=\"" . $hour .
      "\" minute=\"" . $min .
      "\" second=\"" . $sec .
      "\" timezone=\"Eastern\"/>"; 
      printf fileList "\n";
   }
   print fileList "</file-list>";
   close (fileList);

}

How does this look? and how would i get it to read subdirectories?
 
use File::Find

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
What that exactly mean ? (sorry im new)
 
ok, let me a little bit more specific: I have an excel spread sheet wich has a column where I want the location of the files in the directory/subdirectory to be outputed to. So anyway to adjust this code to do that, would be great...thank you!!
 
I create the following script:

Code:
use strict;
use File::Find;

sub eachFile {
  
 my $filename = $_;
  
 my $fullpath = $File::Find::name;
 
 #remember that File::Find changes your CWD, 
 
 #so you can call open with just $_


  if (-e $filename) { 
   
  print "$filename exists!"
      
     . " The full name is $fullpath\n"; 

  }
}

find (\&eachFile, "mydir/");

Is this correct? Additionally I wanted the script to run on an hourly basis to reupdate...how is this possible?
 
Did you run the script? To run it hourly you use a crontab (Unix/Linux) or a Task (Windows). Neither of which has anything to do with perl.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks for all the help so far KevinADC...one last question how do i edit the code above so that it outputs the data to an xml file called "File_List" located in C:\Perl_Test

Thanks again for everything
 
Try this:

Code:
use strict;
use File::Find;

open (OUT ">", 'C:/Perl_Test/File_List.xml') or die "$!";
find (\&eachFile, "mydir/");
close OUT;
sub eachFile {
   my $filename = $_;
   my $fullpath = $File::Find::name;
   if (-e $filename) {
      print OUT "$filename exists! The full name is $fullpath\n";
   }
}

Note: forward slashes work in Windows directory paths and are safer to use in perl code than backslashes.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
When I run the code it throws the following error:
Missing comma after first argument to open function...missing near 'C:/Perl_Test/File_List.xml
 
sorry, change this line to add the comma after OUT:

Code:
open (OUT, ">", 'C:/Perl_Test/File_List.xml') or die "$!";

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

Part and Inventory Search

Sponsor

Back
Top