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

how to delete a file in a dir...and/or clear contents of directory...

Status
Not open for further replies.

spewn

Programmer
May 7, 2001
1,034
i'm currently using this to delete file(s):

unlink "file.txt";

is there a better/correct way to delete a file?

also, i want to clear the contents (all files) of a directory...

any ideas?

- g
 
The simple answer is probably a platform specific one so details of your OS would help.



(slowly healing) Trojan.
 
your simple answers are the most complex...

i have a unix based machine and i'm running perl. my question is a language specific one, as this is the perl forum.

so, any ideas?

- g
 
Your question may be language specific but the answer, in this case, can and DOES depend on your system.

In unix and linux the simple way is to use system or backticks to run "rm -rf directory". That would obviously not work in a windoze environment even though we are still talking about perl.

If you want a pure perl solution then you could use File::Find to traverse the tree removing all files individually and then use "rmdir" to remove the directories.

The final unix/linux way is to run the script as superuser and to supply the -U flag to perl and then "unlink" your directory. This is probably not the wisest of solutions.

Oh look! I am talking perl solutions and yet my answers are PLATFORM specific! Shock Horror!
Maybe you should red flag me!

Or maybe you might like to be more subtle about your accusations when someone is trying to help you.




(slowly healing) Trojan.
 
fair enough.

anyway, if i were to delete a file just using:

unline $fileName;

is that going to be okay?

- g
 
You might prefer to try "unlink $fileName;". I'm not sure perl would understand "unline".

Other than that it should be fine, and platform independant!



(slowly healing) Trojan.
 
these are not quotes, they are backticks

to delete a file
Code:
`rm /path/to/file`;
to empty a file
Code:
`echo > /path/to/file`;


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
peruserpengo,

It is usually better to stick with a pure perl approach if it's possible and easy to do.
The only reason I suggested backticks was because `rm -rf directory` is much simpler to code that a File::Find routine that finds each file and directory in turn and empties them in a managed order to allow the "rmdir" to work on empty directories.
In your case you are suggesting removing (and truncating) files which is easy to do with simple perl so I would always prefer a perl rather than shell solution in situations like that.
Your `rm /path/to/file` will be slower and platform dependant with no obvious benefit over "unlink '/path/to/file';".
Your truncate is possibly more relevant since the equivilant perl would take a few commands (open with create flag and close again for example).



Trojan.
 
Everybody knows that what you said is correct.
Once he is using unix it doesn't matter, I think.
So if he preffers unlink good for him. I do to.
But if he wants another way then once he has unix an `rm -f foo` would not mind.
That's why I post this "other solution".


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
> unlink "file.txt";
> Is there a better/correct way to delete a file?

This way is correct. There are other ways, but there is not a better way.

In fact, this way is better than some of the others, because it will work (assuming the filename is correct) irrespective of platform, and also because it does not invoke an unnecessary external program. The unlink builtin has been in Perl at _least_ since perl 5.003, probably longer, so it is safe to use in code that has to work on crochety old setups.

For the other portion, getting the list of files in a directory, there is always opendir/readdir, or you may also be able to use glob (or even the <> glob operator). If you are doing anything nested, you might also want to look at File::Spec or File::Spec::Functions (depending whether you prefer an object-oriented or plain vanilla call-this-function paradigm, respectively).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top