Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm looking for a Windows port of the UNIX touch command. I don't want to install an entire MKS toolkit just for the one tool. Is there a native port available somewhere or a command in Windows that does the same thing and supports features like all files in a directory by wildcard?

Specifically I'm after changing mtime, ctime and atime for a project that reports ages of files based on... mtime, ctime and atime.

share|improve this question
add comment (requires an account with 50 reputation)

20 Answers

up vote 114 down vote accepted

I've used and recommend unxutils which are native Win32 ports of lots of common Unix utilities. There is a touch command in there.

share|improve this answer
3  
Would upvote twice if I could. This is one of the tools I install after a fresh install, can't get out of the habit of typing ls at any command line I touch. – Jamie Penney May 29 '09 at 3:06
4  
Note: the links on the above page don't seem to work for me but going to the project page sourceforge.net/projects/unxutils (as linked by Adam Davis below) allows you to download them fine. – Chris Feb 10 '11 at 10:28
@Chris: The link in the answer works for me. – Keith Thompson Feb 18 '12 at 1:36
@KeithThompson: Ah, must have just been down temporarily. Thanks for the update. – Chris Feb 20 '12 at 10:11
add comment (requires an account with 50 reputation)

If you want to touch the date stamp of a file using windows, use the following command at the command prompt:

copy /b filename.ext +,,

