Setting file creation date

Roger Burnham rburnham at cri-inc.com
Fri Aug 10 16:11:13 EDT 2001


On Fri, 10 Aug 2001 19:02:51 +0200, "Alex Martelli" <aleaxit at yahoo.com> wrote:


>Which reminds me, SetFileTime lets you change those, and
>I don't know why win32file (part of the win32api) still does
>not make that call available according to the latest manual
>at URL [a single line, some sw will break it]:
>http://aspn.activestate.com/ASPN/Python/Reference/Products/ActivePython/Pyth
>onWin32Extensions/win32file.html
>
>Should be a C extension of a few lines, worst case.  Much
>simpler/faster than the rigmarole of change-date/copy-file/&c
>which I previously suggested.  Maybe calldll.pyd can help.
>

I had a need for this recently, but did not get it back to Mark yet :)

in win32apimodule.cpp, add (beware of wrapped lines)

// @pymethod object|win32api|SetFileTime|Set the files create, last access and
last write times
static PyObject *PySetFileTime(PyObject *self, PyObject *args)
{
  char *fileName;
  PyObject *pycreateTime, *pyaccessTime, *pywriteTime;
  if (!PyArg_ParseTuple(args, "sOOO:SetFileTime", 
                        &fileName, 
                        // @pyparm string|fileName||File to set times of
                        &pycreateTime, 
                        // @pyparm long|createTime||File Create time
                        &pyaccessTime, 
                        // @pyparm long|accessTime||File Access time
                        &pywriteTime
                        // @pyparm long|writeTime||File Write time
                        )) {
    
    return NULL;
  }
  if (!PyLong_Check(pycreateTime)) {
    return NULL;
  }
  if (!PyLong_Check(pyaccessTime)) {
    return NULL;
  }
  if (!PyLong_Check(pywriteTime)) {
    return NULL;
  }
  ULARGE_INTEGER create, access, write;
  if (!PyWinObject_AsULARGE_INTEGER(pycreateTime, &create)) {
    return NULL;
  }
  if (!PyWinObject_AsULARGE_INTEGER(pyaccessTime, &access)) {
    return NULL;
  }
  if (!PyWinObject_AsULARGE_INTEGER(pywriteTime, &write)) {
    return NULL;
  }
  FILETIME createTime, accessTime, writeTime;
  createTime.dwLowDateTime =  create.LowPart;
  createTime.dwHighDateTime = create.HighPart;
  accessTime.dwLowDateTime = access.LowPart;
  accessTime.dwHighDateTime = access.HighPart;
  writeTime.dwLowDateTime = write.LowPart;
  writeTime.dwHighDateTime = write.HighPart;
  HANDLE fileHandle = CreateFile(fileName, GENERIC_WRITE, NULL, NULL,
                                 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
                                 NULL);
  if (!fileHandle || fileHandle == INVALID_HANDLE_VALUE) {
    return ReturnAPIError("CreateFile");		
  }
  BOOL ok = SetFileTime(fileHandle, &createTime, &accessTime, &writeTime);
  if (!ok) {
    return ReturnAPIError("SetFileTime");		
  }
  ok = CloseHandle(fileHandle);
  if (!ok) {
    return ReturnAPIError("CloseHandle");		
  }
  Py_INCREF(Py_None);
  return Py_None;
}

and to 

static struct PyMethodDef win32api_functions[] = {

add

{"SetFileTime",         PySetFileTime,  	1}, // @pymeth SetFileTime|Set
the files create , last access and last write times



Cheers,

Roger Burnham
rburnham at cri-inc.com



More information about the Python-list mailing list