Removing Files

Hey all, I have a question concerning a program I’m writing. Currently my program takes in some data, makes some histograms, and then writes everything to a few files. Then it does it again with different data. The problem is that it tries to write the second data to files with the same name as the ones from the first process, and so the program freaks out.

I don’t need the first set of files anymore by the time the program begins creating the second set of files, so I was wondering what code to use to delete the files. So generally my question is this:

  1. How can I code my program to allow me to delete files previously in use? (manual deletion is prevented because root is using the files, how can I stop this?)

  2. How can I use code in a program/macro to delete files in the current directory.

Hopefully there is an easy answer to this. If not, even a difficult solution would be welcome.

Thanks in advance for any help.

Open your file with option "recreate"
see doc of TFile::Open


Thanks for the tip, however the TTree call ups are within functions that I did not write. I have tried editing them myself to use recreate the second time around, however they seem to go haywire after my edits for no reason apparent to me, so I’d rather not have to go the route of debugging them if at all possible.

Is there no way to delete the .root files without having to exit root?

gSystem->Exec("rm myfile.root");

Thanks again. However there is my problem. Naturally that was the first thing I tried. But root says " ‘rm’ is not recognized as an internal or external command, operable program or batch file." So I am confused.

Check your PATH when running in batch and try, eg

gSystem->Exec("/bin/rm -f myfile.root");


So apparently, you’re working on Windows… Then try:



use gSystem->Unlink(“myfile.root”)

Iam, Eddy

Thanks all. Its working now.