I am writing some data to a .txt file. I am using Open[] to create the text file in the directory I want and then Write[] and WriteString[]. (I am using FileNameJoin[] to create the actual filenames). I tried this out using a test file, called test.txt (and stored in the appropriate directory), it opens, writes to, and closes the file how I want. However, every time the notebook is run, I want to create a new output .txt file, so the files are named based on the date and time (in 24 hours), so part of the filename looks like HH:MM:SS. So, I am running something that looks like:
filename =
FileNameJoin[{"C:", "Users", "Me", "Dir1", "Dir2",
"2019-07-0810:59:01.txt"}];
f=OpenWrite[filename]
Mathematica gives this error:
OpenWrite::noopen: Cannot open C:\Users\Me\Dir1\Dir2\2019-07-0810:59:01.txt.
I think what it doesn't like is the colons, since if I replace : with ; it opens/creates the file just fine (and it can open a file with just the date as the name). However, this is kind of ugly, and I would really prefer to use : instead.
Any suggestions are appreciated! Thanks you guys!
StringReplace[DateString[], h : (_ ~~ _) ~~ ":" ~~ m : (_ ~~ _) ~~ ":" ~~ s : (_ ~~ _) :> h ~~ "h" ~~ m ~~ "m" ~~ s ~~ "s"]
. It solves a second problem too : sometimes (depending on the operating system) the seconds have decimals (10.45 for instance) $\endgroup$