473,225 Members | 1,220 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,225 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 2391
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

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.