Create a log file if it doesn't already exist

Posted by Sean Randall on Sat 20 Sep 2008 08:24 PM — 4 posts, 18,211 views.

United Kingdom #0
Hello,

I've never needed to log my output before but now find myself in the position of requiring dated text files.

I have set up the world configuration logging dialog to my needs, but I get an error like this on connection...
'Could not open log file "logs\Sep 2008\Saturday 20.txt".'

Obviously the problem is that neither the folder nor file exist. Logs does, but that's about it. I should have foreseen this one - I'm just so used to everything from IRC clients to IM software creating the folders and files for you!

Does anybody have any tips on what I can do? obviously it'd be easy enough to make each file manually as I go along: is there something I can add to worldOpen to make them for me? or am I making it more difficult than it needs to be and missing something obvious?

Sean.
Australia Forum Administrator #1
It will create the file for you, naturally, but it won't create folders. You could script folder creation with os.execute in Lua, but probably the simplest is to make it unnecessary. Just create a file called:


"logs\Sep 2008_Saturday 20.txt"



Later on, if you wanted to (ie. each month) you could move batches of files into their own folders, based on the first part of the file name.
United Kingdom #2
Thank you for that.

I almost started to think "oh, I need to CHMOD this folder first". Too much PHP on the brain for me. i'll do it that way, though; with a script to move them monthly. I haven't seen the note anywhere that it'll make files but not folders, good to know!

Thanks. Again!
Sean.
Australia Forum Administrator #3
If you are using auto-logging you are probably best off using %Lfilename, because if you change your current directory (eg. by browsing) the logs might not end up where you expect. (Click on the ? button - %L is the log file directory, including the trailing backslash).


I just tried to save a file in notepad where I added a non-existent directory to the start of the filename - it gave an error "path does not exist" which is the same behaviour as MUSHclient.