I have a similar script that I used to use years ago. It will loop through all the files in a single folder, run code on each file, then save and close (your code would go after the ////Script goes here line).
It only runs on a single folder though, it doesn’t go through subfolders. Recursion hurts my brain so I probably can’t help you there, but maybe this is at least a start. Also, it saves over all the files, so you’ll probably want to create backups before running this in case anything goes wrong.
Here’s the code for the script:
//This script will execute a script on all INDD files in selected folder
var myFolder = Folder.selectDialog(“Choose an Folder”)
var myFiles = myFolder.getFiles( “*.indd” );
//disables warnings (missing fonts, etc.)
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.neverInteract;
//If you’re placing a for loop within this loop, make sure the variables are different
for( var i = 0; i < myFiles.length; i++ ){
if (myFiles[i].name.slice(-5) == “.indd”){
myCurrentFile = app.open(myFiles[i])
}
/////////////////////////
////Script goes here
myCurrentFile.close (SaveOptions.YES)
////////////////////////
}
//re-enable warnings
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll