# Running a script after building

**URL:** <https://qa.fmod.com/t/running-a-script-after-building/16377>\
**Category:** FMOD Studio\
**Tags:** javascript\
**Created:** [October 29, 2020, 1:25pm UTC](https://qa.fmod.com/t/running-a-script-after-building/16377 "2020-10-29T13:25:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![LenneR](https://avatars.discourse-cdn.com/v4/letter/l/ce73a5/32.png) [@LenneR](https://qa.fmod.com/u/LenneR)\
**Post date:** [October 29, 2020, 1:25pm UTC](https://qa.fmod.com/t/running-a-script-after-building/16377/1 "2020-10-29T13:25:20Z")

</div>

Hi,

I’m trying to run a .bat file after building my project. I want to do this to create a file and add it to .git so when people pull the project the file is detected and a build is automatically made (via a post pull script). However, I have no luck in running the batch file from my javascript code. Is there any way of doing this?

Things I have tried:

> studio.project.buildEnded.connect(function(success) {  
> var projectPath = studio.project.filePath.replace(‘project.fspro’, ‘’);  
> //WshShell = new ActiveXObject(“WScript.Shell”);  
> [//WshShell.Run](https://WshShell.Run)(‘file://’ + projectPath + ‘postbuild.bat’,1,true);  
> var shell = WScript.CreateObject(“WScript.Shell”);  
> shell.Run("start ‘’ /D “+projectPath+ " /W postbuild.bat”);  
> });

Thanks

---

<div class="post-metadata">

**Author:** ![LenneR](https://avatars.discourse-cdn.com/v4/letter/l/ce73a5/32.png) [@LenneR](https://qa.fmod.com/u/LenneR)\
**Post date:** [October 29, 2020, 4:54pm UTC](https://qa.fmod.com/t/running-a-script-after-building/16377/2 "2020-10-29T16:54:38Z")

</div>

Solution is using system.start

> studio.project.buildEnded.connect(function(success) {  
> var projectPath = studio.project.filePath.replace(‘project.fspro’, ‘’);  
> try {  
> studio.system.start(projectPath+“postbuild.bat”, { timeout: 200000 });  
> }  
> catch(err) {  
> studio.system.message(“Failed to run”);  
> var textFile = studio.system.getFile(outputPath);  
> }  
> });

---

<div class="post-metadata">

**Author:** ![LenneR](https://avatars.discourse-cdn.com/v4/letter/l/ce73a5/32.png) [@LenneR](https://qa.fmod.com/u/LenneR)\
**Post date:** [November 5, 2020, 12:54pm UTC](https://qa.fmod.com/t/running-a-script-after-building/16377/3 "2020-11-05T12:54:33Z")

</div>

So I thought I solved this, but after developing the script I noticed it wasn’t running. Or at least not completing, but without any errors thrown.

This code here runs a script. I’ve added a while loop that checks if the script is running, but that freezes FMOD and doesn’t unfreeze after the 20000ms timeout. If you remove the while loop no freeze occurs, but it nothing happens either way.

```
function runAScript() {
    var projectPath = studio.project.filePath.replace('project.fspro', '');
    try 
    {
        console.log("Running "+projectPath+"script.bat");
        var z = studio.system.startAsync(projectPath+"script.bat", { timeout: 20000 });
        while(z.isRunning()){
            console.log(z.readAllStandardOutput());
            console.log(z.readAllStandardError());
        }
    }
    catch(err) 
    {
        studio.system.message("Failed to run "+ projectPath+"script.bat");
    }
}

```

The script itself is really simple. It creates an empty file.

`copy /y NUL thisisafile >NUL`

Hope to see a response from a developer, because the documentation is not making me any wiser at this moment. And searching the forums I only found one thread that uses the system.start command.

Thanks!

---

<div class="post-metadata">

**Author:** ![LenneR](https://avatars.discourse-cdn.com/v4/letter/l/ce73a5/32.png) [@LenneR](https://qa.fmod.com/u/LenneR)\
**Post date:** [November 24, 2020, 10:20pm UTC](https://qa.fmod.com/t/running-a-script-after-building/16377/4 "2020-11-24T22:20:39Z")

</div>

Bump. This one is quite vital for my workflow.

---

<div class="post-metadata">

**Author:** ![richard\_simms](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/richard_simms/32/261_2.png) [@richard\_simms](https://qa.fmod.com/u/richard_simms)\
**Post date:** [November 25, 2020, 2:59am UTC](https://qa.fmod.com/t/running-a-script-after-building/16377/5 "2020-11-25T02:59:27Z")

</div>

Hi sorry for the late reply. Could you please post what your bat file is trying to run? It might be that the FMOD `startAsync()` function is running correctly but the build script is either not being called correctly or has some error.

Edit: Oh, sorry I just saw the bat file `copy /y NUL thisisafile >NUL` command.

I have been able to get FMOD Studio to find out the issue is to do with `projectPath` variable. Please use this at the top of your script instead of `var projectPath`

```auto
var projectPath = studio.project.filePath;
projectPath = projectPath.substr(0, projectPath.lastIndexOf("/"));
```

---

<div class="post-metadata">

**Author:** ![LenneR](https://avatars.discourse-cdn.com/v4/letter/l/ce73a5/32.png) [@LenneR](https://qa.fmod.com/u/LenneR)\
**Post date:** [November 27, 2020, 2:42pm UTC](https://qa.fmod.com/t/running-a-script-after-building/16377/6 "2020-11-27T14:42:12Z")

</div>

> [@richard\_simms](#):
>
> e

Hi. Thanks for the response! I’ve tried your solution but it gives me the same result. Just to be sure we are on the same page. The function below gives you a file in the root folder of your project?

```
var projectPath = studio.project.filePath;
projectPath = projectPath.substr(0, projectPath.lastIndexOf("/") +1);

function runAScript() {
    try 
    {
        console.log("Running "+projectPath+"script.bat");
        var z = studio.system.startAsync(projectPath+"script.bat", { timeout: 20000 });
    }
    catch(err) 
    {
        studio.system.message("Failed to run "+ projectPath+"script.bat");
    }
}

var test = {
    name: "test",
    execute: function(){ runAScript(); }
};

var automationMissing = true;
var menuItems = studio.menu.menuItems();
for (var i = 0; i < menuItems.length; ++i) {
    if (menuItems[i].name == test.name) {
        automationMissing = false;
        break;
    }
}

if (automationMissing) {
    studio.menu.addMenuItem(test);
}

```

Edit: I’ve added +1 to the following row.

```
projectPath = projectPath.substr(0, projectPath.lastIndexOf("/") +1);

```

Now my console says the following:

```
Running pathToMyProject/FMOD/script.bat 

```

Which seems correct. But I do not get a file in a my project root directory.

---

<div class="post-metadata">

**Author:** ![richard\_simms](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/richard_simms/32/261_2.png) [@richard\_simms](https://qa.fmod.com/u/richard_simms)\
**Post date:** [November 30, 2020, 11:14pm UTC](https://qa.fmod.com/t/running-a-script-after-building/16377/7 "2020-11-30T23:14:41Z")

</div>

The issue seems to be that running a bat script that does not explicitly state its directory does not work as expected. The bat script by itself will run and create this “thisisafile” file but when run by an external program (such as FMOD Studio) it will create it in a temporary folder somewhere not expected by the user.

Instead, please make the following adjustments:

1. In the bat script, make these adjustments to allow it to take a path as an argument and create the file in that directory:

```auto
echo off
set arg1=%1
cd %arg1%
copy /y NUL thisisafile >NUL

```

1. Please change line 8 of your script to provide the project path. Ensure the directory provided exists, the bat script does not automatically create directories if they don’t exist:

```auto
var z = studio.system.startAsync(projectPath+"myScript.bat", { timeout: 20000, args: [projectPath + "/script_export"] }); // Ensure this path exists or it will silently fail

```

---

<div class="post-metadata">

**Author:** ![LenneR](https://avatars.discourse-cdn.com/v4/letter/l/ce73a5/32.png) [@LenneR](https://qa.fmod.com/u/LenneR)\
**Post date:** [November 30, 2020, 11:46pm UTC](https://qa.fmod.com/t/running-a-script-after-building/16377/8 "2020-11-30T23:46:45Z")

</div>

Yes! So simple. Thanks a bunch Richard that really helps me a lot. To tackle this issue a little simpler, you can add the following line to your script which will set the directory of the script to the location of the script.

```
cd "%~dp"

```

So you get

```
cd /D "%~dp0"
copy /y NUL thisisafile >NUL

```

And it will work as well. Thanks again.