(where filename.ext is your file's name). The +,, is a special flag to copy telling it to simply update the date/time on the file:

* Changing the time and date of a file

If you want to assign the current time and date to a file without modifying the file, use the following syntax:

copy /b Source+,,

The commas indicate the omission of the Destination parameter.

share|improve this answer
3  
This will be slow if the files are large. – mob Sep 18 '09 at 15:09
27  
What is this doing? Where is it documented? (Google fails to index "+,,", and the MS site is not leading me in the right direction as searching on 'copy' turns up a lot of nothing useful.) I mean, it works great and I'll take that much for granted, but I am curious. – dash-tom-bang Oct 30 '09 at 21:42
9  
Documentation for copy: technet.microsoft.com/en-us/library/bb490886.aspx – josmh Jun 3 '11 at 20:19
6  
type touch.bat is @echo off and next line copy /b %1 +,, - put it into C:\bin or what have you for your own scripts, and then you can use it like touch myfile.txt. – Lumi Jun 13 '11 at 13:37
2  
In Win7 this won't work if you are not in the folder containing the file. It will create a copy in the current working directory. – Jamie Jun 27 '12 at 7:48
show 4 more commentsadd comment (requires an account with 50 reputation)

If all you want is to change the file's last modified date (which was my case):

C:\> powershell  (ls your-file-name-here).LastWriteTime = Get-Date
share|improve this answer
1  
PowerShell FTW. I mean, seriously, unix is cool but PS is cooler. – matt eisenberg Sep 9 '11 at 14:35
1  
You can update the last time write for multiple files / folders as well – ThiagoAlves Sep 22 '11 at 1:50
3  
This will work only on one file. For multiple files: ls * | ForEach-Object {$_.LastWriteTime = Get-Date} – Eli Algranti Oct 2 '12 at 4:52
1  
If you want to do it recursively, this works pretty well: gci -recu -inc "." | % { $_.LastWriteTime = Get-Date } – BrainSlugs83 Jan 15 at 1:43
1  
@BrainSlugs83: use backticks ` for code: gci -recu -inc "*.*" | % { $_.LastWriteTime = Get-Date } – alldayremix Jun 29 at 0:44
show 1 more commentadd comment (requires an account with 50 reputation)

@dash-tom-bang:

Here is MSDN's explanation of the mysterious '+' and commas:

copy /b Source+,,

The commas indicate the omission of the Destination parameter.

The copy command supports merging multiple files into a single destination file. Since a blank destination cannot be specified using a space character at the command prompt, two commas can be used to denote that.

And this is MSDN's copy command reference: http://technet.microsoft.com/en-us/library/bb490886.aspx

share|improve this answer
8  
Now that's just messed up syntax. Seriously, what were they thinking? Also note the same documentation says "Destination: Required."... I'm amazed. – sth Nov 25 '09 at 13:22
1  
This doesn't seem to even work in Vista... I wonder if they came to their senses? – quillbreaker Dec 8 '09 at 20:53
1  
Works in Windows 7 – Imran Dec 9 '10 at 5:37
2  
It worked for me even without commas: copy file.ext+ So the documentation is as far from actual behaviour as the behaviour is from any reasonable expectations. – Abgan Jul 17 '11 at 11:24
It worked in Windows Server 2008 but only if you are in the folder containing the file – FrinkTheBrave Dec 12 '11 at 14:48
add comment (requires an account with 50 reputation)
type nul >>file & copy file +,,
  • Creates file if it does not exist.
  • Leaves file contents alone.
  • Just uses cmd built-ins.
  • Both last-access and creation times updated.

UPDATE

Gah! This doesn't work on read-only files, whereas touch does. I suggest:

:touch
if not exist "%~1" type nul >>"%~1"& goto :eof
set _ATTRIBUTES=%~a1
if "%~a1"=="%_ATTRIBUTES:r=%" (copy "%~1"+,,) else attrib -r "%~1" & copy "%~1"+,, & attrib +r "%~1"
share|improve this answer
It's a pity that copy cannot ignore the +r attribute the way xcopy does. Or, it's a pity that xcopy doesn't support the weird filename +,, syntax. – Abel May 6 '12 at 20:05
add comment (requires an account with 50 reputation)

The GnuWin32 project has Windows ports of the Gnu versions of the Unix command line utilities.

It comes as a number of separate packages and you can install just the commands you need with no other dependencies. For touch you would need the CoreUtils package.

share|improve this answer
add comment (requires an account with 50 reputation)

Native win32 ports of many unix commands, including touch.

I've used it before and it works well - no installation, no DLLs, etc

share|improve this answer
add comment (requires an account with 50 reputation)

cygwin comes with touch. I know you mentioned that you don't want to install a whole framework, but cygwin is quite lightweight, and can be called from dos command window without the whole unix-like command line turned on.

You can also control what tools to install, so you could simply install the touch.exe file, and leave the rest of the framework.

share|improve this answer
2  
To your point here, all you need to install is the touch.exe and cygwin.dll file in a directory to use the tool. There are no other dependancies relative to using cygwin based tools. – Tall Jeff Sep 9 '08 at 10:58
1  
when I try this (win7x64) I need 4 cygwin dll's in addition to touch.exe: cygiconv-2.dll cygintl-8.dll cygwin1.dll cyggcc_s-1.dll – matt wilkie May 27 '10 at 20:33
add comment (requires an account with 50 reputation)

here is a recursive version using powershell... this will change the last modified time for all files and subdirectories, and files within this directory's subdirectories

ps c:myDir> Get-ChildItem . * -recurse | ForEach-Object{$_.LastWriteTime = get-date}
share|improve this answer
add comment (requires an account with 50 reputation)

Try this one from CodeProject.

  • No need to install.
  • If you want, you can even modify the source.
share|improve this answer
add comment (requires an account with 50 reputation)

from the website:

Funduc Software Touch is a free 'touch' utility that allows you to change the time/date &/or attribute stamps on one or more files. In addition, FS Touch can add/subtract a specified number of seconds from the existing file time. You can specify which file(s) and/or subdirectories to change via 'complex file masks'. The program can be run from interactively or the command line. New to version 7.2 is a command line switch to change file modified time stamp +/- the specified number of seconds.

FS Touch runs on Windows XP, Vista, Windows 7, & Windows 8.

share|improve this answer
add comment (requires an account with 50 reputation)

I found a quick way to do it if you have vim installed (not great for big files, will open entire file then close it...)

vim foobar.txt +wq!

The "+" sets argument to run the following commands. "wq!" is "write, quite, force". This will open file, do a save, then close it immediately afterward.

share|improve this answer
add comment (requires an account with 50 reputation)

The five alternatives mentioned above, plus three more not mentioned here, can be found on SuperUser: "Windows Recursive Touch Command"

share|improve this answer
add comment (requires an account with 50 reputation)

This is slightly unrelated to the original question, but I find this very useful on Windows due to the GUI.

I'm using the TouchPro utility which provides a GUI (builds into explorer shell):

http://www.jddesign.co.uk/products/touchpro/touchpro.htm

share|improve this answer
add comment (requires an account with 50 reputation)

i tried this to create a empty file in my batch script ,you can use this

ECHO text>file1.txt

share|improve this answer
add comment (requires an account with 50 reputation)

If you are using git for one or more projects, the mingw based git-bash for Windows has the touch command. I want to thank @greg-hewgill for pointing out to me that 'nix utilities exist for windows, because it was that which put me on the idea to try touch in git-bash.

share|improve this answer
add comment (requires an account with 50 reputation)

This content can be saved to a reg file. This will add a right click context menu for all files with the "Touch File" ability (tested on Windows 7). Copy all the following lines to reg file. Run the file and approve the question. Right click on any file (or multiple files) - "Touch File" option is now available.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell]

[HKEY_CLASSES_ROOT\*\shell\Touch File]

[HKEY_CLASSES_ROOT\*\shell\Touch File\command]
@="cmd /C copy /b \"%1\" +,,"
share|improve this answer
add comment (requires an account with 50 reputation)

Try

fsutil file createnew new.txt 0

share|improve this answer
1  
The FSUTIL utility requires that you have administrative privileges. -- and it doesn't behave like touch for existing files. – Keith Thompson Feb 18 '12 at 1:39
add comment (requires an account with 50 reputation)

How about this:

echo %date% > C:\tmp\today.txt
share|improve this answer
1  
The touch command simply updates the timestamp on the file without changing the contents. Your example writes the timestamp into the file. – BryanH Sep 17 '12 at 22:34
1  
Please read the OP's question carefully. This answer doesn't serve OP's question. – askmish Sep 18 '12 at 13:16
2  
-1 for suggesting the OP to destroy his file. – René Nyffenegger Dec 4 '12 at 21:23
Important point @René Nyffenegger makes: the single "Greater Than" redirection > will overwrite the contents of the destination file (in this case C:\tmp\today.txt) instead of appending to the end of it using >>. However it still doesn't solve the problem of timestamping as @BryanH points out – Flak DiNenno Dec 7 '12 at 15:24
add comment (requires an account with 50 reputation)

how about simply :

"">>myFile

works just fine ;)

share|improve this answer
2  
Except that it displays an error message and overwrites the existing file :) (neither of which is what touch does). – Andriy M Jan 3 '12 at 6:35
2  
A >> redirection won't overwrite an existing file. – Keith Thompson Feb 18 '12 at 1:38
2  
@KeithThompson: You are right, but this particular command won't update the modification timestamp either (not in Windows XP SP3, at least). And the error message will still be produced. (It could be removed, of course, only there's no point in fixing a command's side effect if it is its only effect.) Anyway, my first comment was about the original suggestion, which was "">myFile. – Andriy M Feb 20 '12 at 0:44
add comment (requires an account with 50 reputation)

protected by Andriy M Sep 24 '12 at 4:59

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.

Not the answer you're looking for? Browse other questions tagged or ask your own question.