Author Topic: Program to set the date and time of a file  (Read 1818 times)

0 Members and 1 Guest are viewing this topic.

chrisc

  • Guest
Program to set the date and time of a file
« on: April 24, 2018, 04:36:09 AM »
Hello

this is a program that allows you to set the date and time of a specified file

just place in any txt file called "myfile.txt"  in the same folder as the compile program Set_FileTime.exe
and run this exe, it will reset the date to Apr 18 2018 and time 6:55 pm

if you find any problem with this code please feel free to modify it and let us know about it.  Thanxx

Code: [Select]
' Set_FileTime.o2bas
' Set the time and date of a file
$ filename "Set_FileTime.exe"
uses RTL64


uses corewin
uses Timeutil

' https://msdn.microsoft.com/en-us/library/windows/desktop/aa365282(v=vs.85).aspx
typedef struct _OFSTRUCT {
  BYTE cBytes;
  BYTE fFixedDisk;
  WORD nErrCode;
  WORD Reserved1;
  WORD Reserved2;
  CHAR szPathName[OFS_MAXPATHNAME];
} OFSTRUCT, *POFSTRUCT;



'=====================================================
' SetFileDate - Set the time and date of a file
'
SUB SetFileDate(sFileName AS STRING, _
                iYear     AS LONG, iMonth  AS LONG, iDay    AS LONG, _
                iHour     AS LONG, iMinute AS LONG, iSecond AS LONG) EXPORT


    LOCAL hFile          AS  sys
    LOCAL CreationTime   AS FILETIME
    LOCAL LastAccessTime AS FILETIME
    LOCAL LastWriteTime  AS FILETIME
    LOCAL gfSyT             AS SYSTEMTIME
    LOCAL gfLT             AS FILETIME
    LOCAL gfCT             AS FILETIME
    LOCAL gfLAT            AS FILETIME

    LOCAL lpReOpenBuff   AS OFSTRUCT

   hFile = CreateFile(sFileName + CHR$(0), BYVAL %GENERIC_READ OR %GENERIC_WRITE, _
                    BYVAL 0, BYVAL %NULL, BYVAL %OPEN_ALWAYS, _
                    BYVAL %FILE_ATTRIBUTE_NORMAL, BYVAL %NULL)

    IF hFile THEN
        FileTimeToLocalFileTime LastWriteTime, gfLT
        FileTimeToSystemTime gfLT, gfSyT

        FileTimeToLocalFileTime  CreationTime, gfCT
        FileTimeToLocalFileTime  LastAccessTime, gfLAT

        gfSyT.wYear   = iYear
        gfSyT.wMonth  = iMonth
        gfSyT.wDay    = iDay
        gfSyT.wHour   = iHour
        gfSyT.wMinute = iMinute
        gfSyT.wSecond = iSecond

        SystemTimeToFileTime gfSyT, gfLT
        LocalFileTimeToFileTime gfLT, LastWriteTime

        SystemTimeToFileTime gfSyT, gfLAT
        LocalFileTimeToFileTime gfLAT, LastAccessTime
        SystemTimeToFileTime gfSyT, gfCT
        LocalFileTimeToFileTime gfCT, CreationTime

        SetFileTime BYVAL hFile, CreationTime, LastAccessTime, LastWriteTime
        CloseHandle hFile
    END IF

END SUB   


'========================
' Main start
string gProgNam

' Set the time of a specific file to a particular date and time
 gProgNam = "myfile.txt"
SetFileDate(gProgNam, 2018,04,18,18,55, 22)

print "done"

Arnold

  • Guest
Re: Program to set the date and time of a file
« Reply #1 on: April 24, 2018, 09:19:26 AM »
Thank you Chris, for sharing this routine which makes sense and is useful in some cases. It can be run in JIT mode and be compiled as 32-bit and 64-bit executable. This is why I would prefer a notation like this:

$ filename "Set_FileTime.exe"

'uses RTL32
'uses RTL64

Although this is only a matter of taste, OxygenBasic usually supports all three modes, maybe this will change. But as long as it is possible I think it is a nice feature to run a program directly from the editor without the need to compile it to an executable. If everything works correctly then an exe file can be created anyway. Nevertheless I like your app very much.

Roland

chrisc

  • Guest
Re: Program to set the date and time of a file
« Reply #2 on: April 24, 2018, 11:49:22 AM »
Good to hear that you like it Roland.  i 'll do my best to convert our existing PB 32bit software
to O2 and will publish their solutions in this forum.

as my company is converting from PB 32bit to O2 64bit,  i only use  RTL64
and i have to make sure that all the executables be able to run in 64bit environment.

and that we prefer to use notepad rather than oxide,  and we find that o2 is a very versatile
and flexible programming language,  it can also read c++ headers.  i think Charles is one
of the best compiler writer in the world.

JRS

  • Guest
Re: Program to set the date and time of a file
« Reply #3 on: April 24, 2018, 05:39:15 PM »
It will be good for O2 that someone was successful at porting PB to O2. Maybe others will give it a shot.