# Using ROOT prompt commands in a C script

**URL:** <https://root-forum.cern.ch/t/using-root-prompt-commands-in-a-c-script/51204>\
**Category:** Newbie\
**Created:** [August 15, 2022, 7:15pm UTC](https://root-forum.cern.ch/t/using-root-prompt-commands-in-a-c-script/51204 "2022-08-15T19:15:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![theittsco](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/theittsco/32/30606_2.png) [@theittsco](https://root-forum.cern.ch/u/theittsco)\
**Post date:** [August 15, 2022, 7:15pm UTC](https://root-forum.cern.ch/t/using-root-prompt-commands-in-a-c-script/51204/1 "2022-08-15T19:15:40Z")

</div>

Hello, trying to write a root compiled C script to calculate how many files are in a premade root file. My current strategy is to

1. Move into a directory using `<directory name>->cd()`
2. Then count the number of contents in that directory, possibly using `.ls` and iterating over each line to get the total number of contents.

And append this number to a file. Is there an easier way of doing this or is this a good solution? Also how would I incorporate the root command line methods in a C script?

_ROOT Version:_ 6.26/00  
_Platform:_ linuxx8664gcc  
_Compiler:_ g++ 9.3.0

* * *

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [August 16, 2022, 8:42am UTC](https://root-forum.cern.ch/t/using-root-prompt-commands-in-a-c-script/51204/2 "2022-08-16T08:42:34Z")

</div>

One approach:

```auto
TFile* f = TFile::Open("your.root", "READ")
cout << f->GetListOfKeys()->GetSize() << endl;

```
