473,699 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

13 New Member
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 2410
eWish
971 Recognized Expert Contributor
Would you please show us the code that you have tried? Thank You!
Oct 11 '07 #2
bhumikas
13 New Member
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 Recognized Expert Specialist
use the File::Find module:

perldoc: File::Find
Oct 12 '07 #4
eWish
971 Recognized Expert Contributor
I agree with Kevin. I was going to suggest the same module. I just wanted to see some effort first.
Oct 12 '07 #5
bhumikas
13 New Member
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 Recognized Expert Specialist
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
bhumikas
13 New Member
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,d irectory and the folder in the command line)

I need some help to implement this logic...

Thanks
Oct 12 '07 #8
eWish
971 Recognized Expert Contributor
The File::Find module that Kevin suggested will be so much easier.
Oct 13 '07 #9
anjigorantla
8 New Member
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
2135
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. That is bothersome if I want to install the script on other machines. The same for pkwares zip. At least I cannot seem to find a free version of it. gzip seems able to do the trick, but I need to install Cygwin, that's
1
4111
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
4809
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 philosophy but that is the way things need to work for now here. The problem I'm having is that it appears that the browsers (IE, mozilla and netscape) are sometimes getting confused about wether the javascript code is running. By this I mean when I...
5
655
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 and get rid of diretories with rmdir. This works well, but it seems that there should be a more efficient way. Here are my function definitions: def remove_files(target_dir): # This attempts to remove _all_ files from a directory. # Use with...
3
1919
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 - the path to the corresponding file and call submit with the script. Thank you.
1
6329
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 of directories / sub-directories). I am looking for a script that will recursively extract a single file from each of these archives e.g. the file INSTALL, for the extracted file to be moved to a different location and renamed to the name of
2
1643
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 betweeen HAVING and WHERE ? Never used HAVing clauuse or seen it. 4.What is pre-emptive multitasking? 5.What is object serialization? 6.What is the difference between web.config and machine.config? Never
1
2974
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 recursion. Can anybody findout where I am going wrong? Here is the code. #!/usr/bin/perl
3
1953
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 would like to do is recursively backup the specified directories (which copytree will do), but be able to specify exclusion directories (which copytree does not appear to allow you to do). My initial thoughts were I'll probably have to use...
0
8697
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9184
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9042
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8929
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8891
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7759
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5878
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3061
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2013
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.