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

Removing files and/or directories

Status
Not open for further replies.

qednick

Programmer
Jul 26, 2002
516
US
Hi all, I got two simple questions:

1
can anybody tell me the best way on UNIX to remove files and/or directories BUT to test whther they exist beforehand.

Currently I'm using [tt]system("rm filename")[/tt] and [tt]system("rmdir directory")[/tt] - however, I think there must be a way like this:

[tt] if (file exists)
delete it;

if (directory exists and is empty)
delete it;[/tt]

only I don't know how on UNIX.

2
Also, if I kick off another program from within a program, is there any way I can get the PID number easily.

Currently, I'm doing:

[tt]system("./program.out& > output.txt");[/tt]

then examining the contents of 'output.txt' - there must be a better way????

Thanks in advance.
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
1
Make a ifstream to the file, and see if .bad() is true.... if bad() == true then the file doesn't exsist or the only thing in it is EOF.

so try:

Code:
ifstream fin;
fin.open("this.txt");
if(!fin.bad())
{
  fin.close();
  system("rm this.txt");
}
else
  fin.close();

2
You may want to look into popen() for this question... It opens a pipe to execute other code in.... use the man pages or the internet to find more information.
 
Thanks js, I had actually considered doing something similar to your answer for #1 but how would this work for just an empty directory? I guess I could always pass a [tt]ls[/tt] command.

As for popen(), I have used this for sending mail before so thanks for pointing that one out! I'll have a scout round for more info on that one.

:)
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
Hmmm... for a directory, it'd be a bit harder. You may want to try a file pointer and memory mapping, there may be some old C code that would help. How about looking at the source code for the rm command... The RM command itself is written in C/C++ or basic assembly. However it does the check for the file would work before the invocation of rm, or you may just want to copy and paste the rm code straight into it's own function. This would be a little more searching than I'd do just to get rid of a line printed to the screen that could have been piped or redirected from the sceen...
 
Your code is a little ugly ;)
1. you can use access() to check file or dirctory exists or not. actually rmdir() fails if the directory is not empty.

2. you can fork(), then exec() any program you like, From fork() you can get pid.

system() is much slower.
 
For removing files, you can use a function "unlink(filepath)". I don't know whether it is available in all UNIX flavors. It works fine with HP-UX
 
Hi,

You may want to look into using the opendir and readdir commands to check for directory and files exist.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top