Creating a unique filename for Excel exports

Find out how to get things done with Servoy. Post how YOU get things done with Servoy

Creating a unique filename for Excel exports

Postby Bernd.N » Fri Mar 10, 2017 1:05 am

Users love exporting their data to Excel. That is faster when not always asking the user for a filename, but creating it right away.
As a user often exports more than one time, it is handy to let the system create unique filenames.
Because otherwise the second export attempt will fail when trying to export with the same filename, while the first Excel file is open in Excel.

The reason to include also milliseconds is that the user will be able to export twice in the same minute, without producing the same file name.

Code: Select all
/**
* Returns a unique filename like sFileName_7.3.2017_1202.238
*
* @author Gary, Bernd
* @param   {String} sFileName
* @returns {String}
* @public
* @properties={typeid:24,uuid:"C5F48A7D-5E83-4559-815E-06FC9C31FF3A"}
*/
function getUniqueFilename(sFileName) {
   
   var
      sExportPath = "",
      sFilePath   = "",
      dDate  = new Date(),
      iMonth = dDate.getMonth() + 1;
   
   // If the user has set a standard output path, then take that
   if (scopes.utils.stringHasContent(_to_employee$currentpersonid.e_standard_output_path)) {

      sExportPath = _to_employee$currentpersonid.e_standard_output_path;
      
   } else {

      sExportPath = 'c:\\tmp';
   }
   
   sFilePath =
      sExportPath + '\\' + sFileName + '_' +
      dDate.getDate().toString() + '.' +
      iMonth.toString() + '.' +
      dDate.getFullYear().toString() + '_' +
      (dDate.getHours().toString().length   === 1 ? '0' : '') + dDate.getHours().toString() +
      (dDate.getMinutes().toString().length === 1 ? '0' : '') + dDate.getMinutes().toString() + '.' +
      dDate.getMilliseconds().toString()
      
   return sFilePath;
}
Bernd Korthaus
LinkedIn
Servoy 7.4.9 SC postgreSQL 9.4.11 Windows 10 Pro
User avatar
Bernd.N
 
Posts: 544
Joined: Mon Oct 21, 2013 5:57 pm
Location: Langenhorn, North Friesland, Germany

Re: Creating a unique filename for Excel exports

Postby sbutler » Fri Mar 10, 2017 4:28 am

Normal convention is to write to the users system temp folder, and Java already has a built-in cross-platform mechanism for creating unique temp files. In addition, the temp files get deleted automatically when your application closes. This allows the user to save the file to a separate location if they want their own copy, or the program will automatically cleanup after itself.

Example:

Code: Select all
plugins.file.createTempFile('myfile','.txt')


You'll get a file with a unique file name that looks something like myFile234134341334.txt

I suppose with your setup you let each user setup their own output directory, but that seems like it would fill up quick and become a bit of a maintenance nightmare.
Scott Butler
iTech Professionals, Inc.
SAN Partner

Servoy Consulting & Development
Servoy University- Training Videos
Servoy Components- Plugins, Beans, and Web Components
Servoy Guy- Tips & Resources
ServoyForge- Open Source Components
User avatar
sbutler
Servoy Expert
 
Posts: 759
Joined: Sun Jan 08, 2006 7:15 am
Location: Cincinnati, OH

Re: Creating a unique filename for Excel exports

Postby Bernd.N » Fri Mar 10, 2017 8:45 am

Hi Scott,
thanks for the hint to that function, and it is indeed a good idea to right away add a second parameter for the file name ending.

There will be users who would not want that their temporary (Excel) files get deleted automatically.
And I guess most users have an own temp folder on their environment, which they know how to handle or cleanup, so maintenance should not be a big problem.

The temp folder Windows provides is usually C:\Users\<UserName>\AppData\Local\Temp, that is too laborious to reach for my taste.
Bernd Korthaus
LinkedIn
Servoy 7.4.9 SC postgreSQL 9.4.11 Windows 10 Pro
User avatar
Bernd.N
 
Posts: 544
Joined: Mon Oct 21, 2013 5:57 pm
Location: Langenhorn, North Friesland, Germany

