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!

remove if exists?

Status
Not open for further replies.

JackTheRussel

Programmer
Aug 22, 2006
110
FI
Hi.

I have program, which first removes all music files from folder /home/xxxx/folder.

Then there will be saved some new files:
Example
Code:
system("rm $music_dir/*.wav");
system("rm $music_dir/*.mp3");

Now when I look my error_log, there are multiple errors:
rm: cannot remove /path/*.wav No such file or directory...

Is there any 'if exists' options here?
I looked man pages, but rm command does not include that kind of option.

How could I prevent these error messages?
 
get rid of doing system calls and use unlink in perl, and you can use the -e option to check if a file exits.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
As travs suggests, use glob and unlink.

Code:
[url=http://perldoc.perl.org/functions/unlink.html][black][b]unlink[/b][/black][/url] [url=http://perldoc.perl.org/functions/glob.html][black][b]glob[/b][/black][/url][red]([/red][red]"[/red][purple][blue]$music_dir[/blue]/*.wav[/purple][red]"[/red][red])[/red][red];[/red]


- Miller
 
MillerH: In Programming Perl Larry Wall et. al. write (p 626)
Try to avoid filename globbing. Use opendir, readdir and closedir instead. (As of release 5.6.0 of Perl, basic filename globbing is much more portable than it was, but some systems may still chafe under the Unixisms of the default interface if you try to get fancy.)

Does this advice still holds? (I'm primarily working in a Win environment)

_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
The warning shows up in 5.8.8 documentation also.. but I have used it successfully in multiple unix and windows enviroments with no problems, but I don't do much besides delete everything in a directory normally.



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I concurr, I have used glob in a few apps and usually to simply get the listing of a directory without any problems.

But I always use the FILE module for more complex file manipulation or unlink to delete and file operators such as (-e) for other stuff.

So I guess that aint fancy enough to scare it :)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
I think the warning is in reference to this "Unixism":

@files = <path/to/*.pl>;







------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
rvBasic,

I don't think that that still holds true. However, I will say that I never use file globbing in production level code, and instead stick with opendir, readdir, and File::Spec for name manipulations.

It often takes more code, but I believe it makes the code more robust and easier to make modifications later.

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top