Find the answer to your Linux question:
Results 1 to 3 of 3
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 ...
  1. #1
    Just Joined!
    Join Date
    Jul 2007
    Posts
    4

    perl script which goes through 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..
    Code:
    chdir($FOLDER);
    opendir(dir, $FOLDER) or die "Can't open $FOLDER directory: $!\n";
      @directory = sort readdir(dir) or die "Unable to read current dir:$!\n";
      closedir(dir);
     
    foreach $directory (@directory)
    {
    ....
    ....
    opendir (dir,$directory) or die "Can't open the current directory: $!\n";
    @sub_directory = sort readdir(dir) or die "Unable to read current dir:$!\n";
    closedir(dir);
    chdir($directory)
     
    foreach $sub_directory (@sub_directory)
    {
    ....
    opendir(dir, $sub_directory) or die "Can't open the current directory: $!\n";
          @filename = sort readdir(dir) or die "Unable to read current dir:$!\n";
          closedir(dir);
    chdir($sub_directory) or die "Unable to enter dir $sub_directory:$!\n";
    ...
    ....
    foreach $filename (@filename)
    {
    ...
    ...
    chdir($filename) or die "unable..."
     
    }
     
    }
    }

    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 and sample can help me a lot

    Thanks

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    There is often a gap between a code designer's mental image of what should happen and a clean specification of what should happen. It's probably possible for a reader to figure out your description of what should happen, but it wasn't easy for me. The greater the difficulty of figuring that out, the sloppier your code will be, and the less maintainable it will be.

    If there is no pressure deadline, one useful trick is to lay your description aside for a week or two and then come back to this thread and reread what you wrote. You will then be able more easily to read it from the viewpoint of a stranger, and see exactly why it's difficult to read. Removing those difficulties from the description will help you design cleaner code.

    If there is pressure deadline, then write a description of a simple version of the program that doesn't do everything, but just does some simple thing. Then code that and make it work. Then rewrite that simple description so it's slightly more complex. Then add that feature to your program. And so on. And come back here if you have questions with specific problems.

    Of course, we can't design or code this for you, but this will help you get started.

  3. #3
    Just Joined!
    Join Date
    Jul 2007
    Posts
    4
    Thanks for your advice

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...