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

GeneralRe: touch doesn't create a new filememberJörgen Sigvardsson15 Mar '10 - 22:28 
I found the error, and I fixed it. Turns out that the glob/wildcard support patch I got a couple of years ago contains a bug! Submitted the code just now. Smile | :)
--
Kein Mitleid Für Die Mehrheit

GeneralRe: touch doesn't create a new filememberMart Smeets16 Mar '10 - 7:34 
This one works, thanks. Do you have plans for more features?
Like ignore attributes (read-only) or change dates of a directory?
(For me, the gnu touch does not change directories either)
GeneralRe: touch doesn't create a new filememberJörgen Sigvardsson16 Mar '10 - 20:41 
I added the support for directories. Update has been submitted, and will probably go online later today!
--
Kein Mitleid Für Die Mehrheit

GeneralRe: touch doesn't create a new file [modified]memberMart Smeets17 Mar '10 - 11:48 
This is very useful to me. Thanks! I've only used it on a 2k/FAT32 install now but will try other (NTFS) stuff later.
 
Edit: When a file does not exist yet it gets created with the date/time it is given by touch.
But if a dir does not yet exist it gets created with the current date/time.
So if another timestamp is needed the same "touch -d -t yyyymm..etc" must be run again for a dir. Is that how you want it?
 

-- Modified Thursday, March 18, 2010 3:06 PM
GeneralRe: touch doesn't create a new filememberJörgen Sigvardsson15 Mar '10 - 22:01 
I found it. Someone gave me a patch for glob support, and then a bug was introduced. I'll fix it... Smile | :)
--
Kein Mitleid Für Die Mehrheit

GeneralShell integration [modified]memberCBoland31 Jan '07 - 12:52 
Here's a registry snippet for adding a touch command to the context menu when right-clicking files:
 
Windows Registry Editor Version 5.00
 
[HKEY_CLASSES_ROOT\*\shell]
 
[HKEY_CLASSES_ROOT\*\shell\touch]
 
[HKEY_CLASSES_ROOT\*\shell\touch\command]
@="\"C:\\Utils\\touch.exe\" \"%1\""
 

Save the snippet to file touch.reg. Update the path to touch.exe (remember to keep the '\\' characters). Double-click touch.reg and click Yes to add the info into the registry.
 
I use this when deploying ASP.NET applications on the local intranet. It's a convenient way to have the application restart without restarting the web server.
 
Enjoy!
 

-- modified at 11:41 Thursday 1st February, 2007
GeneralRe: Shell integrationmemberJoergen Sigvardsson31 Jan '07 - 22:33 
Thanks for sharing!
 
--
Verletzen zerfetzen zersetzen zerstören
Doch es darf nicht mir gehören
Ich muss zerstören

GeneralRe: Shell integrationmemberSteveKing17 Mar '10 - 21:09 
Or use this little tool[^]
GeneralRe: Shell integrationmemberAstyan28 Feb '11 - 18:36 
Thanks,
I use it in a portable way : in a folder with dropbox
So here it is a batch with no need to customize anything anymore :
 
Create a file in the main folder of the zip,
named for example "touch - (re)install in contextual menu.bat"
and put this code in it :
REM TODO : Is there any deterministic way to get a temp file ?
set tmpfile=_tmp.reg
echo Windows Registry Editor Version 5.00          > %tmpfile%
echo [HKEY_CLASSES_ROOT\*\shell]                  >> %tmpfile%
echo [HKEY_CLASSES_ROOT\*\shell\touch]            >> %tmpfile%
echo [HKEY_CLASSES_ROOT\*\shell\touch\command]    >> %tmpfile%
REM Get current directory
set folder=%CD%
REM Double the backslash
set folder=%folder:\=\\%
REM Create the command line to use when user will select touch in contextual menu
echo @="\"%folder%\\Release\\touch.exe\" \"%%1\"" >> %tmpfile%
REM Run the temporary registry file 
regedit /s %tmpfile%
REM No need to keep this file
del %tmpfile%
 
REM Cleaning screen
cls
@echo Install has completed successfully !
@pause REM You can comment or delete this line to close the window
 
Now you just have to double click on this file and touch will be in the contextual menu Smile | :)
 
P.S:
My batch skills are not high so let me know if the quality of this code can be enhanced !
 
