LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-16-2004, 12:20 AM   #1
supreme_command
LQ Newbie
 
Registered: Feb 2004
Distribution: Mandrake
Posts: 7

Rep: Reputation: 0
Perl Scripting on Filename lengths


Hey

I am trying to write a script that allows me to recursivley search through a directory to find the longest file name. I am trying to do this using perl.

Problem i have it that it is taking the directory and counting it as filename length

Code:
#!/usr/bin/perl -w

#program to print longest file name in a directory - resursive


if (! @ARGV)	  {
   print "Must have a directroy argument\n";
   exit;
}

open(FIND, "find . -print |") || die "Couldn't run find: $!\n";


$length = 0;


FILE:
while ($file = <FIND>)	{
	chop $file;
	if (! -d $file) {
	   print "$file\n";
	   $newlength = `echo $file | wc -c`;
	   if ($newlength > $length)	 {
	      $length = $newlength;
	      $name = $file;
	   }
	   next FILE;
	}
	
}

print "Longest File was $name\n";
Anyone got any ideas - even it i could just split the a line

like ./test/res/file1 so i could have access to the last part after the final /

but it would have to be generic for if it was only ./test/file2

Anyone with any idea please help

Thanks

Last edited by supreme_command; 05-16-2004 at 12:21 AM.
 
Old 05-16-2004, 02:16 AM   #2
diogenese
LQ Newbie
 
Registered: Apr 2004
Location: Good old New Jersey
Distribution: SuSE 9.0
Posts: 3

Rep: Reputation: 0
With perl, there's always more than one way

to skin a cat:

Code:
use Cwd 'chdir';

# identify target directory
if ($#ARGV < 0 or $ARGV[0] eq ".") {
  # use current directory
  $targetDir = $ENV{PWD};
}
else {
  if (! -d $ARGV[0]) {
    # specified directory doesn't exist
    print "    $ARGV[0] is not a valid directory.\n\n";
    exit(1);
  }
  else {
    # capture target directory
    chdir $ARGV[0];
    $targetDir = $ENV{PWD};
  }
}

if (opendir (DIR, "$targetDir") == 0) {
  print "    Can't open directory $targetDir\n\n";
  exit(2);
}
else {
  # list everything in the directory
  @dir_items = readdir DIR;
  closedir DIR;

  $maxLen = 0;
  foreach $object(@dir_items) {
    # evaluate files only
    if (-f $object) {
      if (length($object) > $maxLen) {
        $maxLen = length($object);
        $maxFile = $object;
      }
    }
  }
}

if ($maxLen > 0) {
  print "Longest file is $maxFile, at $maxLen bytes\n";
}
else {
  print "No files were found in the specified directory.\n";
}

exit(0);
 
Old 05-16-2004, 03:06 AM   #3
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to seperate filename from path in perl mengle Programming 2 08-28-2005 01:35 AM
filename truncation Perl belmer2 Programming 1 04-24-2005 01:31 PM
question about perl scripting feetyouwell Linux - Software 2 02-13-2004 03:11 PM
Perl Parsing filename The Jesus Programming 4 10-03-2003 03:21 PM
Perl scripting Rameriez Programming 4 02-03-2003 01:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration