473,320 Members | 1,695 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

need help for a script which goes through the directories recursively...

Hi all,
I need a help in perl script.The basic idea is,it must have command line arguments for the user flexibility.the files are in the format as shown below.

MainFolder
Directory
Sub_directory
files
Sub_directory
files
Directory
Sub_Directory
files
Sub_directory
files
Directory
Sub_Directory
files
Sub_directory
files
and so on....

I need to do some manipulation on the files depending on the commandline arguments.
1.If i just give the Folder name,it should go recursively till the end of all files in all the sub_directories.
2.If i give the directory name,it should process it and remaining directories in the folder.
2.If I give a sub_directory name,I should also mention the Directory name(mandatory) then it should process all the files in that subdirectory and keep continuing till it processes the entire directories.

I need more help in processing of the command line arguments.
i.e, how to declare and use them etc..

Thanks in advance..
Oct 11 '07 #1
9 2394
eWish
971 Expert 512MB
Would you please show us the code that you have tried? Thank You!
Oct 11 '07 #2
I need some advice on this piece also.

In this way I can read complete folder and its contents...
Expand|Select|Wrap|Line Numbers
  1. opendir(dir, $FOLDER) or die "Can't open $FOLDER directory: $!\n";
  2.   @directory = sort readdir(dir) or die "Unable to read current dir:$!\n";
  3.   closedir(dir);
what I need is if I give the directory name,it has to start from that directory and continue till end....and also for the subdirectories....

Plzz can someone guide me
Oct 11 '07 #3
KevinADC
4,059 Expert 2GB
use the File::Find module:

perldoc: File::Find
Oct 12 '07 #4
eWish
971 Expert 512MB
I agree with Kevin. I was going to suggest the same module. I just wanted to see some effort first.
Oct 12 '07 #5
I need advice on how to have the conditions for command line arguments.
The code which I placed in my above post goes for the entire folder.
What I want to do is depending on the commandline it has to go either for entire folder or from the directory I pass or from the sub-directory of a directory I pass.
How to use these condns for same piece of coding....
Oct 12 '07 #6
KevinADC
4,059 Expert 2GB
I need advice on how to have the conditions for command line arguments.
The code which I placed in my above post goes for the entire folder.
What I want to do is depending on the commandline it has to go either for entire folder or from the directory I pass or from the sub-directory of a directory I pass.
How to use these condns for same piece of coding....
You have not posted any code. Did you read the File::Find documentation I posted a link to?
Oct 12 '07 #7
As shown below it starts for a specific folder and keeps on going...

Expand|Select|Wrap|Line Numbers
  1. chdir($FOLDER);
  2. opendir(dir, $FOLDER) or die "Can't open $FOLDER directory: $!\n";
  3.   @directory = sort readdir(dir) or die "Unable to read current dir:$!\n";
  4.   closedir(dir);
  5.  
  6. foreach $directory (@directory)
  7. {
  8. ....
  9. ....
  10. opendir (dir,$directory) or die "Can't open the current directory: $!\n";
  11. @sub_directory = sort readdir(dir) or die "Unable to read current dir:$!\n";
  12. closedir(dir);
  13. chdir($directory)
  14.  
  15. foreach $sub_directory (@sub_directory)
  16. {
  17. ....
  18. opendir(dir, $sub_directory) or die "Can't open the current directory: $!\n";
  19.       @filename = sort readdir(dir) or die "Unable to read current dir:$!\n";
  20.       closedir(dir);
  21. chdir($sub_directory) or die "Unable to enter dir $sub_directory:$!\n";
  22. ...
  23. ....
  24. foreach $filename (@filename)
  25. {
  26. ...
  27. ...
  28. chdir($filename) or die "unable..."
  29.  
  30. }
  31.  
  32. }
  33. }
I might be wrong someway above,but its just a sample I typed in...

The thing where I need a suggestion is
1.If I start with the main folder,its fine and gonna work good.
2.If i start with a directory(also be mentioning the folder name in the command line),it has to start from that directory and has to keep going for that and also the remaining directories in that folder.How to set a condn for that,so that it can start from there and go recursively...
if($directory),I mean if I entered a value for directory...How to start the for loops and so on...
3.Its a condn more than the above as I need to start from a sub_directory of a directory and has to go recursively till the end of all directories in the folder.
(I will be passing the sub_directory,directory and the folder in the command line)

I need some help to implement this logic...

Thanks
Oct 12 '07 #8
eWish
971 Expert 512MB
The File::Find module that Kevin suggested will be so much easier.
Oct 13 '07 #9
As like mentioned above will work perfectly.

use File::Find traversing module.
it will definitely help u.
Oct 13 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Max M | last post by:
I guess that the best approach is calling a shell tool with something like os.popen(). But I cannot seem to find any free tools. Winzip has a command line option, but for registered users only....
1
by: Trinity | last post by:
Hi, I want to delete a particular file in all the sub-directories. I am using the SHFileOperation() API. But it is not deleting the files recursively. Any sample code is appreciated. TIA
4
by: Derek | last post by:
Hi, I've built a rather large CGI that dumps a lot of data and a fairly complex javascript app out to the client's browser. Granted this may be poor style according to someone web design...
5
by: rbt | last post by:
What is the most efficient way to recursively remove files and directories? Currently, I'm using os.walk() to unlink any files present, then I call os.walk() again with the topdown=False option...
3
by: Kamen TOMOV | last post by:
Hi, Is uploading recursively directories to a web server possible with JavaScript? I mean is it possible read a directory recursively and dynamically construct <input type="file"> with value...
1
by: Peter Thorne | last post by:
I am a perl newbie who is trying to write a script to automate a task. I have a large collection of compressed archives (mostly .tar.gz, tar.bz2, tar.Z, .tgz etc). This are stored in a number...
2
by: weird0 | last post by:
Hi! I need answers to these questions that were given to me in a job test. 1. What is IDisposable in .NET? 2.What is the base class of all the controls in .NET? 3.What is the difference...
1
by: ArchanaAshok | last post by:
Hi All, I have done a script to compare 2 similar directories containing equal number of files, and to compare the content of those files in the directories. But there is some problem in the...
3
by: misceverything | last post by:
I am writing a script that will backup specified folders from one hard drive to another (for example, backup source "C:\DATA", destination "D: \Backup"), and was thinking of using shutil. What I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.