Small question, hard answer! This depend of the system, the type of file, if is directory, link, is loked or opened, etc...

* For files:
your need to know if you have permission (like unix), and if it's loked, and in some systems if it's opened.

"Don't assume that a single unlink completely gets rid of the file: some filesystems (like VMS) have versioned filesystems, and unlink() removes only the most recent one". (from pod)

* For links (if the OS accept):
Are you trying to delete the link or the target of the link!? If is the target all the check for files need to be done.

* For directorys:
In almost OS, it need to be clean, without files or sub-directorys inside. In some OS like Windows it can't be opened by another program.

If you really want to make this easy and safe, use a module! The advantage of the module is that it makes all the checks, for each OS, and the code are already tested!

* See this modules:

File::CheckTree (Maybe this is only what you need)
File::Spec (for path)
File::Stat (can be usefull for links)
filestest (Another way to test permissions)

The good thing is that this modules comes with the standart release! :-P

Take a look at "perlport - Writing portable Perl", specially in the item "Files and Filesystems", but you should read every thing, lot of things that we have to know are there!

"The creativity is the expression of the liberty".

In reply to Re: How do I know if I can delete a file/folder? by gmpassos
in thread How do I know if I can delete a file/folder? by soffen

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.