Save the file from Enterprise Architect Script
I am trying to create a script that shows a dialog where I can select the path to save the file to. The following got almost, but this is to open the file, not save it.
var filePath = OpenCSVFileDialog();
var fileName = GetFilenameFromPath(filePath);
function OpenCSVFileDialog()
{
var Project;
var Filename, FilterString, Filterindex, Flags, InitialDirectory, OpenorSave, filepath;
Filename = "";
FilterString = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*||";
Filterindex = 1;
Flags = 0;
InitialDirectory = "";
OpenorSave = 0;
Project = Repository.GetProjectInterface();
filepath = Project.GetFileNameDialog(Filename, FilterString, Filterindex,
Flags, InitialDirectory, OpenorSave);
return filepath;
}
function GetFilenameFromPath(filePath)
{
var bsindex, fileName;
// find the last backspace in the file path
bsindex = filePath.lastIndexOf("\\");
if (bsindex > 0)
{
// get the name of the file only - minus the directory path
fileName = filePath.substring(bsindex+1, filePath.length);
}
else
{
fileName = filePath;
}
return fileName;
}
source to share
To get the path to the file, use the Jscript-Dialog
script available in the EAScriptLib script group, this will prevent you from rewriting all the code to get the dialog.
(To reference another script, use !INC
, in this case, place !INC EAScriptLib.JScript-Dialog
at the beginning of your script)
Call DLGSaveFile(filterString,filterIndex)
and provide:
- filter string, in your case its
CSV Files (*.csv)|*.csv|All Files (*.*)|*.*||
- The filter index (at the previous point) you want to use, which you already provide
It will return the path to the file.
You can use Project.GetFileNameDialog
, this is the same but with more parameters as well, here's the link
If you are using a CSV library to create a CSV file, your file should be exported after the function call CSVEExportFinalize()
. You must call firstCSVEExportInitialize(filepath,columns,exportcolumsHeadings)
For any file, this can be done with JScript and VBScript , but not javascript
JScript
var fso = new ActiveXObject("Scripting.FileSystemObject");
var a = fso.CreateTextFile("c:\\testfile.txt", true);
a.WriteLine("This is a test.");
a.Close();
V. B.
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("c:\testfile.txt", True)
MyFile.WriteLine("This is a test.")
MyFile.Close
source to share