Gossamer Forum
Home : General : Perl Programming :

delete and rename

Quote Reply
delete and rename
what is the perl code to delete a directory and what is perl code to rename a file or a directory??
Quote Reply
Re: delete and rename In reply to
Don't know if this works or directories...
Code:
rename("/path/old.name","/path/new.name");

This either...
Code:
unlink ("/path/file_to_delete");

------------------
webmaster@racedaze.com
Quote Reply
Re: delete and rename In reply to
From perldoc -f rename:

Quote:
rename OLDNAME,NEWNAME
Changes the name of a file. Returns `1' for success, `0'
otherwise. Behavior of this function varies wildly
depending on your system implementation. For example, it
will usually not work across file system boundaries,
even though the system *mv* command sometimes
compensates for this. Other restrictions include whether
it works on directories, open files, or pre-existing
files. Check the perlport manpage and either the
rename(2) manpage or equivalent system documentation for
details.

From perldoc -f rmdir:

Quote:
rmdir FILENAME
rmdir Deletes the directory specified by FILENAME if that
directory is empty. If it succeeds it returns TRUE,
otherwise it returns FALSE and sets `$!' (errno). If
FILENAME is omitted, uses `$_'.

And finally, from perldoc -f unlink:

Quote:
unlink LIST
unlink Deletes a list of files. Returns the number of files
successfully deleted.

$cnt = unlink 'a', 'b', 'c';
unlink @goners;
unlink <*.bak>;

Note: `unlink()' will not delete directories unless you
are superuser and the -U flag is supplied to Perl. Even
if these conditions are met, be warned that unlinking a
directory can inflict damage on your filesystem. Use
`rmdir()' instead.

If LIST is omitted, uses `$_'.

I hope this helps.
Quote Reply
Re: delete and rename In reply to
Thanks a lot, the problem has solved successfully!!