Page 1 of 1

Generate filename based on current date & time.

Posted: Fri Sep 25, 2015 2:43 pm
by Dav
This little snippet generates a filename based on the current date and time. Can be useful for apps that save series of files, like an image saver, or text saving program. An example is:: 2015-08-01-09-12-50.txt

I use it for a notepad app (that's why I added .txt to the end of filename). Maybe someone can use it.

- Dav

Code: Select all

'datefilename.txt
'generates a filename based on date & time
'example: 2015-08-01-09-12-50.txt
'by Dav


print datename$

end


def datename$()
  'make a name based on date & time
  'example: 2015-08-01-09-12-50.txt
  fd$=current_year()&"-"
  fd$=fd$&pad$(str$(current_month()))&"-"
  fd$=fd$&pad$(str$(current_date()))&"-"
  fd$=fd$&pad$(str$(current_hour()))&"-"
  fd$=fd$&pad$(str$(current_minute()))&"-"
  fd$=fd$&pad$(str$(current_second()))&".txt"
  datename$=fd$
end def
def pad$(a$)
  'adds leading 0 to string if len=1
  if len(a$)=1 then pad$="0"&a$ else pad$=a$
end def

Re: Generate filename based on current date & time.

Posted: Fri Sep 25, 2015 8:52 pm
by Mr. Kibernetik
It is not necessary to convert number to string with str$() function when combining strings.
For example it is correctly to write:

a$ = 5 & " and " & 3

Smart BASIC will understand what you want to get.

Re: Generate filename based on current date & time.

Posted: Sat Sep 26, 2015 12:06 am
by rbytes
I think this will be a lot more useful than my current system, which just numbers the files so they don't overwrite each other. Thanks.