Thread: changing file date

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    changing file date

    Hello,

    I want to change the creation, last modified and last viewed date of a file. Can somebody help me with that? I think it can be done with the windows api, but I have no idea how.

    MFG,
    keeper

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Simple example which creates file time.txt with very strange dates.
    The SYSTEMTIME struct:
    Code:
    typedef struct _SYSTEMTIME {
        WORD wYear;
        WORD wMonth;
        WORD wDayOfWeek;
        WORD wDay;
        WORD wHour;
        WORD wMinute;
        WORD wSecond;
        WORD wMilliseconds;
    } SYSTEMTIME;
    And here's the code:
    Code:
    #include <windows.h>
    
    int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil){
        HANDLE hFile=CreateFile("time.txt",GENERIC_WRITE,0,NULL,OPEN_ALWAYS,0,NULL);
        FILETIME created,accessed,modified;
        SYSTEMTIME sys_created={1601,1,1,1,3,7,9,0},sys_accessed={1704,2,1,2,11,43,12,0},
            sys_modified={2962,1,1,8,19,42,40,0};
        SystemTimeToFileTime(&sys_created,&created);
        SystemTimeToFileTime(&sys_accessed,&accessed);
        SystemTimeToFileTime(&sys_modified,&modified);
        SetFileTime(hFile,&created,&accessed,&modified);
        CloseHandle(hFile);
        return 1;
    }
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    71
    Quote Originally Posted by maxorator View Post
    Simple example which creates file time.txt with very strange dates.
    The SYSTEMTIME struct:
    Code:
    typedef struct _SYSTEMTIME {
        WORD wYear;
        WORD wMonth;
        WORD wDayOfWeek;
        WORD wDay;
        WORD wHour;
        WORD wMinute;
        WORD wSecond;
        WORD wMilliseconds;
    } SYSTEMTIME;
    And here's the code:
    Code:
    #include <windows.h>
    
    int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil){
        HANDLE hFile=CreateFile("time.txt",GENERIC_WRITE,0,NULL,OPEN_ALWAYS,0,NULL);
        FILETIME created,accessed,modified;
        SYSTEMTIME sys_created={1601,1,1,1,3,7,9,0},sys_accessed={1704,2,1,2,11,43,12,0},
            sys_modified={2962,1,1,8,19,42,40,0};
        SystemTimeToFileTime(&sys_created,&created);
        SystemTimeToFileTime(&sys_accessed,&accessed);
        SystemTimeToFileTime(&sys_modified,&modified);
        SetFileTime(hFile,&created,&accessed,&modified);
        CloseHandle(hFile);
        return 1;
    }

    Thanks, but where get this file time.txt saved and how does I change the date and time of an existing file. And can you tell me how to call this function int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil). I don't know what parameters to give.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    WinMain is like main for windows programs.

    http://cboard.cprogramming.com/showthread.php?t=79619

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM