Home Forums General XMetaL Discussion Run batch file from macro Reply To: Run batch file from macro

Derek Read

Reply to: Run batch file from macro

Sorry, wasted your time. I actually needed to know where the .bat file is (my question should have replaced MCR with BAT).

Various solutions:

var batFileName = "myfile.bat";
var batFileSubfolder = "\SubFolder\";
var batFilePath = Application.Path + batFileSubFolder + batFileName;

Then replace “myfile.bat” with your filename (obviously) and “\SubFolder\” with something like “\Macros\“.

That will work if you are putting the .bat file inside any subfolder in the XMetaL installation path, the most important bit being that Application.Path gives you the folder for the currently running instance of xmetal60.exe (or other version).

If you are putting the .bat file in the same folder as xmetal.mcr then you may use the API Application.AllUsersMacroFile, however, that gives you the full path to that MCR file so you would need to strip off the filename and append your .bat file name. It would be easier to use the method above with the folder “\Macros\“.

Note that there are various other APIs that give you paths for files included in a document-level customization, not useful in this case (as your customization uses xmetal.mcr it is application-level) so this is just an FYI (check the Programmers Guide for more info if you are curious, searching for “path” will turn up most of them).

If you are putting the .bat file outside the XMetaL install folders and you always install it to the same location (and therefore know that it must be there) you might just hard code it:
var batFilePath = "C:\XMetaLCustomizationFilesAlwaysHereForEachUser\myfile.bat";

If it is outside the XMetaL install folders but in a Windows “special” folder (such as the users “Documents and Settings” folder for example) that location might change on a per-user basis and you may wish to read this so you can find the user-specific location: http://forums.xmetal.com/index.php/topic,27.0.html

Anticipating Missing Files:
In all cases you may wish to wrap this code in a try...catch block, or use the API Application.FileExists() before attempting to run the .bat file so you can display a nicer error message if the file is not found for whatever reason.