Click here to Skip to main content
Click here to Skip to main content

Touch for Windows

By , 17 Mar 2010
 

Introduction

I've seen at least two people asking for a Windows version of the UNIX utility touch this past week. So here it is: a Windows version of the OpenBSD touch command. It's not fully compatible as my utility can only touch one file at a time and does not support the -f argument (force touch, ignoring access rights). However, my version supports altering modification, access and creation time, while the OpenBSD version only supports alteration of access and modification time.

If you have a need for a touch utility which ignores access rights and can handle multiple files (including globing), tell me and I'll see what I can do.

I've also made sure it's UNICODE compatible, but as my system is as western as it gets, I haven't really been able to test it. If you find anything fishy when compiled as a UNICODE binary, please let me know.

What Does It Do?

For those who don't know, here's a simple explanation: It updates the dates associated with a file to whatever you want it to be. It can change access, modification and creation dates. You as a user can update them independently of each other, you can update them with any possible date. Dates can be now, user specified or copied from another file. If you try to touch a file which does not exist, this command will create it for you (empty of course).

You can always run touch -h for help.

Where's the Article?

This is not rocket science. I'm basically just using simple Win32 API functions. Writing an article about it would be like reciting the MSDN documentation. If you are interested in how it works, use the source - it's not big and it doesn't really contain any surprises.

License Grant

You are granted a license to use the code for whatever purpose, if and only if the following conditions are met:

  • You may expect no warranties from my part. If you break something, you fix it.
  • If we meet some day in the flesh, you may buy me a beer of my choice. This is not a requirement, but it would be a very nice thing to do and I would appreciate it. If your religion prohibits you from buying any alcohol, even if not for yourself, a coke is fine or whatever well testing beverage which you find morally and ethically acceptable.

Revision history

  • 2002-11-28 - Initial version
  • 2004-04-03 - Bug fixes: UNICODE, argument parsing, daylight saving, time string parsing
  • 2006-02-15 - Wildcard support, much thanks to PhaetonTheGreat!
  • 2010-03-16 - Wildcard support introduced a bug. Fixed it, and gave touch the ability to handle multiple files and glob patterns
  • 2010-03-17 - Added support for directories per request. See the new -d switch. -r can also be specified to use a directory as a time source

License

This article, along with any associated source code and files, is licensed under The BSD License

About the Author

Jörgen Sigvardsson
Software Developer (Senior)
Sweden Sweden
Member
I make software.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionEXIF enhancementmemberYitzY Berlin16 Mar '13 - 23:25 
i took your code and added an EXIF option (and a verbose option) to it. I can now clean up pictures from my camera, and "touch -e" resets all of the dates back to the EXIF date and times. I would be happy to give you the code...
 
have fun....YitzY
GeneralMy vote of 4memberAravind Kumar K22 Aug '12 - 6:44 
Super article. I wish there was a recursive option. Never mind. I will extend it to fit my purpose!
Thanks again!
QuestionGreat utility - minor feature requestmemberncdev10 Feb '12 - 7:44 
Thanks for writing this utility! It is particularly useful in situations when XCOPY is used to clone directory structures but creates new subdirectories with the current date/time rather than the original timestamp of the source.
 
I had some rather complicated scripting to try to "restore" the directory time stamps by reading the reference directories - all done using the win32 port of gnu touch -- however, when running on x64 windows, the win32 port utilities are slow (emulation) and having your native Windows version and the -r option of using another file/directory as a "reference" looks great.
 
I was thinking of one enhancement: would it be possible to add the ability to choose the lesser - or greater - timestamp (destination or the directory/file specified by -r)? In the case of directories, I would like the option of applying the timestamp of the -r reference only if it is less (i.e., leave the destination timestamp untouched if the reference is newer.
 
Of course, if I'd used robocopy from the start with its preserve directory timestamp option, I wouldn't need your utility to clean up afterwards ::
QuestionChange "Date Taken" information (EXIF) for photos?memberbeerslayer20008 Dec '11 - 12:13 
Hi,
 
I'm having a really hard time finding a utility to do what I need done - to change the "Date Taken" field on a bunch of photos taken when the camera's Date/Time settings were not correct.   I have yet to find a "Touch"-type utility that will do this, and unfortunately yours is no exception.
 
I don't know whether this is a really hard feature to implement, since it involves interpreting the EXIF data embedded within the file, or whether nobody considers it important enough to bother with.   Surely I can't be the only user out there who needs to do this!
 
Please consider adding this feature for any future revision you might do.   Thanks!
 

-- Jeff
GeneralThe licencememberRozis16 Mar '10 - 11:22 
Thumbs Up | :thumbsup: I like the licence...
GeneralRe: The licencememberJörgen Sigvardsson17 Mar '10 - 2:15 
Smile | :)
 
Am I getting a beer? Big Grin | :-D
--
Kein Mitleid Für Die Mehrheit

Generaltouch doesn't create a new filememberfrancwalter13 Mar '10 - 14:58 
Hello,
 
i expected touch.exe to create a new file with this:
 
touch C:\test.txt
 
but nothing happens. No new file, like it would happen in unix' touch.
Is this not implemented?
 
Thank you,
 
franc
GeneralRe: touch doesn't create a new filememberJörgen Sigvardsson13 Mar '10 - 21:30 
That's probably because you don't have access rights to modify C:\. What error messages are you getting?
 
This program is indeed designed to create an empty file if one is not present.
--
Kein Mitleid Für Die Mehrheit

GeneralRe: touch doesn't create a new filememberMart Smeets15 Mar '10 - 13:00 
I don't get a new file either. And yes I can create files in any dir. With me touch simply finishes as if it's done its job, no messages.
Do you want to know more?
GeneralRe: touch doesn't create a new filememberJörgen Sigvardsson15 Mar '10 - 21:54 
Sounds bizarre... The CreateFile() call is checked for errors, and any such error is printed to screen. Could it be some virtualization trick in Vista/W7? Unsure | :~
--
Kein Mitleid Für Die Mehrheit

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 17 Mar 2010
Article Copyright 2002 by Jörgen Sigvardsson
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid