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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unlink not working

Status
Not open for further replies.

clemrock

Programmer
May 20, 2004
118
0
0
US
This is rather strange - I have a function that creates a temperary file in a directory and then a file that deletes the temperary file.

They both use the base path of:

D:/Websites/website_com/test/perl_scripts/test/temp/temp_dir/

and the file name would be appended to that path.

The permissions are set to 777 so it should be able to be deleted by the unlink function like this:

unless( unlink( $file_to_be_deleted ) ){ print "<p><b>file could not be deleted!</b>"; }

I always get the error message that the file could not be deleted when I run the unlink function.

any ideas?

Thanks,
Clem
 
Try changing your print statement to include the perl variable $!, which should contain some error text. This may shed some light on what's happening.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
is the file closed befoe you try and unlink it?

- Kevin, perl coder unexceptional!
 
Ok - I was wrong - it's a permissions problem but when I add this chmod code after the file is created it still gives me a file permissions error when I try to delete it.

Code:
open (SESSION, ">$temp_file") || die("$! \n");
print SESSION  join(":", @temp_array) . "\n" ;
close (SESSION);
	
unless( chmod( 0777, $temp_file ) ){die "error 490: $!"; }

I'm so used to working on a linux box and this site is on a windows machine.

Thanks,
Clem
 
there is no chmod on windows.

- Kevin, perl coder unexceptional!
 
Windows has a system call, 'flag', that changes file permissions.



"We must fall back upon the old axiom that when all other contingencies fail, whatever remains, however improbable, must be the truth." - Sherlock Holmes

 
Code:
open (SESSION, ">$temp_file") or die "Can't open $temp_file: $!";
print SESSION  join(":", @temp_array) . "\n" ;
close (SESSION) or die "Can't close SESSION: $!";

unlink($temp_file) or die "Can't unlink $temp_file: $!";



- Kevin, perl coder unexceptional!
 
LawnBoy (MIS)


Do you have an example on how to call that windows 'flag' system call?
 
Code:
C:\Documents and Settings\Noah>flag
'flag' is not recognized as an internal or external command,
operable program or batch file.

-------------
Cuvou.com | The NEW Kirsle.net
 
The permission denied error message in windows is very generic and may have nothing to do with file permissions but simply means the operating system could not successfully complete whatever it tried to do, such as delete a file. Earlier you asked for ideas and I gave you one, the file is still open, in which case windows will not delte it. Another suggestion is that the file does not exist or you are using the wrong path to the file.

- Kevin, perl coder unexceptional!
 
Kirsle - Odd, it's on all our w2k and xp machines...

clemrock - "flag /?" will show usage (from a windows command prompt).

There is also "attrib", which does the same job as "flag".

 
I guess at this point - what I need to know - is it humanly possible for a perl script to delete a file on a windows 2003 server?
 
of course it is, have you read anyhting I posted?

- Kevin, perl coder unexceptional!
 
Yes I have read what you posted but I have to close the file after it's written. The file maybe referenced for hours until the person logs out.

Once the logout function is invoked then I am calling the unlink() function.

Something tells me that keeping a file open for that amount of time is beyond horrible programming practice.


 
Keeping a file open for a long time is not necessarily a bad thing in-and-of-itself, it depends on why the file needs to be open.

Have you tried running unlink and capturing the operating systems return status, like so:

Code:
unlink('$yourfile') or die "Can't unlink $yourfile: $!";

the value of "$!" might help shed some light, you could also check the servers error logs if possible.



- Kevin, perl coder unexceptional!
 
Good idea Kevin. See post #2

I realise you are number 1 on the MVP list, but do to try to keep up...[smile]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
hehehe... will do Steve [smile]



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

Part and Inventory Search

Sponsor

Back
Top