ExpandVars uses characters that are invalid in filenames

delphi package - automated exception handling
Post Reply
obones
Posts: 66
Joined: Fri May 15, 2009 11:47 am

ExpandVars uses characters that are invalid in filenames

Post by obones »

Hello,

As I'm using madExcept in services, I want the bugreport to be automatically saved.
This can be easily configured, but this also means bugreports all end in the same file, or a unique file gets overwritten (which is my case).
To make things clearer and add a bit of uniqueness, I wanted to add a date/time part in the filename. The documentation wasn't clear about that but the source told me that the filename gets parsed by ExpandVars before being used.
So, looking inside that method, I see that there is %date%, %time% and %datetime% that are supported here.
Unfortunately, the time marker uses colons to separate the various time parts, but the colon character is forbidden in filenames, which leads to a bogus filename being generated on disc. In my case, it only keeps the millisecond part.
Short of modifying the source, is there a way for me to intervene on the bugreport final filename just before it gets used?

Regards,
Olivier
obones
Posts: 66
Joined: Fri May 15, 2009 11:47 am

Re: ExpandVars uses characters that are invalid in filenames

Post by obones »

Well, here is my workaround:

Code: Select all

procedure AdjustFileNameExceptHandler(const exceptIntf: IMEException; var handled: boolean);
var
  sysTime: TSystemTime;
  date, time: string;
begin
  GetLocalTime(sysTime);
  date := IntToStrExW(dword(sysTime.wYear        ), 4) + '-'  +
          IntToStrExW(dword(sysTime.wMonth       ), 2) + '-'  +
          IntToStrExW(dword(sysTime.wDay         ), 2);
  time := IntToStrExW(dword(sysTime.wHour        ), 2) + '.'  +
          IntToStrExW(dword(sysTime.wMinute      ), 2) + '.'  +
          IntToStrExW(dword(sysTime.wSecond      ), 2) + '.' +
          IntToStrExW(dword(sysTime.wMilliseconds), 1);

  exceptIntf.BugReportFile := ChangeFileExt(exceptIntf.BugReportFile, ' - '+ date + ' ' + time + ExtractFileExt(exceptIntf.BugReportFile));
end;

initialization
  RegisterExceptionHandler(AdjustFileNameExceptHandler, stDontSync, epQuickFiltering);
Basically, the same code as in madExcept itself, but I use dots instead of colons for every time elements.
This works but has the drawback that it must be done in every project, whereas I have a centralized configuration file for my services.
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Re: ExpandVars uses characters that are invalid in filenames

Post by madshi »

Will fix this in the next build by replacing ":" with "." after ExpandVars, when used for saving bug reports.
obones
Posts: 66
Joined: Fri May 15, 2009 11:47 am

Re: ExpandVars uses characters that are invalid in filenames

Post by obones »

Thanks, that will do it for time.
However, there are quite a few other variables that get expanded and their content may also contain other forbidden characters. Here is a reference:
https://msdn.microsoft.com/en-us/librar ... onventions

As this is "undocumented" territory, maybe it could be made to use something other than ExpandVars, with a subset of available variables and tailored for filenames.
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Re: ExpandVars uses characters that are invalid in filenames

Post by madshi »

There are not a whole lot of %vars% supported, and most of them are compatible with file names, so I think replacing : with . should do. In all the years of madExcept you're the first one who had a problem with this, anyway, so I don't think it's a wide spread problem.
obones
Posts: 66
Joined: Fri May 15, 2009 11:47 am

Re: ExpandVars uses characters that are invalid in filenames

Post by obones »

I believe I'm the first to report this because it does not seem to be documented that one can use %xx% variables in the bugreport filename.
And I must insist, to me there are other variables that can contain more than the colon, here is a list:

exceptMsg
bugReport
time
datetime
errorDetails

Hence my suggestion to have a special function just for filenames, but I agree this may lead to duplicated code.
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Re: ExpandVars uses characters that are invalid in filenames

Post by madshi »

time and dateTime are already covered. All other 3 can be very long (longer than MAX_PATH, and that's just for the file name!) and as such are totally unsuitable to be used as file names.
Post Reply