firstbaseman83 has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to:

If I haven't made myself clear, just let me know

~firstbaseman83

Replies are listed 'Best First'.
Re: file i/o beyond my knowledge
by BrowserUk (Patriarch) on Jan 14, 2003 at 03:53 UTC

    Is there a way to:
    • prompt the user for a directory,

      Look at perlfunc:print to prompt the user and perlop under "i/o operators" to retrive his answer.

    • read the files from that directory,

      perlfunc:readdir or perlfunc:glob to obtain the list of files.

      Or some prefer to use File::Find which is a part of (the?)/my standard Perl distribution

    • create a sub-directory,

      perlfunc:mkdir to create the new directory.

    • and copy all the files into that sub directory?

      Module File::Copy (also standard) will take care of the copying for you.

    Come back with your code if you run into specific problems. Good luck.

    Examine what is said, not who speaks.

    The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

Re: file i/o beyond my knowledge
by theorbtwo (Prior) on Jan 14, 2003 at 03:38 UTC

    None of this is actualy complicated. It's all a matter of taking a bunch of simple things and putting them together.

    "Prompt the user for a directory": print, <>.

    "Read the files from that directory": glob('*'), or opendir(), readdir(), ...

    "Create a sub-directory": mkdir

    "copy all the files into that sub directory": `cp`, or open, etc, etc


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

Re: file i/o beyond my knowledge
by fredopalus (Friar) on Jan 14, 2003 at 04:09 UTC
    It would have probably been a good idea if you had shown some effort and showed us your attempt to accomplish this. Also, you should give us some info about the system you're working on.


    Here's some documentation:
    Next time follow these steps:
    1. Read Documentation
    2. Ask Question
      My apologies..
      I'm using Windows 2000, ActiveState Perl version 5.6.1 if you
      need to know that too.

      Here is my current coding:

      #!/usr/bin/perl # Import Modules # use File::Copy; use Image::Magick; use File::Find; # Prompt for Directory # print "Enter Directory: "; chomp($dir = <STDIN>); # Find Images # find(\&mogrify, $dir); # Resize Images # # Write to thumbnails directory # # With same filename extension # sub mogrify { $img = Image::Magick->new; $img->Read($_); # Test for Extensions # if ($_ =~ /\.(\w+)$/) { $ext2 = $1; } my $ext = $ext2; $img->Resize('geometry' => "120x120", 'filter' => "Cubic"); $img->Write("$_"."-thumb"."\.$ext2"); } # Make the directory of thumbnails # system("mkdir thumbnails"); # Move all thumbnails to the thumbnails directory # system("move *thumb*.*, thumbnails");

      Sorry if the indentation's bad....

      Is any of this the correct way to do it?

      Thanks,
      ~firstbaseman83
        and In reply to freopalus,
        I read the documentation for eveything
        I could think of but I couldn't find my exact
        answer.
        ~firstbaseman83
Re: file i/o beyond my knowledge
by waswas-fng (Curate) on Jan 14, 2003 at 03:34 UTC
    yep.

    If you would like to show us your try at it we could point out issues and resolutions.

    -Waswas
Re: file i/o beyond my knowledge
by Cody Pendant (Prior) on Jan 14, 2003 at 03:57 UTC
    I'm just wondering what happens if you really do copy every file from a directory, including the . and .. files? I don't want to be the one to try it...

    I forget the approved way to do this, but you're going to want to do something like:
    @list = grep {! /^\.+/} @list;
    on the list of files...
    --

    “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.” M-J D