UserMane has asked for the wisdom of the Perl Monks concerning the following question:
Hello,
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
Re: Advice/help with beginner script (file sorting, execute command line command)
by boftx (Deacon) on May 21, 2014 at 20:23 UTC
|
A quick starting point for further research would be to read the docs for any of the File::* modules (both CORE and CPAN) that have an interesting title for you. That should entail a bit of reading, but you should also get a lot of ideas for what you can do. Given that you are on a Windows box, you almost have to use those modules anyway to achieve any sort of cross-platform compatibility.
It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
| [reply] [d/l] |
Re: Advice/help with beginner script (file sorting, execute command line command)
by Laurent_R (Canon) on May 21, 2014 at 21:25 UTC
|
Well, what efforts have you made so far to try to solve these easy tasks?
Googling question 1 with the following reasonable search query "perl find current path", I get this page: https://www.google.fr/search?q=perl+find+current+path&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:fr:official&client=firefox-a&channel=sb&gfe_rd=cr&ei=qQ19U7bGI4GB8QfxroHoBA. The very first link (Cwd) will tell you about the Cwd module and getcwd (get current working directory) function that should help you solve question 1. Everything is explained, but if you need help to use this module, please show what you have attempted and tell us where it fails to give you what you want.
For question 2, you could possibly use the glob function to retrieve a list of directory entries, use the -d file test operator to find out if it is a directory, and to use the glob function again on that sub-directory to find the entries there. Then use the -s file test operator to find the size of each one. Finding the largest one should be easy relatively with a little bit of thinking.
OK, I have given you a few clues, I hope they will help you.
There are some modules that can do part of the work for you, but, in your case, I would probably recommend that you try to do it fully by yourself: you are obviously a beginner, you really need to write programs and think the whole process by yourself.
Once again, if you show some attempted code, many monks on this site, including myself, will be more than happy to help you. But we need to see your code, not only to verify that you are really trying (although this is part of the expectation), but more importantly because we need to see your code to know at which level you stand and what you need to learn or where you need guidance.
Sorry if my post is demanding things from you, but learning how to develop programs is a real challenge and requires some real work. We can help you solve specific problems and point to possible mistakes, we can't do the work for you. I could easily write the solution for you (it might take me less time than what I spent writing this detailed answer), but you would most probably not learn anything useful from it. I hope you are appreciating that I am trying to help you the best way.
| [reply] [d/l] [select] |
|
Thank you for the helpful links. You are correct that reading tutorials and examples is a good place to start. This is work related and unfortunately it may take some time to get around to. I will try to post an example of what I've written when I come up with something. I will admit I was looking for a bit of a shortcut; in my search I've come across 3 line examples that will do something similar in Bash and PowerShell, and an expert could probably do this in a few minutes. Hopefully I'll get there.
| [reply] |
Re: Advice/help with beginner script (file sorting, execute command line command)
by GotToBTru (Prior) on May 21, 2014 at 20:24 UTC
|
I would suggest working thru a perl programming tutorial so that you can at least put the broadest structure to this; it is easier to help fill in details than to write the whole thing from scratch. Google "getting started with perl" - this is one I read which I thought good.
| [reply] |
Re: Advice/help with beginner script (file sorting, execute command line command)
by Discipulus (Canon) on May 22, 2014 at 07:33 UTC
|
| [reply] [d/l] |
Re: Advice/help with beginner script (file sorting, execute command line command)
by Sinistral (Monsignor) on May 21, 2014 at 20:40 UTC
|
As much as I love Perl, since you're on a Windows 7 box, I would highly recommend using Power Shell. All of the functionality you describe can be done with native methods in PowerShell, and arguably can be done faster than a Windows compiled Perl could. You've come to the right place if you really want to do it in Perl, but it might be worth considering
| [reply] |
|
Sorry, but you must be kidding. Sure you can do things in power shell, but Perl is so much better, more expressive, more powerful, faster and so on. I am using regularly Unix shell scripting to launch my Perl program because it is sometimes useful to use OS scripts to do a few things (such as defining environment variables), but I would never recommend using sh or csh or ksh to replace Perl.
| [reply] |
|
Thanks for the reply. Interesting you should say that, I was thinking the same thing. The program that I want to run is written in Java, accessed via the command line using a wrapper written in Perl. Using a PowerShell script to run a Perl wrapper for a program written in Java seems a bit convoluted and I thought I may as well try it in Perl, but I think you may be correct.
| [reply] |
Re: Advice/help with beginner script (file sorting, execute command line command)
by Cristoforo (Curate) on May 24, 2014 at 20:17 UTC
|
To answer some of your numbered questions
1. see Cwd
2. File::Find::Rule has found favor and can provide a good solution to finding files in the specified directories. Super Search is a good source for finding out more about File::Find::Rule as well as any other questions you might have.
To find the largest .txt file in each directory, you might want to see reduce from List::Util.
Update The largest could be found this way as well.
my @files = glob "*.txt";
my $largest = shift @files;
for my $file (@files) {
$largest = $file if -s $file > -s $largest;
}
3. see mkdir
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,
Because some filenames/directories can themselves have spaces, it might be better to separate them with a tab character or even separate them by the newline character.
To see how to create a new file, look at the documentation for open. | [reply] [d/l] |
|
|