P.S 2:
Same licence Big Grin | :-D !
GeneralWell Done - it just works !!memberChrisRRRR31 Jan '07 - 0:59 
Well Done - it just works !!
It's great when something just works as it says it does.
This should be in sysinternals.
Smile | :)
 
Chris Ringrow

GeneralRe: Well Done - it just works !!memberJoergen Sigvardsson31 Jan '07 - 22:32 
Thanks!
 
--
Verletzen zerfetzen zersetzen zerstören
Doch es darf nicht mir gehören
Ich muss zerstören

GeneralRecursionmemberBleary Eye28 Nov '06 - 5:54 
I didn't see a recursion option (for sub-directories). That would be a nice add-on.
GeneralDoes not create files if it does not exist.membergorfou7 Sep '06 - 7:22 
Well, title say it all. I used VC++ Express to compile the project, and there were a couple of "deprecated" message while building.
GeneralThanks, Jorgen!memberMy other CP account18 Jun '06 - 19:04 
Nice utility - does what it claims, and does it well. Smile | :)
 
~ Nazgûl
+++++++++++++

QuestionWhy not to use original touch?memberPaulius Maruška21 Feb '06 - 10:39 
GNU Win32 project has a latest version of touch compiled for win32 platform. I say, don't reinvent the wheel - use existing tools!
Ok. There is one (only one!) exception to my rule - you *can* reinvent the wheel if you're doing that for learning purposes.
 
GNU Win32 project[^]
Package that contains touch[^]
AnswerRe: Why not to use original touch?memberJörgen Sigvardsson21 Feb '06 - 10:42 
Because I wrote it for a) fun, b) someone asked for it a long time ago. If you don't want to use it, that's fine with me. Smile | :)
GeneralRe: Why not to use original touch?memberPaulius Maruška21 Feb '06 - 10:48 
Ok, in that case i would like to add one more exception to my rule - it's ok, if it is done for fun.
So if you're learning or just having fun - it's ok to reinvent the wheel... Smile | :)

AnswerRe: Why not to use original touch?memberNemanja Trifunovic17 Mar '10 - 7:31 
The GNU version you are mentioning is as "original" as Jörgen's one. touch utility appeared in Version 7 AT&T UNIX[^]
GeneralFor touch with handling multiple files at once...memberirnis21 Feb '06 - 3:13 
...look the eXpress TimeStamp Toucher at http://www.irnis.net/soft/xtst/
GeneralThank you!memberPhaetonTheGreat!14 Feb '06 - 18:56 
You saved me from boring works. Thanks a lot! ^^
 
김동일 / Kim, Dong-il
Remember the difference between a boss and a leader: a boss says "Go" a leader says "Let's go"
GeneralRe: Thank you!memberJörgen Sigvardsson14 Feb '06 - 20:32 
????? Smile | :)
GeneralRe: Thank you!memberJörgen Sigvardsson14 Feb '06 - 20:34 
Doh. I meant to write "kam sam ni da", but in Hangul. It would seem that I'd have to figure out the unicode values and encode the symbols as html entities, because obviously, CP is not unicode. Sigh | :sigh:
GeneralRe: Thank you!memberPhaetonTheGreat!15 Feb '06 - 0:43 
NIt's my pleasure.
 
I make some update to your source code.
This modified version supports wildcard parameter.
 
http://user.chol.com/~phaeton/codeproject/touch_20060215.zip
 
Hoppas du har en trevlig dag. Smile | :)
 
김동일 / Kim, Dong-il
Remember the difference between a boss and a leader: a boss says "Go" a leader says "Let's go"
 
-- modified at 6:43 Wednesday 15th February, 2006
GeneralRe: Thank you!memberJörgen Sigvardsson15 Feb '06 - 1:04 
Thank you very much for the update! I will try to update the article right away. (Might take some time as this article is already edited, and cannot be modified directly by me anylonger)
 
PhaetonTheGreat! wrote:
Hoppas du har en trevlig dag.

Tack detsamma!
GeneralGood, but look at ....memberamauta2 Feb '05 - 11:43 
Hello,
 
If you are interesed in Touch look at
 
http://www.flos-freeware.ch/misc.html[^]
 

Bye.
 

