Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Moving Files In Perl 1

Status
Not open for further replies.

blundellp

Technical User
Apr 6, 2005
25
0
0
GB
I am creating a program that checks the first line of a file which has the date of the month init. It then moves that file into to corresponding folder. I.e a file from november into the "1-month-ago" folder. I am haveing trouble with one line. I am useing the code from
which states
use File::Copy;
$oldlocation = "myhtml.html";
$newlocation = "html/myhtml.html";
move($oldlocation, $newlocation);

but my program has an error on the move line, i don't know how much of my program to paste sorry as it is pretty big but here's a snip:

$date = substr($firstline,3 ,2); #Collect the month from the log
$N = 0;
until ( $N == 6) #Until all 6 months have been checked
{
@folder = qw( 1-month-ago 2-months-ago 3-months-ago 4-months-ago 5-months-ago 6-months-ago);
$month - 1; #Counts down 6 months
if ( $date == $month) #if the first line of the log equals the date
{
$oldlocation = "/perl/ass/@logfiles[$hour]"; #Cut and Pastes the log file into corresponding folder
$newlocation = "/perl/ass/@folder[$N]/@logfiles[$hour]";
move($oldlocation, $newlocation) or die "File cannot be moved."; #THIS LINE IS WERE THE ERROR STOPS THE PROGRAM
$N = 3;
$date = 999; #Makes the date not equal to any other date
}
$N++;
$history = $history - 7;
}

any help is much appreciated
Paul
 
print the value returned by these two lines:

Code:
            $oldlocation = "/perl/ass/@logfiles[$hour]";           #Cut and Pastes the log file into corresponding folder
            $newlocation = "/perl/ass/@folder[$N]/@logfiles[$hour]";
[b]print "old = $oldlocation -- new = $newlocation\n";[/b]


@logfiles[$hour] should really be wrriten as: $logfiles[$hour]

same with @folder[$N]

- Kevin, perl coder unexceptional!
 
The print line returns >>

old = /perl/ass/03pm01Dec.txt -- new = /perl/ass/1-month-ago/03pm01Dec.txt

did you want to see this or shall i put it in my code? Becuase it stills says the error :(
 
Add more explicit error reporting. From the documentation at


PerlDoc said:
All functions return 1 on success, 0 on failure. $! will be set if an error was encountered.

Therefore change your move attempt to include $!

Code:
 move($oldlocation, $newlocation) or die "File cannot be moved: $!";

This should tell you what the problem is.
 
just wanted to see to make sure the variables were defined correctly. Try this, replace this line:

Code:
 move($oldlocation, $newlocation) or die "File cannot be moved.";

with:

Code:
move($oldlocation, $newlocation) or die "File cannot be moved: [b]$![/b]";

and see what the value of "$!" is when the error is printed. All of your "die" statemnts should really include "$!" in the them. "$!" captures the error returned by the operatng system.

- Kevin, perl coder unexceptional!
 
Ah yes its a permission's error, thanks i didnt no about the '$!'

File cannot be moved: Permission denied at temp.pl line 54, <CURRENTLOG> line 1.

hmmm i'm sure i have full access rights on this computer, i'll have to read up on this

Paul
 
Well i've searched the internet an certain books, i can work out that i need to do something like;

$mode = 0700; chmod $mode, "/perl/ass/$folder[$N]";



if ( $date == $month) #if the first line of the log equals the date
{
$oldlocation = "/perl/ass/@logfiles[$hour]"; #Cut and Pastes the log file into corresponding folder
$newlocation = "/perl/ass/$folder[$N]/$logfiles[$hour]";
#$mode = 0700; chmod $mode, "/perl/ass/$folder[$N]";
#$mode = 0700; chmod $mode, "/perl/ass/$folder[$N]/$logfiles[$hour]";
move($oldlocation, $newlocation) or die "File cannot be moved: $!";
$N = 3;
$date = 999; #Makes the date not equal to any other date
}

however i can't find the actual meaning of all the different numbers '0700'. And if 0700 does allow me permission then i must have it implementated wrong as my program still won't work, still says permissions error.
 
Try setting the folders to 0755. chmod setting are like so:


Code:
            read   write   execute
owner         X      X       X     
group         X              X     
all users     X              X

where the X indicates a permission is turned on (set).
(the above is equivalent to 0755 permissions)


or like so:

-rw-r--r-- for regular files (0644)
drwxr-xr-x for directories (0755)


owner = you and your scripts
group = your associated group
all users = world wide web (the internet)

read = 4
write = 2
execute = 1

Code:
7 (4+2+1) user has all permissions
6 (4+2)   user has read and write permissions
5 (4+1)   user has write and execute permissions
4         user has read permissions only
3 (2+1)   write and execute permissions
2         write permission only
1         read permission only
0         no permissions at all

further reading:







- Kevin, perl coder unexceptional!
 
this one:

5 (4+1) user has write and execute permissions


should be:

5 (4+1) user has read and execute permissions


- Kevin, perl coder unexceptional!
 
I have the code i've put the permissions in but it stills says permission error :S


if ( $date >= $historyformer && $date <= $history ) #if the log equals greater than a week ago but less than 2 weeks ago
{
$hour = $hour - 1;print "hangs up on loop number:- $hour \n";$hour = $hour + 1;
print " old = $oldlocation \n new = $newlocation\n";
$yes = "/perl/ass/@folder[$N]";
chmod (0755, $yes) or die "Can't change permissions: $!"; #Gives everyone all permissions on all files in the folder
$yes = "/perl/ass/@folder[$N]/@logfiles[$hour]";
chmod (0755, $yes) or die "Can't change permissions: $!"; #Gives everyone all permissions on all files in the folder
$oldlocation = "/perl/ass/@logfiles[$hour]"; #Cut and Pastes the log file into corresponding folder
$newlocation = "/perl/ass/@folder[$N]/@logfiles[$hour]";
move($oldlocation, $newlocation) or die "File cannot be moved: $!";
$N = 3;
$date = 999; #Makes the date not equal to any other date
}
 
Ask tech support what the problem is. There could be a number of reasons why chmodding a folder to 755 is not working and the reasons may be good ones that you shouldn't be messing with.

If there is no tech support, you can try changing the umask:

Code:
if ( $date >= $historyformer && $date <= $history ) {
   [b]umask(000);[/b]
   #rest of your code


I urge you to ask your tech support first though.

- Kevin, perl coder unexceptional!
 
Hey i found the error, I was getting permission denied, because i was attempting to open a directory as a file

My readdir was pulling in all the entries in my /perl/ass folder including subdirectories. And i was then attempting to open one of those subdirectories as a file.

Soon to be fixed :D

 
Oh and by the way, window's does not support chmod you have to use "system hooks.</
 
Oh and by the way, window's does not support chmod you have to use "system hooks.</"

DOH!

- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top