I have a challenge with the perl unlink command. I use this in well established code to remove unwanted files - but recently I notice it fails with some files. I am using Windows 7, and Activstate binary build 1007 perl, v5.10.1 built for MSWin32-x86-multi-thread. Here's a code snippet...

my($no) = unlink $targ || carp "Unable to delete file $targ\n"; if ($no == 0){ printf " %s\n", $!; }else{ printf " %s\n", $no; }

So very simple... The variable $targ only ever has one file name. When the file to delete has unusual (illegal?) characters I observe the problem. For instance with a filename of

D:/Users/Dave/Documents/Personal/CV/~$rriculum Vitae - 09 June 2007.do +c
unlink reports the file deleted (and $no is set to 1 indicating one file deleted) - but in reality the file still exists. I suspect the error is generated by the tilde (~) and dollar character following directly after the directory seperator character (/), but can't figure out how to escape it.

I can stat the file before calling unlink (and it is found). Does anyone have a suggestion ? Thanks in advance


In reply to unlink fails to delete files with perculiar names by DaveARoberts

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.