GeneralRe: Good, but look at ....membergorfou7 Sep '06 - 7:19 
No good if you need a command line touch
GeneralI was looking for a good touch :)memberAvinoam Sapir19 Jan '05 - 21:28 
Thanks for sharing!
Generalquestionmemberzetroooooo3 Sep '04 - 19:44 
can you run this program from a cgi file like
 
`touch finenamehere`;
 

GeneralRe: questionmemberJörgen Sigvardsson3 Sep '04 - 22:53 
I suppose so. I guess it depends on the capabilities of the web server.
 
--
Arigato gozaimashida!
QuestionDirectory Support?sussAdam P.30 May '04 - 11:33 
I was hoping you could add directory support to your touch utility. I have been unable to find any other utilities to modify the creation time stamp of directories.
 
Thank you for your time.
AnswerRe: Directory Support?memberDavidLowndes@msn20 Feb '06 - 22:37 
TouchPro can modify directory timestamps - you can get it here: www.jddesign.co.uk
AnswerRe: Directory Support?memberWillow530 May '09 - 17:45 
Hmm, TouchPro seems like a step in the wrong direction. It's a Windows GUI app that registers itself into Windows Explorer -- hardly a lightweight W32 app like Jörgen's demo. TouchPro includes a command line version but jddesign forces you to register with them before you can use it.
 
Why don't you use gnu or cygwin Win32 touch.exe for directory touch functionality? Sources for both are freely available.
 
Alternatively, Jörgen might be persuaded to update his demo for this OpenBSD-like functionality. Hint, hint Jörgen!
 
Hi hi
Willow
GeneralProblems with Secondsmembermario543233 Apr '04 - 4:10 
Setting the seconds didn't work correct and the hours are different (perhaps GMT ? i have CEST (GMT+1) on my computer) if i try to set
 
touch -t 200402191526.04 xxx.txt
 
sets the filedate to 19.02.2004 16:26:40
 

GeneralRe: Problems with SecondsmemberJörgen Sigvardsson3 Apr '04 - 6:06 
The hour offset is explained by daylights saving. But why 04 becomes 40 is beyond me.. I'm on it. Smile | :)
 
--
Din mamma.
GeneralRe: Problems with SecondsmemberJörgen Sigvardsson3 Apr '04 - 7:03 
Found the problems, and fixed them. Moving on to next error reports.... Smile | :)
 
--
Din mamma.
GeneralFew small FYIsmemberJoe Woodbury15 Mar '04 - 7:26 
The '-h' argument doesn't work. It's not only not explicitly handled, a single argument is always assumed to be a file name.
 
If the file doesn't exist, no error is emitted. Instead, the file is created. (Why this is the default is beyond me.
 
Project won't compile with UNICODE; several strings aren't prefixed properly and LPSTR is repeatedly used rather than LPTSTR.
 
I'd fix it, but this is the first time I've needed the program since I can remember and don't anticipate needing it again any time in the near future.
 
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke

GeneralRe: Few small FYIsmemberJörgen Sigvardsson15 Mar '04 - 23:41 
Joe Woodbury wrote:
The '-h' argument doesn't work. It's not only not explicitly handled, a single argument is always assumed to be a file name.
 
Confused | :confused: Granted it's been a long time since I wrote this, but I'm sure I tested this. I'll have a look at it this evening.
 

Joe Woodbury wrote:
Project won't compile with UNICODE; several strings aren't prefixed properly and LPSTR is repeatedly used rather than LPTSTR.
 
Unsure | :~ This is embarrasing.. Blush | :O
 
--
Futue te et ipsum caballum.
GeneralRe: Few small FYIsmemberJörgen Sigvardsson3 Apr '04 - 7:04 
Joe Woodbury wrote:
If the file doesn't exist, no error is emitted. Instead, the file is created. (Why this is the default is beyond me.
 
That's traditional UNIX semantics for ya.
 
I'm looking into the unicode stuff as I write this...
 
--
Din mamma.
GeneralRe: Few small FYIsmemberNemanja Trifunovic13 Apr '04 - 10:10 
Jörgen Sigvardsson wrote:
That's traditional UNIX semantics for ya.
 
So true[^] (although I must add I am not a fan of ESR)
GeneralRe: Few small FYIsmemberJörgen Sigvardsson3 Apr '04 - 7:13 
Everything fixed, included the stuff reported in the post above yours. I'll submit the changes now to the editors.. Smile | :)
 
--
Din mamma.

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.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