3

I'm tring to create a script thats shows a dialog where I can select a path to save a file. The following got it almost, but this is to open a 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;
}
qwerty_so
  • 35,448
  • 8
  • 62
  • 86
byandreee
  • 57
  • 6

2 Answers2

3

To get the file path, use the Jscript-Dialog script available in the EAScriptLib Script group, it will prevent you from rewriting the whole code for getting the dialog.

(To reference another script, use !INC, in this case, put !INC EAScriptLib.JScript-Dialog to the top of your script)

Call DLGSaveFile(filterString,filterIndex) and provide:

  • the filter string, in your case its CSV Files (*.csv)|*.csv|All Files (*.*)|*.*||
  • The index of the filter(in the previous point) you want to use, which you are already providing

It will return the path of the file.

You can use Project.GetFileNameDialog, it's the same thing but with more parameters also, here's a link

If you use the CSV library to create your CSV file, then your file should be exported once you call the CSVEExportFinalize() function. You must have called CSVEExportInitialize(filepath,columns,exportcolumsHeadings) first

For any file, it 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();

VB

Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("c:\testfile.txt", True)
MyFile.WriteLine("This is a test.")
MyFile.Close

Microsoft Reference

Mart10
  • 758
  • 3
  • 14
  • He's focusing on the call that asks for the filename! – qwerty_so Jun 22 '17 at 19:02
  • @ThomasKilian eh didnt pay too much attention to his code, thought he was trying to save the file, not getting the actual path... will edit my answer – Mart10 Jun 22 '17 at 19:04
  • Iit is possible to do this in JavaScript - use var fso = new COMObject("Scripting.FileSystemObject"); – wikitect Jul 03 '21 at 13:37
1

GetFileNameDialog is a new function in V13. Try different parameters for the OpenOrSave parameters (the docu might be flawed as well). Sparx always ships banana software! If it doesn't work either, send a bug report.

Alternatively (preferred!) use the operation pointed out by @Hue.

qwerty_so
  • 35,448
  • 8
  • 62
  • 86