How to create a open Text Writer file name with a variable

How to create a open Text Writer file name with a variable
0

#1

I have a script that creates a csv. It works fine. My only issue is that I need the file name to have a date at the end of it so it doesn’t get over written each month. I currently have something like this -

var fileNameForExport = (“BankSummaryFee + Math.round(new Date().getTime()/1000)”);

var fileOut = openTextWriter(“c:\out\fileNameForExport.csv”);

I have played around with this and this is where I ended up. My results is simply fileNameForExport.csv.

Can any one help me??
Thank You


#2

Can you do something like this

var fileNameForExport = (“BankSummaryFee + Math.round(new Date().getTime()/1000)”);
var filepath = "C:\out\";
var fileext = ".csv";

var filePathname = filepath + fileNameForExport + fileext;

var fileOut = openTextWriter(filePathname);

#3

You can also use a template literal.

var fileNameForExport = (“BankSummaryFee + Math.round(new Date().getTime()/1000)”);

var fileOut = openTextWriter(`c:\out\${fileNameForExport}.csv`); // note that double quotes have been changed to back ticks

#4

I have tried both responses above. Neither of them work. I don’t get any errors, just no output. I actually a had to change it to something like this to get the file.

var fileNameForExport = (“BankSummaryFee + Math.round(new Date().getTime()/1000)”);
var fileOut = openTextWriter(“c:\out\fileNameForExport.csv”);

This literally gives a csv file called fileNameForExport. It’s like it isn’t recognizing the variables.


#5

That’s because you’ve got the filename as a string literal which will not interpolate variables. You can copy and paste the code from either post above and it will work.


#6

I did copy and paste from both posts. I did not get any output file at all. It seems as though the only way I get any out put is if I use - var fileOut = openTextWriter(“c:\out\ ( then literally names file whatever you put here) .csv


#7

I am using Planet Press Connect ( if anyone is familiar with this). I’m not sure if this makes a difference.


#8

I think the problem with @owel’s solution is that the backslash escapes the double quote:

var fileNameForExport = (“BankSummaryFee + Math.round(new Date().getTime()/1000)”);
var filepath = "C:\out\"; <--  right here
var fileext = ".csv";

I think it should be:

var fileNameForExport = (“BankSummaryFee + Math.round(new Date().getTime()/1000)”);
var filepath = "C:\out\\"; 
var fileext = ".csv";

Now the backslash escapes the backslash not the string. Somewhat confusing, but you can tell by the text highlighting the difference.


#9

@PortableStick’s solution has an error with quotes too.

var fileNameForExport = (“BankSummaryFee + Math.round(new Date().getTime()/1000)”);

should be

var fileNameForExport = ("BankSummaryFee" + Math.round(new Date().getTime()/1000));

@Leftee, Please your show full code so we can see any typos you are making.


#10

My full code is below - please note that if I use var fileOut = openTextWriter(c:\out\BankSummary.csv); It works and I get a file. If I use the below code I do not get a file output.


var fileNameForExport = ("BankSummaryFee" + Math.round(new Date().getTime()/1000));
var filepath = "C:\out\\"; 
var fileext = ".csv";
var filePathname = "filepath + fileNameForExport + fileext";
var fileOut = openTextWriter(filePathname);
var str = [], oneRec, twoRec, prop;

// Add Column names as first line in CSV
oneRec = data.records[0];
twoRec = oneRec.tables["APVendor"];
for (prop in oneRec.fields) {
   str.push('"' + prop + '"');
}
for (prop in twoRec[0].fields) {
   str.push('"' + prop + '"');
}

fileOut.write(str.join(","));
fileOut.newLine();

str = [];
// Add all values for all records
for (var i = 0; i < data.records.length; i++) {
   oneRec = data.records[i];
   twoRec = oneRec.tables["APVendor"];
   for (var j=0;j<twoRec.length;j++){
      for (prop in oneRec.fields) {
         str.push('"' + oneRec.fields[prop] + '"');
      }
      for (prop in twoRec[j].fields) {
         str.push('"' + twoRec[j].fields[prop] + '"');
      }
      fileOut.write(str.join(","));
      fileOut.newLine();
      str = [];
   }
}
//Close file
fileOut.close();

#11

:sob: I copy/pasted that bit without checking. Also, the backslash in my template literal is escaping the $ that makes the template work. Adding another slash puts two slashes in the final string. Bummer. Owel’s solution is much better in this case.


#12

Take out the quotes.

var filePathname = filepath + fileNameForExport + fileext;

#13

Just noticed another problem. This is not valid JS. What is the goal here?


#14

OK - I got something !!! I am getting - Bank SummaryFee1506625976.csv


#15

I am working in Planet Press Connect. While they say it works off Javascript, It doesn’t always seem to follow the same rules. I had a very hard time with this one but with the code that I provided it gives the exactly waht I need.

I am taking an xml file I am using for the data and in my worklfow, before it goes goes to a pdf I am creating a csv file.


#16

@PortableStick working with file names is always a pain in the butt :joy:


#17

Thank you everyone for all your help !!! I really appriciate it!!!