Re: Creating a unique filename for Excel exports

Postby sbutler » Fri Mar 10, 2017 4:21 pm

Bernd.N wrote:The temp folder Windows provides is usually C:\Users\<UserName>\AppData\Local\Temp, that is too laborious to reach for my taste.


Usually the users don't need to ever go do that directly. You would open the file for them automatically. They run a report, then it opens automatically in their default application for that file extension. So, if it were excel, excel would open, if they then decided they wanted to save a it, they could choose to save it wherever they wanted, or just close it and it deletes and cleans up when their session ends. This would save users a few clicks as the reports just open for them automatically. They don't need to go find them and open them.

Example below. I have a few variations of this, but here is one I just dug up quickly.

Code: Select all
function openFile(filePath){
   var vOS = application.getOSName();
   if(utils.stringPatternCount(vOS, 'Windows') == 1)
   {
      application.executeProgram( 'rundll32', 'url.dll,FileProtocolHandler', filePath );
   }
   else if(utils.stringPatternCount(vOS, 'FreeBSD') == 1 || utils.stringPatternCount(vOS, 'Linux') == 1)
   {
      application.executeProgram('mozilla', filePath );
   }
   else if(utils.stringPatternCount(vOS, 'Mac') == 1)
   {
      application.executeProgram('open', filePath );
   }
}
Scott Butler
iTech Professionals, Inc.
SAN Partner

Servoy Consulting & Development
Servoy University- Training Videos
Servoy Components- Plugins, Beans, and Web Components
Servoy Guy- Tips & Resources
ServoyForge- Open Source Components
User avatar
sbutler
Servoy Expert
 
Posts: 759
Joined: Sun Jan 08, 2006 7:15 am
Location: Cincinnati, OH

Re: Creating a unique filename for Excel exports

Postby Bernd.N » Fri Mar 10, 2017 4:37 pm

Yes, we fire application.executeProgram() too. Thanks for your sample code which covers all operation systems.

utils.stringPatternCount() is new for me. We should all share more code to learn from each other. :)
Bernd Korthaus
LinkedIn
Servoy 7.4.9 SC postgreSQL 9.4.11 Windows 10 Pro
User avatar
Bernd.N
 
Posts: 544
Joined: Mon Oct 21, 2013 5:57 pm
Location: Langenhorn, North Friesland, Germany

Re: Creating a unique filename for Excel exports

Postby sbutler » Fri Mar 10, 2017 4:48 pm

That one is probably a bad example I shouldn't have shared :) A lot of the utils.* stuff are ports of FileMaker functions from the early days to make it easier for them to switch. stringPatternCount is an example. In new code that I write, I tend to avoid those and use the pure JavaScript version instead.
Scott Butler
iTech Professionals, Inc.
SAN Partner

Servoy Consulting & Development
Servoy University- Training Videos
Servoy Components- Plugins, Beans, and Web Components
Servoy Guy- Tips & Resources
ServoyForge- Open Source Components
User avatar
sbutler
Servoy Expert
 
Posts: 759
Joined: Sun Jan 08, 2006 7:15 am
Location: Cincinnati, OH

Re: Creating a unique filename for Excel exports

Postby patrick » Fri Mar 10, 2017 6:43 pm

Stuff like this is also available in the svyUtils suite of helper methods. in scopes.svyIO you would find

Code: Select all
openFileWithDefaultViewer(file)


which also takes care of different OSes (code is basically the same as Scott showed).
Patrick Ruhsert
Servoy DACH
patrick
 
Posts: 3703
Joined: Wed Jun 11, 2003 10:33 am
Location: Munich, Germany

Re: Creating a unique filename for Excel exports

Postby j0rdan23turn3r » Fri Apr 16, 2021 2:21 pm

Thanks for the utils.stringPatternCount(). This one solved my problem!
Jordan Turner,
Full Stack Developer at

Image
j0rdan23turn3r
 
Posts: 1
Joined: Fri Apr 16, 2021 1:50 pm


Return to How To

Who is online

Users browsing this forum: No registered users and 5 guests

cron