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

unlink won't delete file

Status
Not open for further replies.

CJason

Programmer
Oct 13, 2004
223
US
I'm am running a script on Windows. In the script, I'm creating a couple files. At the end of the script, I attempt to unlink them...but, they don't unlink. If I run a simple script afterwards, that ONLY unlinks these files, they delete appropriately...it just doesn't work in the same script as where the files are created.

Any ideas on what's going on or how to remedy the situation?

Thanks!
 
How are you creating the files? If you are doing a open are you closing the files before you try and unlink them?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Actually, perltidy is generating the 2 files I'm trying to delete. Can I maybe look at the return code of unlink to see what the problem is?
 
are you doing a system call for pertidy? I haven't used it yet so I don't know how it integrates.

I have also seen problems on windows boxes trying to unlink files immediately after creating them because of virus scanners trying to scan them. What error is unlink giving you back? Also if you post your code I'll take a look at it to.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Here some code to try:

Code:
use Perl::Tidy;
perltidy(source=>"tidytest.pl");
unlink "tidytest.pl.ERR";

Make sure your tidytest.pl has some sort of syntax problem (like too many "}" or something). You'll get two output files:
tidytest.pl.tdy (tidied file)
tidytest.pl.ERR (tidy error file).

I can't get the .ERR file to unlink!

Thanks.
 
Well.. I can't get it to delete either.. but if you don't want it, how about just not creating it?

Code:
use Perl::Tidy;
perltidy(source=>"tidytest.pl", errorfile=>\@blah);
@blah = '';
print "Done with perltidy\n";


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Hmmm, that's a good idea. I thought I tried something similiar to this and it didn't work. Oh well, I'll give it another shot.

Thanks for the idea!!!
 
Most likely the problem is the files are still open. You can try to close using close() then try and unlink.

Code:
use Perl::Tidy;
perltidy(source=>"tidytest.pl");
close "tidytest.pl.ERR" or print "Can't close error file: $!";
unlink "tidytest.pl.ERR" or print "Can't delete error file: $!";

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I didn't realize you could close a file without knowing the filehandle (and trust me.. I looked through the module, but nothing I tried worked)

I have this code now
Code:
use Perl::Tidy;
perltidy(source=>"tidytest.pl");
close "tidytest.pl.ERR" or print "Can't close error file: $!";
unlink "tidytest.pl.ERR" or print "Can't delete error file: $!";
and I got this
Code:
C:\Documents and Settings\Travis.MERLIN\Desktop>tek2.pl
Name "main::tidytest.pl.ERR" used only once: possible typo at C:\Documents and S
ettings\Travis.MERLIN\Desktop\tek2.pl line 3.
## Please see file tidytest.pl.ERR
close() on unopened filehandle tidytest.pl.ERR at C:\Documents and Settings\Trav
is.MERLIN\Desktop\tek2.pl line 3.
Can't close error file: Bad file descriptorCan't delete error file: Permission d
enied

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
oops.... posted before I had a cup of coffee. Of course you can't use the filename to close the file. My bad. [hammer]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I tried on xp to.. got the same results. I think perltidy isn't closing the filehandle.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Using the process explorer from systernals it definately shows a handle being left open to the .ERR file until the program closes.
 
Well ... I found something that works so I will post it and maybe the people who are a bit better then me can improve on the idea.

What I did was make two programs the first:
Code:
#! perl

use strict;
use diagnostics;
use warnings;

system 'create.pl';
sleep 15;
system 'rm tidytest.pl.ERR'
and the second named create.pl:
Code:
#! perl

use strict;
use diagnostics;
use warnings;

use lib './lib';
use Perl::Tidy;

perltidy(source=>"bad.pl");
You could pass the second the name using ARGV or something instead of hard coding it and although not the best solution it's better then nothing.

I hope this helps.
 
Corection:
Code:
perltidy(source=>"[red]bad.pl[/red]";
should have read:
Code:
perltidy(source=>"[red]tidytest.pl[/red]";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top