Tasp
Full Member
Posts: 215
|
Post by Tasp on May 22, 2020 11:24:24 GMT -5
So completely stuck on even starting this one.
I would like to create folders to store log files. I am currently making a new file everyday, which isn't ideal for my application.
The concept is to have the path as "\Log\2020\05\22\log.txt" then the following day that path would be "\Log\2020\05\23\log.txt"
The easy bit was grabbing the date into individual values.
GLOBAL CurrentDay$, CurrentMonth$, CurrentYear$
print CurrentDay$ Print CurrentMonth$ print CurrentYear$
END
SUB GetDate CurrentMonth$ = LEFT$(DATE$("mm/dd/yy"),2) CurrentDay$ = MID$(DATE$("mm/dd/yy"),4 ,2) CurrentYear$ = RIGHT$(DATE$("mm/dd/yy") ,2) END SUB
I understand I would need to use FILES, MKDIR etc to check for and create the directories. I just have no clue where to start with this, I've been literally sat here for quite some time spinning ideas but nothing!!
Any help, suggestions or pointers would be gratefully recieved!
|
|
|
Post by Chris Iverson on May 22, 2020 11:31:53 GMT -5
I fail to see how this would be easier than just naming the log file with the date.
You say you don't want to make a new file everyday, but that's exactly what you'd be doing with this method. However, you'd have far more complications arranging all the directories correctly and dealing with them. What's the difference between making a new file "log-2020-05-23.txt" and making a new file "log\2020\05\23\log.txt"? You're still making a new file every day, but then you have all of the overcomplicated infrastructure you're running into right now just to maintain the folders.
|
|
Tasp
Full Member
Posts: 215
|
Post by Tasp on May 22, 2020 11:37:44 GMT -5
Sorry, my brain is a bit frazzled with trying to make this. So I haven't explained this very well at all.
I do want to make a new file everyday.
But actually, I don't want a folder for each day I want a folder for each month of that year.
So, it would actually be "\Log\2020\05\" then the logfile will be the date.
It will be easier for a human to search for a particular days logs, rather than having 365+ files to look through after a years plus logs.
|
|
|
Post by Chris Iverson on May 22, 2020 12:01:09 GMT -5
Ok, that does make more sense. Although, if you name the files in the order you've specified(the same order I copied in my previous post), then searching for a particular log would be very easy. If you name the files with the whole date in the name, in the order Year-Month-Day, then when you sort the list of files by name, they'll also be arranged by date.
That's actually why I recommend doing exactly that. If you write the log file name as Month-Day-Year, then you'll be mixing up multiple years' log files together. By naming it Year-Month-Day, then no matter what you use to browse for the log files, they'll be in order.
Take a look at the files that get generated by this(you can just delete the directory after checking):
'02, 04, 06, 09, 11
print mkdir("testlog")
for x = 1 to 12 select case x case 2 numDays = 28 case 4,6,9,11 numDays = 30 case else numDays = 31 end select
for y = 1 to numDays month$ = right$("00" + str$(x), 2) day$ = right$("00" + str$(y), 2) fileName$ = "testlog\log_2020-" + month$ + "-" + day$ + ".txt" print fileName$
open fileName$ for output as #file close #file next y next x
|
|
Tasp
Full Member
Posts: 215
|
Post by Tasp on May 22, 2020 12:20:35 GMT -5
Yeah, I think you're right there.
This would also be easier if I was ever to create a log viewer in the future at some point.
I haven't done a huge amount with MKDIR either and I thought it attempt to overwrite an existing folder and wipe the contents. So really I don't have to check if the folder exists either.
Cheers Chris
|
|
|
Post by Chris Iverson on May 22, 2020 12:27:58 GMT -5
If you use this method, I do recommend using full YYYY years, and you'll want to make sure that you're always using double-digit numbers for the day and month, as I did in the code above. Zero-prefix the single-digit days and months, if needed.
If you're using your GetDate function, the only change I would make is to use date$("mm/dd/yyyy") instead of "mm/dd/yy" inside the function. Otherwise, that function would be fine to use.
Basically, the date order on filenames should always be YYYY-MM-DD.
|
|
Tasp
Full Member
Posts: 215
|
Post by Tasp on May 22, 2020 12:37:25 GMT -5
Yes good point. Need to use the full year.
What I've decided on is to generate folders for year only and date stamp the logfile YYYY-MM-DD
It's a shame that MKDIR cannot make more than 1 folder at a time.
The following doesn't create the folder, if you create "Log" folder manually then it works no problem.
GLOBAL CurrentDay$, CurrentMonth$, CurrentYear$
CALL GetDate
print CurrentDay$ print CurrentMonth$ print CurrentYear$
'LogPath$ = DefaultDir$ + "\Log\" + CurrentYear$ + "\" 'print LogPath$
LogPath$ = "Log" + CHR$(92) + "2020"
print mkdir(LogPath$)
END
SUB GetDate CurrentMonth$ = LEFT$(DATE$("mm/dd/yy"),2) CurrentDay$ = MID$(DATE$("mm/dd/yy"),4 ,2) CurrentYear$ = RIGHT$(DATE$("mm/dd/yyyy") ,4) END SUB
|
|
|
Post by Chris Iverson on May 22, 2020 14:43:13 GMT -5
Yeah, it looks like MKDIR is probably a wrapper around the CreateDirectory API call, which behaves the same way. (Errors out if directory already exists, does not create intermediate directories).
If you're willing to use APIs, you can use the SHCreateDirectoryExA() API call to create all intermediate directories as well as the final one. The only downside is needing to specify the full path.
folder$ = DefaultDir$ + "\testlogs\2020"
calldll #shell32, "SHCreateDirectoryExA",_ 0 as ulong,_ folder$ as ptr,_ 0 as ulong,_ ret as long
print ret
|
|
|
Post by honkytonk on May 23, 2020 5:26:24 GMT -5
I submit mine (Attention: put the ".bas" file in a folder, or else the folders and subfolders (etudes) will be balanced in bulk around the ".bas")
NOMAINWIN WindowWidth = 350: WindowHeight = 140 UpperLeftX = 100: UpperLeftY = 20 TEXTBOX #w.entr, 20 , 40, 200, 25 BUTTON #w.go, "Génère !", [go], UL, 240, 40, 65, 25
BUTTON #w.op, "Open", [open], UL, 275, 10, 50, 20 GRAPHICBOX #w.m 5, 5, 335, 100 OPEN "Help" FOR window_nf AS #w #w, "TRAPCLOSE [closeHelp]" #w.m "down;fill darkgreen;color white;backcolor darkgreen" #w.m "font courrier 12 bold" #w.m, "place 10 20": #w.m, "\ Numéro Affaire" #w.entr, "1" #w.m, "flush" wait [go] #w.entr, "!contents? var$" fold$="..\";var$ subun$="01 - Document de Base" full$=fold$;"\"; subun$ result = mkdir(fold$) '--------- result = mkdir(full$) if result=0 then #w.m, "place 10 90": #w.m, "\ Folder: ";chr$(34);var$;chr$(34);" was created" #w.m, "flush" end if '-------- subdeu$="02 - Relevés" full$=fold$;"\"; subdeu$ result = mkdir(full$) '----- subtroi$="03 - Photos" full$=fold$;"\"; subtroi$ result = mkdir(full$) '------- subqua$="04 - Etudes" full$=fold$;"\"; subqua$ result = mkdir(full$) '******************** subsubun$="A - Doc en cours" full$=fold$;"\"; subqua$;"\";subsubun$ result = mkdir(full$) '----- subsubdeu$="B - Doc pour approbation" full$=fold$;"\"; subqua$;"\";subsubdeu$ result = mkdir(full$) '----- subsubtro$="C - Doc en exécution" full$=fold$;"\"; subqua$;"\";subsubtro$ result = mkdir(full$) '----- subsubtro$="D - Doc à créer" full$=fold$;"\"; subqua$;"\";subsubtro$ result = mkdir(full$) '-------*************** subcin$="05 - Calculs": full$=fold$;"\"; subcin$: result = mkdir(full$) subsix$="06 - Courriers émis": full$=fold$;"\"; subsix$: result = mkdir(full$) subsep$="07 - Courriers reçus": full$=fold$;"\"; subsep$: result = mkdir(full$) subhui$="08 - Documentation": full$=fold$;"\"; subhui$: result = mkdir(full$) subneu$="09 - Plans de vérification": full$=fold$;"\"; subneu$: result = mkdir(full$) subdix$="10 - Divers": full$=fold$;"\"; subdix$: result = mkdir(full$) subonz$="11 - Demande de Travaux": full$=fold$;"\"; subonz$: result = mkdir(full$) subdou$="12 - CR Réunions": full$=fold$;"\"; subdou$: result = mkdir(full$) subtrei$="13 - Appels d'offres": full$=fold$;"\"; subtrei$: result = mkdir(full$) '-------- run "explorer.exe ..\";defaultDir$ wait [open] run "explorer.exe ..\";defaultDir$ wait [closeHelp] CLOSE #w END
|
|