newbie trying to solve file copying problem 
Author Message
 newbie trying to solve file copying problem

Quote:

> OK. I feel really stupid, this should be simple.
> I get a bunch of files dumped into a directory, all belonging to different
> users. The filename contains the name of the user, ie. xxxxUSERNAMExxxx.txt
> I need to figure out a way to copy the files into the directory owned by
> the user. Seems to me that this should be simple, but I can't figure out a
> simple way to do it.

> I don't know any languages, but I figured perl was the way to go. I started
> working on a script, I'm stuck on the file::copy function

> use File::Copy;
> move('filename','destination');
> and this works.

> However, if I do this

> use File::Copy;
> move($filename,$destination)

;-----------------------------^

Quote:
> where the strings are valid, it doesn't work. I've tried altering the above
> to no avail, and I've scoured the web and read through several books
> looking for a way to do this. Obviously, I'm missing something. Can anyone
> point me in the right direction?

...
"Doesn't work" isn't very descriptive.  Do you get an error message?  If
so, what does it say?  If not, exactly why do you think it doesn't work?

My ESP says to make sure your filename variables don't have extra
characters in them which aren't printable, like newlines on the end.
Perhaps you forgot to use the chomp function on data read from a file?
--
Bob Walton



Sat, 21 Aug 2004 04:48:34 GMT  
 newbie trying to solve file copying problem

Quote:


>> OK. I feel really stupid, this should be simple.
>> I get a bunch of files dumped into a directory, all belonging to
>> different users. The filename contains the name of the user, ie.
>> xxxxUSERNAMExxxx.txt I need to figure out a way to copy the files into
>> the directory owned by the user. Seems to me that this should be simple,
>> but I can't figure out a simple way to do it.

>> I don't know any languages, but I figured perl was the way to go. I
>> started working on a script, I'm stuck on the file::copy function

>> use File::Copy;
>> move('filename','destination');
>> and this works.

>> However, if I do this

>> use File::Copy;
>> move($filename,$destination)
> ;-----------------------------^

>> where the strings are valid, it doesn't work. I've tried altering the
>> above to no avail, and I've scoured the web and read through several
>> books looking for a way to do this. Obviously, I'm missing something. Can
>> anyone point me in the right direction?
> ...
> "Doesn't work" isn't very descriptive.  Do you get an error message?  If
> so, what does it say?  If not, exactly why do you think it doesn't work?

> My ESP says to make sure your filename variables don't have extra
> characters in them which aren't printable, like newlines on the end.
> Perhaps you forgot to use the chomp function on data read from a file?

Yes, I suppose an error message would help...

Here's the output of the script attached to the first posting.
<OUTPUT>
This is the list of files to be moved:
AAAA-2000-13Feb2002102824480.XML
AAAA-2000-13Feb2002102824530.XML
AAAA-2000-13Feb2002102824590.XML
AAAA-2001-13Feb2002102824480.XML
AAAA-2002-13Feb2002102824530.XML
AAAC-2003-20Dec2001144557220.XML

The current working directory is:/home/humanfly/Longevity/perlscripts

The value of the scalar placed at location 0
is:AAAA-2000-13Feb2002102824480.XML

The filename we are extracting the user id from is:
AAAC-2003-20Dec2001144557220.XML
The user id is: 2003
Copy the file AAAC-2003-20Dec2001144557220.XMLto the directory owned by
user 2003
Attempt to copy AAAC-2003-20Dec2001144557220.XML failed at ./longevity.pl
line 48, <XMLFILELIST> line 6.
<END OUTPUT>

Thanks for the advice, I didn't use the chomp function, and I did read from
a file. <GOES BACK TO THE {*filter*}Y BOOKS...>



Sat, 21 Aug 2004 06:15:01 GMT  
 newbie trying to solve file copying problem

Quote:



[snipped lots]

For starters ... ALWAYS have the following lines present:

use strict;
use warnings;

Quite often these will point out real and potential problems in your code!

Quote:
> >> use File::Copy;
> >> move($filename,$destination)

This is NOT the line as given in your attachment .... your longevity.pl has
this line:

move('$filename','$destination') or die "Attempt to copy ",$filename,"
failed";

Single quoted strings will NOT have variable substitution performed so this
call will actually attempt to move the literal file $filename (which will
not exist) to $destination (literally as well).

The line should be

move($filename,$destination) or die "Attempt to copy ",$filename," failed";

Furthermore, if move does fail, $! will contain the error message produced
by the system ... so ...

move($filename,$destination) or die "Attempt to copy ",$filename," failed
because: $!";

Quote:
> Attempt to copy AAAC-2003-20Dec2001144557220.XML failed at ./longevity.pl
> line 48, <XMLFILELIST> line 6.

I notice "chomp" was already covered elsewhere.

The other potential problem is:
Does the destination path ALREADY exist? I haven't tested this but I would
assume that the move call would fail if the destination directory does not
already exist.

These things should help to narrow your problems in any case.

 --
Chris Eustace.



Sat, 21 Aug 2004 06:57:19 GMT  
 newbie trying to solve file copying problem

Quote:


> > >> use File::Copy;
> > >> move($filename,$destination)

> This is NOT the line as given in your attachment .... your longevity.pl has
> this line:

> move('$filename','$destination') or die "Attempt to copy ",$filename,"
> failed";

<snip>

Quote:
> The line should be

> move($filename,$destination) or die "Attempt to copy ",$filename," failed";

-------------------------------------------------------^^---------^^ ???

why not just:

move($filename,$destination) or die "Attempt to copy $filename failed";

big (with, as you suggest further down, $! included too...)



Sat, 21 Aug 2004 07:16:45 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. File::Copy, copying files problem.

2. Using File::Copy and Sockets to copy public files from http addresses

3. Could be a stupid question but I tried whole night to solve it

4. Newbie trying to do a file move

5. Newbie trying to compare two .txt files

6. newbie trying to parse one file into three

7. Perl Newbie trying to parse file

8. SOLVED: problem with seek() and editing a file in place

9. Newbie File::Copy question

10. Newbie ques - how to copy files from URLs

11. Newbie question about File::Copy

 

 
Powered by phpBB® Forum Software