Recent

Author Topic: Set the timestamp of a directory  (Read 2318 times)

jb57

  • Newbie
  • Posts: 3
Set the timestamp of a directory
« on: January 26, 2022, 07:34:42 am »
With freepascal, can I set the date of a folder (directory)?

I can set the date of a file:

Code: Pascal  [Select][+][-]
  1. USES dos;
  2. VAR i:longint; fnam:string; dt:datetime; f:file;
  3. BEGIN;
  4.   fnam:='C:\MyFolder\MyFile.txt';
  5.   assign(f,fnam); {$I-}reset(f){$I-};
  6.   if ioresult=0 then begin
  7.     dt.year:=2022; dt.month:=1; dt.day:=26; dt.hour:=7; dt.min:=20; dt.sec:=0;
  8.     packtime(dt,i);
  9.     setftime(f,i); writeln('done');
  10.   end;
  11. END.
  12.  

And I can GET the date of a folder:

Code: Pascal  [Select][+][-]
  1. USES dos, SysUtils;
  2. VAR i:longint; fnam:string; dt:datetime; sr:TSearchRec;
  3. BEGIN;
  4.   fnam:='C:\MyFolder';
  5.   if FindFirst(fnam, faAnyFile or faDirectory, sr) = 0 then begin
  6.     i:=sr.Time; FindClose(sr); unpacktime(i,dt);
  7.     write('Timestamp of '+fnam+' is: ');
  8.     writeln(dt.year,'-',dt.month,'-',dt.day,' ',dt.hour,':',dt.min,':',dt.sec);
  9.   end;
  10. END.
  11.  

But I found nothing to SET the date of a folder...

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Set the timestamp of a directory
« Reply #1 on: January 26, 2022, 09:25:28 am »
Use FileSetDate from the SysUtils unit. The functions in the DOS unit are only intended for working with files.

BobDog

  • Sr. Member
  • ****
  • Posts: 394
Re: Set the timestamp of a directory
« Reply #2 on: January 26, 2022, 02:12:01 pm »

Is this of any use to you.
Everything in main program for clarity.
Create a folder somewhere as a tester.
Code: Pascal  [Select][+][-]
  1. uses
  2. windows;
  3. var
  4. hfind:HANDLE;
  5. CurrentTime:FILETIME ;
  6. PathName:pchar;
  7. ans:systemtime;
  8. new:systemtime;
  9. begin
  10.  
  11. PathName:='C:\Users\Computer\Desktop\fb\pascal\mystuff\tester';
  12.  
  13.  
  14. hFind := CreateFile( PathName, GENERIC_WRITE,FILE_SHARE_WRITE,nil,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,0);
  15.  
  16. GetFileTime(hFind, @CurrentTime,nil,Nil);
  17. CloseHandle(hFind);
  18.  
  19. FileTimeToSystemTime(Currenttime,ans);
  20. writeln('folder details:');
  21. writeln('year ',ans.year);
  22. writeln('month ',ans.month);
  23. writeln('day of week ',ans.dayofweek);
  24. writeln('hour ',ans.hour);
  25. writeln('minute ',ans.minute);
  26. writeln('second ',ans.second);
  27. writeln;
  28.  
  29.  
  30. hFind := CreateFile( PathName, GENERIC_WRITE,FILE_SHARE_WRITE,nil,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,0);
  31.  
  32.  GetSystemTime(&new);
  33.  SystemTimeToFileTime(@new,@currenttime);
  34.  
  35.  
  36. writeln(SetFileTime(hFind, @CurrentTime, @CurrentTime, @CurrentTime));
  37.  
  38. CloseHandle(hFind);
  39.  
  40. GetFileTime(hFind, @CurrentTime,nil,Nil);
  41. CloseHandle(hFind);
  42. FileTimeToSystemTime(Currenttime,ans);
  43. writeln('new folder details:');
  44. writeln('year ',ans.year);
  45. writeln('month ',ans.month);
  46. writeln('day of week ',ans.dayofweek);
  47. writeln('hour ',ans.hour);
  48. writeln('minute ',ans.minute);
  49. writeln('second ',ans.second);
  50. readln;
  51. end.
  52.  
  53.  

jb57

  • Newbie
  • Posts: 3
Re: Set the timestamp of a directory
« Reply #3 on: January 28, 2022, 02:52:43 pm »
Use FileSetDate from the SysUtils unit.

Unfortunately, FileSetDate seems to not work. I tried the following:
Code: Pascal  [Select][+][-]
  1. USES SysUtils;
  2. VAR path,stamp:string; tdt:TDateTime;
  3. BEGIN;
  4.   path:='C:\MyFolder\'; stamp:='10.01.2000 14:34:56';
  5.   tdt:=StrToDateTime(stamp);
  6.   FileSetDate(path, DateTimeToFileDate(tdt));
  7. END.
  8.  
And it compiles and runs without error, but it does not set the folder date.
If I use it with a file "C:\MyFile.txt" instead of a folder "C:\MyFolder", then it works.

So I think the command is not for folders.

jb57

  • Newbie
  • Posts: 3
Re: Set the timestamp of a directory
« Reply #4 on: January 28, 2022, 02:58:19 pm »
Is this of any use to you.

Yes, this works for me  :) thank you.
I shortened it for my purposes, and this is working:
Code: Pascal  [Select][+][-]
  1. USES windows;
  2. VAR path:pchar; ft:filetime; st:systemtime; h:handle;
  3. BEGIN;
  4.   path:='C:\MyFolder\';
  5.   st.year:=2022; st.month:=2; st.day:=2; st.hour:=2; st.minute:=22; st.second:=0;
  6.   SystemTimeToFileTime(st,ft);
  7.   h:=CreateFile(path,GENERIC_WRITE,FILE_SHARE_WRITE,nil,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,0);
  8.   if SetFileTime(h,ft,ft,ft) then writeln('OK') else writeln('Fail');
  9.   CloseHandle(h);
  10. END.
  11.  

 

TinyPortal © 2005-2018