Author Topic: Change date to file  (Read 2698 times)

0 Members and 1 Guest are viewing this topic.

Offline krovit

  • Forum Regular
  • Posts: 179
    • View Profile
Change date to file
« on: July 26, 2021, 12:33:26 pm »
Maybe it's the timestamp?
On the forum there is very little on the subject, or I do not know what is the keyword to search for.
I don't know...

I would like to change the dates to the file (creation date, last modification, last access).
These values do not seem to exist in "as it is" inside the file and are not available with OPEN BINARY.
Probably are written in another format.

I tried to look for them in UNIX TIME but it does not seem to be the right method.

All this I need for PDF files.
The dates I'm talking about are not metadata (which in PDF files are easily identifiable and editable).

I would like to do with QB64 - if is it possibile - something of what Attribute Changer very good does (https://www.petges.lu)


Any suggestions?

(Sorry, if I knew how to use WinAPI I would probably solve many problems...)



Tank you (beforehand)


« Last Edit: July 26, 2021, 12:40:43 pm by krovit »
Nothing is easy, especially when it appears simple (and nothing could be as dangerous as trying to do good to others)

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Change date to file
« Reply #1 on: July 26, 2021, 12:43:21 pm »
@krovit There is a way to do this with WinAPI code. I am trying to write up a small sample for you ;)
Shuwatch!

Marked as best answer by krovit on July 26, 2021, 10:39:16 pm

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Change date to file
« Reply #2 on: July 26, 2021, 01:23:32 pm »
@krovit Here you go. This will make a file called "filetest.txt" and then set the last modified time to my birthdate.

Code: QB64: [Select]
  1.  
  2. Const GENERIC_READ = &H80000000
  3. Const GENERIC_WRITE = &H40000000
  4.  
  5. Const FILE_ATTRIBUTE_NORMAL = &H00000080
  6.  
  7. Const CREATE_NEW = 1
  8. Const OPEN_EXISTING = 3
  9.  
  10. Type SYSTEMTIME
  11.     As _Unsigned Integer wYear, wMonth, wDayOfWeek, wDay, wHour, wMinute, wSecond, wMilliseconds
  12.  
  13. Type FILETIME
  14.     As _Unsigned Long dwLowDateTime, dwHighDateTime
  15.  
  16.     Function CreateFile%& (lpFileName As String, Byval dwDesiredAccess As _Unsigned Long, Byval dwShareMode As _Unsigned Long, Byval lpSecurityAttributes As _Offset, Byval dwCreationDisposition As _Unsigned Long, Byval dwFlagsAndAttributes As _Unsigned Long, Byval hTemplateFile As _Offset)
  17.     Sub SystemTimeToFileTime (ByVal lpSystemTime As _Offset, Byval lpFileTime As _Offset)
  18.     Sub SetFileTime (ByVal hFile As _Offset, Byval lpCreationTime As _Offset, Byval lpLastAcessTime As _Offset, Byval lpLastWriteTime As _Offset)
  19.     Sub CloseHandle (ByVal hObject As _Offset)
  20.  
  21. Dim As SYSTEMTIME sys
  22. Dim As FILETIME modifiedtime
  23.  
  24. Dim As _Offset hFile
  25.  
  26. sys.wYear = 1995
  27. sys.wMonth = 12
  28. sys.wDayOfWeek = 6
  29. sys.wDay = 30
  30.  
  31. hFile = CreateFile("filetest.txt", GENERIC_READ Or GENERIC_WRITE, 0, 0, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0)
  32. If hFile <> -1 Then 'If the function fails, the file might already exist.
  33.     SystemTimeToFileTime _Offset(sys), _Offset(modifiedtime)
  34.     SetFileTime hFile, _Offset(modifiedtime), _Offset(modifiedtime), _Offset(modifiedtime)
  35.     hFile = CreateFile("filetest.txt", GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0) 'We open the existing file, maybe.
  36.     If hFile <> -1 Then
  37.         SystemTimeToFileTime _Offset(sys), _Offset(modifiedtime)
  38.         SetFileTime hFile, _Offset(modifiedtime), _Offset(modifiedtime), _Offset(modifiedtime)
  39.     End If
  40.  
  41. CloseHandle hFile
« Last Edit: July 26, 2021, 01:26:56 pm by SpriggsySpriggs »
Shuwatch!

Offline krovit

  • Forum Regular
  • Posts: 179
    • View Profile
Re: Change date to file
« Reply #3 on: July 27, 2021, 02:30:40 am »
Thank you SpriggsySpriggs!

More than Superman (Spriggsy's youtube Channel) you are Mandrake that from the top hat (WinAPI) always pulls out a rabbit (routine)!...



« Last Edit: July 27, 2021, 02:31:56 am by krovit »
Nothing is easy, especially when it appears simple (and nothing could be as dangerous as trying to do good to others)