CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
New Perl user, advice/help with beginner script (file sorting, execute command line command)


New User

May 19, 2014, 3:49 PM

Post #1 of 2 (3124 views)
New Perl user, advice/help with beginner script (file sorting, execute command line command) Can't Post


This will be my first time using Perl and I am hoping those with more experience may be able to help with some advice on which commands to use.

I would like to write a small script that will:

1) Find the directory that the script is being run from and save the path, for example $ScriptDir may be C:\PerlScripts

2) Go through each of the subdirectories, one level, and save the full path of the largest sized .txt file in each subdirectory to a new/appended string for example $FilePaths.

To clarify, say that there are 3 subdirectories in the directory that the script is in. Each of the three folders contains 5 .txt files. I would like to end up with a single string containing 3 full path names separated by spaces, the largest single .txt file from each subdirectory. For example, $FilePaths would be "C:\PerlScripts\SubDir1\File1.txt C:\PerlScripts\SubDir2\File2.txt C:\PerlScripts\SubDir3\File3.txt"

3) Create a new subdirectory under the script's path, for example $ScriptDir\NewFolder

4) Execute a command line command (this will be in Windows 7 so similar to a cmd.exe command) using $ScriptDir\NewFolder, for example:

MyProgram.exe --options $FilePaths $ScriptDir\NewFolder

Where $FilePaths is the files to run the MyProgram.exe on, and $ScriptDir\NewFolder is where to save the output.

If someone could please help with directing me to which functions to use, particularly for sorting files by size and obtaining a list of the largest file in each subdirectory as a string separated by spaces, I would be tremendously appreciative. Please let me know if there is any more info I can provide.

Thank you

(This post was edited by UserMane on May 19, 2014, 3:51 PM)

Chris Charley

May 19, 2014, 7:22 PM

Post #2 of 2 (3051 views)
Re: [UserMane] New Perl user, advice/help with beginner script (file sorting, execute command line command) [In reply to] Can't Post

I gave an answer to a similar question that asked for the newest file in a directory;sb=post_latest_reply;so=ASC;forum_view=forum_view_collapsed;guest=9127415

It could be adapted to your problem like this:

use strict;
use warnings;
use List::Util 'reduce';

my $path = './CSV'; # or whatever directory you're looking for

my $largest = reduce {-s $a > -s $b ? $a : $b} glob "$path/*.txt";

Update: If you are still following this thread, here is a complete program that does what you want.

use strict;
use warnings;
use Cwd;
use List::Util 'reduce';
use File::Find::Rule;

my @dirs = find( directory => mindepth => 1 => maxdepth =>1, in => cwd);

my @largest;
for my $dir (@dirs) {
my @files = find( file => name => qr/\.txt$/i => maxdepth => 1, in => $dir );
if (@files) {
push @largest, reduce {-s $a > -s $b ? $a : $b} @files;

my $new_dir = "newdir";
mkdir $new_dir unless -e $new_dir;

open my $fh, ">", "$new_dir/largest.txt" or die $!;
print $fh "$_\n" for @largest;
close $fh or die $!;

(This post was edited by Chris Charley on Jun 1, 2014, 12:35 PM)


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives