Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

problems moving files

by smanicka (Scribe)
on Feb 09, 2009 at 20:39 UTC ( [id://742574]=perlquestion: print w/replies, xml ) Need Help??

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

Hello,

I am trying to write a code that would copy or move files to a new location.Here is the problem.I want the script to create the folders if they are not already present and I am not sure as to how to go about.I need them to be created on the fly and not hard coded as

$xyz= md c:\\dump\\new folder; system($xyz);
Any idea on how to do this? Thanks -Sandhya

Replies are listed 'Best First'.
Re: problems moving files
by CountZero (Bishop) on Feb 09, 2009 at 21:58 UTC
    A combination of Path::Class and File::Copy will do the trick.

    use Path::Class; my @dirs = qw/path to my directory/; my $path = my $dir = Path::Class::Dir->new(@dirs); $path->mkpath; #creates the directories as necessary
    Since $path stringifies to the path, it can be used as an argument in the move function of File::Copy:
    use File::Copy; move('file_to_move', $path);
    By using Path::Class and File::Copy you do not have to worry about what kind of directory separator to use and your script will be truly portable.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: problems moving files
by roboticus (Chancellor) on Feb 09, 2009 at 21:12 UTC
    smanicka:

    Rather than shell out to the operating system to do it, you could just use the function mkdir(), like:

    mkdir("C:\\dump\\new folder") or die $!;

    In general, when you find yourself getting ready to ask the operating system to do something for you, you may want to browse through "perldoc perlfunc" and find out what's already there for you. (CPAN, of couse, is another great place to find tasks that have already been done for you.)

    ...roboticus
Re: problems moving files
by MidLifeXis (Monsignor) on Feb 09, 2009 at 20:44 UTC
Re: problems moving files
by poolpi (Hermit) on Feb 10, 2009 at 14:13 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://742574]
Approved by sweetblood
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-04-19 05:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found