Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Is it possible to set the date/time of a directory in windows with Python? If so how?

Reply
Thread Tools

Is it possible to set the date/time of a directory in windows with Python? If so how?

 
 
ToddLMorgan
Guest
Posts: n/a
 
      05-13-2006
I'm trying to set any of the dates (create, modification, access) of a
directory under windows with python.

I'm trying to do this as I'm trying to write a unittest for a directory
cleaning script I'm writing (ie I need the test to set the create/mod
time for some of the directories so that I can be sure that the script
works properly - as it picks dirs based upon their age).

I've tried using the utime( path, times). That doesn't work as the doco
states
"Whether a directory can be given for path depends on whether the
operating system implements directories as files (for example, Windows
does not)."

So that doesn't work.

So I tried mark hammonds win32 extensions with something like this

import win32file, win32con, pywintypes
filehandle = win32file.CreateFile(file,
win32file.GENERIC_WRITE,win32file.FILE_SHARE_WRITE ,
None,win32con.OPEN_ALWAYS, 0, None)
nowWin32=pywintypes.Time(theTime)
win32file.SetFileTime(filehandle, nowWin32, nowWin32, nowWin32)

which works fine for files but fails with "Access Denied" when I invoke
the CreateFile with a directory. This seems to occur no matter the
combination of READ or WRITE I choose for the parameters.

So does anyone have any useful suggestions (other than not using
windows)?

I've thought of a couple of reallly nasty solutions:
1. Temporarily alter the underlying system clock back to the required
time and create the directory then resetting it back again afterwards.
2. Create the required directory in some form of virtual filesystem -
eg Zip file, rar archive, tar etc. Then extract it to the real
filesystem and hope windows honors the timestamps (untested at this
point).

None of those is particularly appealing.

Thanks for listening.

 
Reply With Quote
 
 
 
 
Roger Upole
Guest
Posts: n/a
 
      05-13-2006
"ToddLMorgan" <(E-Mail Removed)> wrote in message news:(E-Mail Removed) oups.com...
> I'm trying to set any of the dates (create, modification, access) of a
> directory under windows with python.
>
> I'm trying to do this as I'm trying to write a unittest for a directory
> cleaning script I'm writing (ie I need the test to set the create/mod
> time for some of the directories so that I can be sure that the script
> works properly - as it picks dirs based upon their age).
>
> I've tried using the utime( path, times). That doesn't work as the doco
> states
> "Whether a directory can be given for path depends on whether the
> operating system implements directories as files (for example, Windows
> does not)."
>
> So that doesn't work.
>
> So I tried mark hammonds win32 extensions with something like this
>
> import win32file, win32con, pywintypes
> filehandle = win32file.CreateFile(file,
> win32file.GENERIC_WRITE,win32file.FILE_SHARE_WRITE ,
> None,win32con.OPEN_ALWAYS, 0, None)
> nowWin32=pywintypes.Time(theTime)
> win32file.SetFileTime(filehandle, nowWin32, nowWin32, nowWin32)
>
> which works fine for files but fails with "Access Denied" when I invoke
> the CreateFile with a directory. This seems to occur no matter the
> combination of READ or WRITE I choose for the parameters.
>
> So does anyone have any useful suggestions (other than not using
> windows)?
>
> I've thought of a couple of reallly nasty solutions:
> 1. Temporarily alter the underlying system clock back to the required
> time and create the directory then resetting it back again afterwards.
> 2. Create the required directory in some form of virtual filesystem -
> eg Zip file, rar archive, tar etc. Then extract it to the real
> filesystem and hope windows honors the timestamps (untested at this
> point).
>
> None of those is particularly appealing.
>
> Thanks for listening.


To create a handle to a directory, you have to use
FILE_FLAG_BACK_SEMANTICS.

Roger


 
Reply With Quote
 
 
 
 
ToddLMorgan
Guest
Posts: n/a
 
      05-13-2006
Thanks very much for that roger

I changed my code to

filehandle = win32file.CreateFile(file, win32file.GENERIC_WRITE,
win32file.FILE_SHARE_WRITE, None, win32con.OPEN_ALWAYS,
win32con.FILE_FLAG_BACKUP_SEMANTICS, None)
nowWin32=pywintypes.Time(theTime)
win32file.SetFileTime(filehandle, nowWin32, nowWin32, nowWin32)

ie FILE_FLAG_BACKUP_SEMANTICS and everything works fine

Just for the sake of completeness ...

I'd just thought of another solution ... to have python determine the
modification date for the directory based upon the oldest contained
file (I'm guessing windows does the opposite - ie the newest file mod)
to give me the effect of having a directory with an older date.

The virtual file system idea didn't work either ... files had the
correct dates but the dirs didn't as doing the extraction with python
libs is subject to the same constraints ... and I didn't think that
altering the ZipInfo time entries would have any affect as they appear
to only represent files and not directories. I could have spawned a
command line to do the extraction but I lost interest at that point.

thanks again

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
Add users to directory/folder and set permissions in Windows Gallagher, Tim (NE) Python 1 08-21-2006 09:53 PM
Possible? Authentication using Windows/Active directory, but access SQL Server using Membership class Andy ASP .Net 1 04-15-2006 08:48 PM
How to define a Windows directory in ProcessBuilder directory() method? minjie@excite.com Java 2 03-16-2006 10:50 PM
question regarding overriding of web.config in the root directory..in a web app in a virtual directory dotnetprogram ASP .Net 1 12-27-2003 06:02 AM



Advertisments