-2

i need to write a file with the filename containing the current date.. everything works besides the date, it gives a class exception 'run error(3)'

(the importo.text is the text of a TEdit.. but i guess it's irrelevant)

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

...

var
  contributo:real;
  f:textfile;
...
  datee: string;

...

contributo:= (StrToInt(importo.text)/ 100)*4;

  datee:= DateToStr(Date);

  assignfile(f,'fattura minimi n.'+n.text+' '+datee+'.txt');
  rewrite(f);

  writeln(f,'Giovanna Migliore');
  ...

  closefile(f);

1 Answers1

7

DateToStr() will return the date formatted according to regional settings. In your case this is almost certainly returning a folder/path delimiter character (/ or \) which is causing the problem (path not found).

Even if you change your regional settings to avoid using such characters, the code will still fail on other systems if those regional settings are not "compatible". To avoid this you need to ensure that your encoding of the date in the filename is not sensitive to such potential problems.

You could remove/replace any such characters after forming the filename, or you could explicitly encode the date in a manner that will not introduce such characters to start with, similar to:

var
  y, m, d: Word;

..

  DecodeDate(Date, y, m, d);
  dateStr := Format('%4d-%2d-%2d', [y, m, d]);

  // e.g. dateStr value for 31st Dec 2016 would be:  '2016-12-31'

You could then incorporate the date component values in your filename either by concatenation as required, or directly in a single format statement:

 filename := Format('fattura minimi n.%s %4d-%2d-%2d.txt [n.text, y, m, d]);
 assignfile(f, filename);
Deltics
  • 22,162
  • 2
  • 42
  • 70
  • 2
    You can use `FormatDateTime()` instead of using `DecodeDate()` and `Format()` separately: `dateStr := FormatDateTime('yyyy"-"mm"-"dd', Date);` and even something like this: `filename := FormatDateTime('"fattura minimi n.' + n.text + ' "yyyy"-"mm"-"dd".txt"', Date);` – Remy Lebeau Jan 19 '17 at 23:35
  • 3
    @David - you can't teach *anyone* *anything* if they don't want to learn and you are wasting your time trying. Note that this answer doesn't just give a solution to the problem at hand. It explains why there is a problem and the reasoning behind arriving at the solution. A **Q+A** site is about providing helpful *answers* that help people learn, not sending people packing just because they don't yet have the skills the rest of us enjoy. Helping someone to learn to fish does not have to involve refusing to help with their immediate hunger in the meantime. – Deltics Jan 20 '17 at 00:06
  • @Remy - I did say "similar to" [what follows] rather than suggesting I had The One True Way. There are indeed numerous ways to skin the feline and *personally* I find your alternatives to be less prefereable. Am I the only to have to remind myself whether Delphi uses **nn** rather than **MM** differentiate minutes and months ? And since the intent is to encode date components in a string, vs. "*format a date/time as a string*" *FormatDateTime* is simply arguably inappropriate or even 'incorrect'. in this case. Certainly it's no more "correct" in terms of solving OP's problem. – Deltics Jan 20 '17 at 01:20
  • @Remy I don't have a problem with `FormatDateTime()`, but I certainly wouldn't use `FormatDateTime(+, DateValue);` because you expose yourself to unintended substitutions in the `` portion of the string. – Disillusioned Jan 20 '17 at 05:47
  • 1
    It's fine to help like this. I do it plenty. I sometimes wonder whether there's a better way. Probably the asker still doesn't know how to debug such a problem. That has to be holding him back. These days an overwhelming majority of questions here are of this form. Asked by people who don't know how to debug. I wonder what would happen if they got no answer. Would the asker be forced to learn to debug. That's how it was when I started. Anyway, I'm just musing. – David Heffernan Jan 20 '17 at 06:42
  • @david first i did debug the code but did not make sense out of it. Second this site is so big that neither i or you can know it all.. I've spent 4 hours looking for a question that was already here. Third everybody has to start somewhere. And Deltics kindly not only gave me a solution but also an explanation. –  Jan 20 '17 at 06:49
  • 1
    @thomasmattia To add to what David has said. If you had debugged to the point of "FileName = `abc.txt` works but `abc20/01/2017.txt` doesn't" and mentioned as such in your question: then that would suggest effective debugging, but a gap in understanding of file-name specification. And probably a much better question that would have been more easily searchable. _On that note, "bad" questions tend to be harder to search because they're too specific to individual problems. Effective debugging should yield more information to help write a better question **and** search more effectively._ – Disillusioned Jan 20 '17 at 12:44