• Welcome to Jose's Read Only Forum 2023.
 

File Management Functions Examples

Started by José Roca, August 29, 2011, 03:19:26 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca

 
A file object provides a representation of a resource (either a physical device or a resource located on a physical device) that can be managed by the I/O system. Like other objects, they enable sharing of the resource, they have names, they are protected by object-based security, and they support synchronization. The I/O system also enables reading from or writing to the resource.

José Roca

 
The following code illustrates the use of the GetBinaryType function.


#COMPILE EXE
#DIM ALL
#INCLUDE "windows.inc"

FUNCTION PBMAIN () AS LONG

   LOCAL szApplicationName AS ASCIIZ * %MAX_PATH
   LOCAL dwBinaryType AS DWORD
   LOCAL strMsg AS STRING

   szApplicationName = "c:\windows\explorer.exe"
   IF GetBinaryType(szApplicationName, dwBinaryType) <> 0 THEN
      strMsg = "The selected file is "
      SELECT CASE dwBinaryType
         CASE %SCS_32BIT_BINARY
            strMsg = strMsg + " a 32-bit Windows-based application"
         CASE %SCS_DOS_BINARY
            strMsg = strMsg + " an MS-DOS – based application"
         CASE %SCS_OS216_BINARY
            strMsg = strMsg + " a 16-bit OS/2-based application"
         CASE %SCS_PIF_BINARY
            strMsg = strMsg + " PIF file that executes an MS-DOS – based application"
         CASE %SCS_POSIX_BINARY
            strMsg = strMsg + " POSIX – based application"
         CASE %SCS_WOW_BINARY
            strMsg = strMsg + " a 16-bit Windows-based application"
      END SELECT
   ELSE
      strMsg = "The selected file is not an executable"
   END IF

   ? strMsg
   #IF %DEF(%PB_CC32)
      WAITKEY$
   #ENDIF

END FUNCTION


José Roca

 
The following example uses the GetFileTime function to retrieve the last-write time for a file. It converts the time to local time based on the current time-zone settings, and creates a date and time string that can be shown to the user.

C++ Example


#include <windows.h>
#include <tchar.h>
#include <strsafe.h>

// GetLastWriteTime - Retrieves the last-write time and converts
//                    the time to a string
//
// Return value - TRUE if successful, FALSE otherwise
// hFile      - Valid file handle
// lpszString - Pointer to buffer to receive string

BOOL GetLastWriteTime(HANDLE hFile, LPTSTR lpszString, DWORD dwSize)
{
    FILETIME ftCreate, ftAccess, ftWrite;
    SYSTEMTIME stUTC, stLocal;
    DWORD dwRet;

    // Retrieve the file times for the file.
    if (!GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))
        return FALSE;

    // Convert the last-write time to local time.
    FileTimeToSystemTime(&ftWrite, &stUTC);
    SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);

    // Build a string showing the date and time.
    dwRet = StringCchPrintf(lpszString, dwSize,
        TEXT("%02d/%02d/%d  %02d:%02d"),
        stLocal.wMonth, stLocal.wDay, stLocal.wYear,
        stLocal.wHour, stLocal.wMinute);

    if( S_OK == dwRet )
        return TRUE;
    else return FALSE;
}

int _tmain(int argc, TCHAR *argv[])
{
    HANDLE hFile;
    TCHAR szBuf[MAX_PATH];

    if( argc != 2 )
    {
        printf("This sample takes a file name as a parameter\n");
        return 0;
    }
    hFile = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL,
        OPEN_EXISTING, 0, NULL);

    if(hFile == INVALID_HANDLE_VALUE)
    {
        printf("CreateFile failed with %d\n", GetLastError());
        return 0;
    }
    if(GetLastWriteTime( hFile, szBuf, MAX_PATH ))
        _tprintf(TEXT("Last write time is: %s\n"), szBuf);
       
    CloseHandle(hFile);   
}


PowerBASIC Example


#COMPILE EXE
#DIM ALL
#INCLUDE "windows.inc"

