Re: unlink fails to delete files with perculiar names
by BrowserUk (Patriarch) on Nov 22, 2011 at 09:51 UTC
|
If you had warnings enabled, you'd see that part of your filename is being taken as a perl variable for interpolation:
$targ = "D:/Users/Dave/Documents/Personal/CV/~$rriculum Vitae - 09 Jun
+e 2007.doc";;
Use of uninitialized value $rriculum in concatenation (.) or string at
+ (eval 17) line 1, <STDIN> line 10.
Just escape the $ with \: $targ = "D:/Users/Dave/Documents/Personal/CV/~\$rriculum Vitae - 09 Ju
+ne 2007.doc";;
And the subsequent unlink will likely work.
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
|
The OP doesn't say how $targ was set. It's merely speculation that he assigned it using a double-quoted literal. It might just as well have come from a readdir or whatever.
Also, why would unlink have returned 1, as the OP states? That should only have happened in the (unlikely) case there also was a file 'D:/Users/Dave/Documents/Personal/CV/~ Vitae - 09 June 2007.doc' around at the time of the unlink call.
| [reply] [d/l] [select] |
|
It's merely speculation that he assigned it using a double-quoted literal
Agreed. That's why likely work.
It might just as well have come from a readdir
But don't you have to combine the values from readdir with the path in order to get a fully qualified path?
Could he be doing something like $targ = "$path/$fname";?
why would unlink have returned 1, as the OP states?
Not sure, but that construct looks suspect to me: my($no) = unlink $targ || carp "Unable to delete file $targ\n";.
What ends up in $no if unlink returns 0; the return value from carp()?
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
|
|
|
| [reply] |
|
| [reply] |
|
Then I suggest you try del "D:\Users\Dave\Documents\Personal\CV\~$rriculum Vitae - 09 June 2007.doc" on a command line and see what error message is produced by the shell.
Update: Having seen one of your other replies, try doing:
attrib -h -s -r "D:\Users\Dave\Documents\Personal\CV\~$rriculum Vitae
+- 09 June 2007.doc"
And then the unlink or del.
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
Re: unlink fails to delete files with perculiar names
by Eliya (Vicar) on Nov 22, 2011 at 08:58 UTC
|
my($no) = unlink $targ || carp "Unable to delete file $targ\n";
This doesn't explain your deletion problem, but you most likely meant
my $no = unlink $targ or carp "Unable to delete file $targ\n";
As you have it, the carp would only ever fire if $targ is false (e.g. ""), but not when the unlink failed. | [reply] [d/l] [select] |
Re: unlink fails to delete files with perculiar names
by moritz (Cardinal) on Nov 22, 2011 at 09:56 UTC
|
Just a speculation: the variable name looks like it could be an automatic backup file from MS Word, and might be locked by the Word process, and thus not (easily) removable.
Can you delete the file simply by pressing DEL in the Windows Explorer?
| [reply] [d/l] |
|
I think you are right in that the file is an auto backup from word, and probably one that was orphaned some months back. Unfourtunatly the file cannot be seen in an explorer window, or from the dir command in a command window.
| [reply] |
|
Unfourtunatly the file cannot be seen in an explorer window, or from the dir command in a command window.
hidden files require doing more than the default to see them
| [reply] |
|
Thinking more on this I think that you have touched on the source of the file (a temporary backup from an MS Office program - I have excel, word and powerpoint examples). If I try to delete a file using unlink with the application still open it complains (Permission denied).
The code snippet is from a file replication utility. I think what has happened is that an oen file has been replicated, the original destroyed by the creating application, and then replaced by a subsequent replication event.
The preventative cure is to prohibit the copy of a file with the pattern "/~$" in its path/name. The curative solution may be to delete any directory containing files with this pattern - and recreate what I want from a backup
| [reply] |
Re: unlink fails to delete files with perculiar names
by Khen1950fx (Canon) on Nov 22, 2011 at 09:22 UTC
|
#!perl
use strict;
use warnings;
use File::Overwrite qw(overwrite_and_unlink);
my $new_file = shift @ARGV;
overwrite_and_unlink($new_file);
Note: The file must be closed in order for this to work on
Windows.
| [reply] [d/l] |
Re: unlink fails to delete files with perculiar names
by Anonymous Monk on Nov 22, 2011 at 09:20 UTC
|
$ dir /b ~$
File Not Found
$ echo > "~$"
$ dir /b ~$
~$
$ perl -e " unlink '~$' or die $!"
$ dir /b ~$
File Not Found
$ notepad > "~$"
$ dir /b ~$
~$
$ perl -e " unlink '~$' or die $!"
Permission denied at -e line 1.
$ perl -e " unlink '~$' or die qq/ $! \n $^E / "
Permission denied
The process cannot access the file because it is being used by another process at -e line 1.
| [reply] |
Re: unlink fails to delete files with perculiar names
by ikegami (Patriarch) on Nov 22, 2011 at 18:06 UTC
|
unlink reports the file deleted (and $no is set to 1 indicating one file deleted) - but in reality the file still exists.
That means a program still has the file open. The file is marked to be deleted and will be deleted as soon no program has the file open anymore. Windows doesn't have anonymous files like unix does.
| [reply] |
Re: unlink fails to delete files with perculiar names
by salva (Canon) on Nov 22, 2011 at 09:52 UTC
|
Use some utility as Process Monitor to see what's going on at the OS level. | [reply] |
Re: unlink fails to delete files with perculiar names
by stefbv (Curate) on Nov 22, 2011 at 08:57 UTC
|
my($no) = unlink "$targ" || carp "Unable to delete file $targ\n";
| [reply] [d/l] |
|
Putting the variable inside quotes is useless in this context.
| [reply] |
|
I think it is not useless it's about dealing with spaces in file names.
Update: Sorry, made some further tests and I was wrong.
| [reply] |
|
Re: unlink fails to delete files with perculiar names
by Marshall (Canon) on Nov 22, 2011 at 09:00 UTC
|
unlink ($targ) || carp "Unable to delete file $targ\n";
Unlink is different than "delete".
Can you provide more information about how and why "unlink" does not work?
| [reply] [d/l] |
|
| [reply] |