CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
variables for moving files

 



stevieger
Novice

Nov 5, 2012, 6:45 PM

Post #1 of 9 (2407 views)
variables for moving files Can't Post

I'm trying to code a simple task of moving a file via move($oldlocation, $newlocation).

Why is it that if I try to read in a text file for those variables my file does not move, but if I explicitly define $oldlocation and $newlocation to the EXACT same thing that is contained in the text file it does work? Why does it matter where it gets the information from in order for it to work??

I'm hoping there is a simple explanation as to why this is this way and my goal is to be able to use the text file contents to direct file transfers. Thanks in advance.


BillKSmith
Veteran

Nov 5, 2012, 8:36 PM

Post #2 of 9 (2402 views)
Re: [stevieger] variables for moving files [In reply to] Can't Post

I suspect that you are making a mistake in your use of quote marks, but without your code I can only guess.

The command move is not a perl function. You probably should be using either the function rename or the module File::Copy.
Good Luck,
Bill

(This post was edited by BillKSmith on Nov 5, 2012, 8:39 PM)


rovf
Veteran

Nov 6, 2012, 1:22 AM

Post #3 of 9 (2389 views)
Re: [stevieger] variables for moving files [In reply to] Can't Post


Quote
I'm trying to code a simple task of moving a file via move($oldlocation, $newlocation).


I guess you use File::Copy::move ?


Quote
if I try to read in a text file for those variables my file does not move


I'm pretty sure the bug is in line 42 of your code.

A wild guess: Maybe you forgot to chomp the strings you have read in? Use Data::Dumper to inspect the content of the variables before moving.


stevieger
Novice

Nov 6, 2012, 5:57 AM

Post #4 of 9 (2366 views)
Re: [stevieger] variables for moving files [In reply to] Can't Post

here's my code:

Code
use File::Copy; 

open (DIRECTIONS, 'data.txt');

@directions = <DIRECTIONS>;
($file, $dirTo) = @directions;
chomp $file;
chomp $dirTo;
close (DIRECTIONS);

print "Taking $file and putting in $dirTo";

move($file, $dirTo);


Where the data.txt file contains:
line 1:"update/blah.txt"
line2:"new"

the code is in folder Perl64 which contains a folder named update and a folder named new and the update folder contains a text file blah.txt which is my target file to move.


FishMonger
Veteran / Moderator

Nov 6, 2012, 7:08 AM

Post #5 of 9 (2354 views)
Re: [stevieger] variables for moving files [In reply to] Can't Post

Your code is telling me that you explicitly told perl not to tell you why it was failing.

Start by adding these 2 prgmas to your script.

Code
use strict; 
use warnings;

They should be in every Perl script you write.

The strict pragma will force you to declare your vars, which is done with the 'my' keyword.

You should ALWAYS check the return code of an open call to verify that it was successful and take action if it wasn't. You should use a lexical var for the filehandle instead of the bareword and you should use the 3 arg form of open.

Since you haven't shown us the contents of data.txt, I must ask: are the source and destination on separate lines and are they the only 2 lines in the file? If the answer is no to either of those questions, then that would be the source of the problem.


Code
#!/usr/bin/perl 

use strict;
use warnings;
use File::Copy;

open my $fh, '<', 'data.txt' or die "failed to open 'data.txt' $!";
chomp (my ($file, $dirTo) = <$fh>);
close $fh;

print "Taking $file and putting in $dirTo";
move($file, $dirTo) or die "failed to move '$file' to '$dirTo' $!";



BillKSmith
Veteran

Nov 6, 2012, 7:09 AM

Post #6 of 9 (2352 views)
Re: [stevieger] variables for moving files [In reply to] Can't Post

I do not see any problem. Why not ask perl for more info? Always use strict and warnings. Check the status of open and move.

Code
use strict; 
use warnings;
use File::Copy;
open (my $DIRECTIONS, '<', 'data.txt') or die "open failed: $!";
chomp( my @directions = <$DIRECTIONS> );
close ($DIRECTIONS);
my ($file, $dirTo) = @directions;
print "Taking $file and putting in $dirTo\n";
move($file, $dirTo) or die "Move failed: $!";
print "Move succeeded\n";

Good Luck,
Bill


FishMonger
Veteran / Moderator

Nov 6, 2012, 7:11 AM

Post #7 of 9 (2352 views)
Re: [stevieger] variables for moving files [In reply to] Can't Post


Quote
Where the data.txt file contains:
line 1:"update/blah.txt"
line2:"new"

I must have skipped over that part of your post.

If the quotes are in the file as you've shown here, then that's the problem. You'll need to strip them off.


stevieger
Novice

Nov 6, 2012, 7:22 AM

Post #8 of 9 (2350 views)
Re: [stevieger] variables for moving files [In reply to] Can't Post

Thanks for the help I'm very new to Perl and interpreted programming language.

My issue was definitely my open argument and not declaring my variables with 'my' as well as the data.txt file contained quotation marks. Several mistakes but I understand better now, thanks again.


rovf
Veteran

Nov 7, 2012, 12:10 AM

Post #9 of 9 (2306 views)
Re: [stevieger] variables for moving files [In reply to] Can't Post

(1) Could it be that the line in data.txt, which denotes the destination directory, has trailing spaces?

(2) What does $! say after the move()?

(3) I would explicitly check that you have proper access rights to source and destination, because even if you get something on $!, you don't know whether the error refers to reading or to writing.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives