# Execute script directly

**URL:** <https://root-forum.cern.ch/t/execute-script-directly/40384>\
**Category:** Newbie\
**Created:** [July 12, 2020, 8:32am UTC](https://root-forum.cern.ch/t/execute-script-directly/40384 "2020-07-12T08:32:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zhassulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/zhassulan/32/20693_2.png) [@Zhassulan](https://root-forum.cern.ch/u/Zhassulan)\
**Post date:** [July 12, 2020, 8:32am UTC](https://root-forum.cern.ch/t/execute-script-directly/40384/1 "2020-07-12T08:32:37Z")

</div>

Hi,  
I use ROOT 5.16/00 on Windows. So I want to write a script, that allows me go straight to directory (without opening new TBrowser) where I’ll be able to run two scripts, which I already have: one of them converter .dat files to .root, another draw histograms.

Thank you!

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [July 12, 2020, 9:55am UTC](https://root-forum.cern.ch/t/execute-script-directly/40384/2 "2020-07-12T09:55:35Z")

</div>

Something like this?

```auto
void change_dir(const char *dir, const char *macro1, const char *macro2)
{
   TString file1(macro1), file2(macro2);
   gSystem->ChangeDirectory(dir);
   std::cout << "Directory changed to " << gSystem->WorkingDirectory() << std::endl;
   if (gSystem->FindFile(".", file1)) {
      std::cout << "Running " << macro1 << std::endl;
      gInterpreter->ExecuteMacro(macro1);
   }
   if (gSystem->FindFile(".", file2)) {
      std::cout << "Running " << macro2 << std::endl;
      gInterpreter->ExecuteMacro(macro2);
   }
}

```

Here is the output:

```auto
C:\Users\bellenot\rootdev>root -l
root [0] .x change_dir.C("Forum/testing", "macro1.C", "macro2.C")
Directory changed to C:\Users\bellenot\rootdev\Forum\testing
Running macro1.C
This is macro1...
Running macro2.C
This is macro2...
root [1]

```

---

<div class="post-metadata">

**Author:** ![Zhassulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/zhassulan/32/20693_2.png) [@Zhassulan](https://root-forum.cern.ch/u/Zhassulan)\
**Post date:** [July 12, 2020, 4:07pm UTC](https://root-forum.cern.ch/t/execute-script-directly/40384/3 "2020-07-12T16:07:54Z")

</div>

Thank you very much! It works. One question how can I return back to previous path? I wrote gROOT-\>cd(); but it doesn’t work.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [July 12, 2020, 5:47pm UTC](https://root-forum.cern.ch/t/execute-script-directly/40384/4 "2020-07-12T17:47:21Z")

</div>

```auto
void change_dir(const char *dir, const char *macro1, const char *macro2)
{
   TString file1(macro1), file2(macro2);
   std::string olddir(gSystem->WorkingDirectory());
   gSystem->ChangeDirectory(dir);
   std::cout << "Directory changed to " << gSystem->WorkingDirectory() << std::endl;
   if (gSystem->FindFile(".", file1)) {
      std::cout << "Running " << macro1 << std::endl;
      gInterpreter->ExecuteMacro(macro1);
   }
   if (gSystem->FindFile(".", file2)) {
      std::cout << "Running " << macro2 << std::endl;
      gInterpreter->ExecuteMacro(macro2);
   }
   gSystem->ChangeDirectory(olddir.c_str());
   std::cout << "Directory changed to " << gSystem->WorkingDirectory() << std::endl;
}

```

---

<div class="post-metadata">

**Author:** ![Zhassulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/zhassulan/32/20693_2.png) [@Zhassulan](https://root-forum.cern.ch/u/Zhassulan)\
**Post date:** [July 13, 2020, 4:32am UTC](https://root-forum.cern.ch/t/execute-script-directly/40384/5 "2020-07-13T04:32:56Z")

</div>

I don’t know what I would do without you:)  
Thanks a lot!