FUNCTION GetLastWriteTime (BYVAL hFile AS DWORD, BYREF strDateTime AS STRING) AS LONG

   LOCAL ftCreate, ftAccess, ftWrite AS FILETIME
   LOCAL stUTC, stLocal AS SYSTEMTIME

   ' // Retrieve the file times for the file.
   IF GetFileTime(hFile, ftCreate, ftAccess, ftWrite) = 0 THEN EXIT FUNCTION

   ' // Convert the last-write time to local time.
   FileTimeToSystemTime(ftWrite, stUTC)
   SystemTimeToTzSpecificLocalTime(BYVAL %NULL, stUTC, stLocal)

   ' // Build a string showing the date and time.
   IF ISMISSING(strDateTime) THEN EXIT FUNCTION
   strDateTime = FORMAT$(stLocal.wMonth) & "-" & FORMAT$(stLocal.wDay) & "-" & _
                 FORMAT$(stLocal.wYear) & " " & FORMAT$(stLocal.wHour) & ":" & _
                 FORMAT$(stLocal.wMinute)

   FUNCTION = %TRUE

END FUNCTION

FUNCTION PBMAIN () AS LONG

   LOCAL hFile AS DWORD
   LOCAL strBuf AS STRING
   LOCAL nError AS LONG

   ' // Change the file name as needed
   hFile = CreateFile("test.txt", %GENERIC_READ, %FILE_SHARE_READ, BYVAL %NULL, _
           %OPEN_EXISTING, 0, %NULL)

   IF hFile = %INVALID_HANDLE_VALUE THEN
      nError = GetLastError
      PRINT "CreateFile failed with ", nError
   ELSE
      IF GetLastWriteTime(hFile, strBuf) THEN
         PRINT "Last write time is: " & strBuf
      END IF
      CloseHandle(hFile)
   END IF
   WAITKEY$

END FUNCTION


José Roca

 
The following example sets the last-write time for a file to the current system time using the SetFileTime function.

The NTFS file system stores time values in UTC format, so they are not affected by changes in time zone or daylight saving time. The FAT file system stores time values based on the local time of the computer.

The file must be opened with the CreateFile function using FILE_WRITE_ATTRIBUTES access.

C++ Example


#include <windows.h>

// SetFileToCurrentTime - sets last write time to current system time
// Return value - TRUE if successful, FALSE otherwise
// hFile  - must be a valid file handle

BOOL SetFileToCurrentTime(HANDLE hFile)
{
    FILETIME ft;
    SYSTEMTIME st;
    BOOL f;

    GetSystemTime(&st);              // Gets the current system time
    SystemTimeToFileTime(&st, &ft);  // Converts the current system time to file time format
    f = SetFileTime(hFile,           // Sets last-write time of the file
        (LPFILETIME) NULL,           // to the converted current system time
        (LPFILETIME) NULL,
        &ft);   

    return f;
}


PowerBASIC Example


#INCLUDE "windows.inc"

'// SetFileToCurrentTime - sets last write time to current system time
'// Return value - TRUE if successful, FALSE otherwise
'// hFile  - must be a valid file handle

FUNCTION SetFileToCurrentTime (BYVAL hFile AS DWORD) AS LONG

   LOCAL ft AS FILETIME
   LOCAL st AS SYSTEMTIME
   LOCAL f AS LONG

   GetSystemTime(st)              ' // Gets the current system time
   SystemTimeToFileTime(st, ft)   ' // Converts the current system time to file time format
   f = SetFileTime(hFile, _       '  // Sets last-write time of the file
         BYVAL %NULL, _           '  // to the converted current system time
         BYVAL %NULL, _
         ft)
   FUNCTION = f

END FUNCTION

FUNCTION PBMAIN () AS LONG

   LOCAL hFile AS DWORD
   LOCAL strBuf AS STRING
   LOCAL nError AS LONG

   ' // Change the file name as needed
   hFile = CreateFile("test.bas", %FILE_READ_ATTRIBUTES OR %FILE_WRITE_ATTRIBUTES, _
           %FILE_SHARE_READ, BYVAL %NULL, %OPEN_EXISTING, 0, %NULL)

   IF hFile = %INVALID_HANDLE_VALUE THEN
      nError = GetLastError
      ? "CreateFile failed with ", nError
   ELSE
      IF SetFileToCurrentTime(hFile) = 0 THEN
         ? "SetFileToCurrentTime failed"
      ELSE
         ? "SetFileToCurrentTime succeeded"
      END IF
      CloseHandle(hFile)
   END IF

END FUNCTION