Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What do you use when you want to update the date-modified field of a file on Windows?

  1. commands accessible via C++, .NET, C#, or something native to Windows (Vista preferably)
  2. tools/applications preferably free, and if possible open source as well

Edit: there is already a page for applications as pointed out by CheapScotsman here.

If anyone knows how I can do this via C++, C#, WSH or something similar, well and good, else I would think everything else is covered in the linked question.

share|improve this question

migrated from stackoverflow.com Jul 21 '09 at 23:26

This question came from our site for professional and enthusiast programmers.

    
The four alternatives mentioned above, plus four more not mentioned here, can be found in the answer to a similar question: "Windows Recursive Touch Command" – JdeBP Feb 28 '11 at 19:58
    
stackoverflow.com/questions/210201/… mentions a load, including REM.>a – barlop Jun 17 '15 at 0:30

28 Answers 28

up vote 19 down vote accepted

If you feel like coding it yourself, .NET offers the File.SetLastAccessTime, File.SetCreationTime and File.SetLastWriteTime methods.

share|improve this answer

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.

Edit based on comments by Lumi and Justin: put this in a batch file, eg. touch.cmd

@COPY /B %1+,, %1

This works even if the file is not in the current directory (tested on Windows 7).

share|improve this answer
4  
This will be slow if the files are large. – mob Sep 18 '09 at 15:09
38  
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
14  
Documentation for copy: technet.microsoft.com/en-us/library/bb490886.aspx – Joseph Jun 3 '11 at 20:19
13  
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
13  
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

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
1  
As almost every unix-to-windows port ever, this fails to work with unicode characters in the filenames outside of the encoding set as the default for non-unicode programs. Test filename that will trip up every such tool: "test тест τεστ.txt" – romkyns Apr 29 '14 at 3:06
    
gnuwin32 has it. but indeed, gnuwin32's also fails for unicode characters. – barlop Jan 23 at 4:37

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
6  
PowerShell FTW. I mean, seriously, unix is cool but PS is cooler. – matt 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
8  
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 '13 at 1:43
5  
@BrainSlugs83: use backticks ` for code: gci -recu -inc "*.*" | % { $_.LastWriteTime = Get-Date } – alldayremix Jun 29 '13 at 0:44
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
    
Unfortunately this does not work (at least not in Windows 7). It seems that the command interpreter does not update the timestamp of the destination of a redirection if the file is not actually modified (i.e., there is not data to redirect). – Synetech Sep 15 '15 at 0:57

@dash-tom-bang:

Here is Technet'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 Technet's copy command reference: http://technet.microsoft.com/en-us/library/bb490886.aspx

share|improve this answer
15  
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
5  
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
2  
It worked in Windows Server 2008 but only if you are in the folder containing the file – FrinkTheBrave Dec 12 '11 at 14:48

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

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

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

Here's a simple regfile I wrote to add right-click "touch" in Windows explorer. It'd be easy to script it, too, since it just calls:

cmd.exe /c copy %1+nul %1 /by
share|improve this answer
    
And don't forget copy nul some_file to create an empty file (which is what I see touch most often used for). – Joey Aug 27 '09 at 6:28
    
Ack! Zip file inaccessible (something about brinkster22.com needing to be the referring site). Jon, can you update this? – Dan7119 Mar 28 '11 at 14:22

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

ECHO text>file1.txt

share|improve this answer
3  
This is BY FAR the best quick method.. I just needed to create a .gitignore file, and Windows 7 keeps complaining "must enter a filename" etc etc. echo pie>.gitignore worked a treat - thanks! – Alex McMillan Mar 7 '14 at 4:02
1  
@Alex This is unrelated, but that windows error is because the file starts with a period. There's a strange hack to get around that in Windows Explorer: make the file end with a period too. So type .gitignore. and when you press enter Windows removes the trailing period. Crazy, but it works. – Brad Cupit Apr 23 '15 at 15:26
    
I wish that I could give this enough upvotes that it would catch the big players in popularity. – Jonathan Mee Mar 3 at 16:56

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

Try this one from CodeProject.

  • No need to install.
  • If you want, you can even modify the source.
share|improve this answer
1  
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Yan Sklyarenko Jun 5 '14 at 14:32

You could also install Cygwin which gives you Touch as well as a plethora of other *NIX commands.

share|improve this answer
2  
can't live on windows without cygwin. – jweede Sep 10 '09 at 12:32
    
And cygwin without mintty is pretty lame. – Ahe Jan 25 '10 at 14:30

There are Windows ports of many Unix utilities. Have a look at unxutils or GnuWin32 projects.

share|improve this answer

From a similar question on Stack Overflow.

For updating timestamps (ignoring the other functionality of touch), I'd go with:

copy /b filename.ext +,,
share|improve this answer
    
This is a repeat of Gish Domains's answer from 2 yrs prior, without the explanation. – fixer1234 Feb 20 at 0:39

How about codeproject "Touch for Windows": http://www.codeproject.com/KB/applications/touch_win.aspx

edit; Same question as here: http://stackoverflow.com/questions/51435/windows-version-of-the-unix-touch-command/51439

share|improve this answer
    
Thanks. I missed that in my search which resulted in loads of touch screen phone related stuff. Probably needs a better tag label I guess. – facepalmd Jul 21 '09 at 21:52

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

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

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

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, quit, force". This will open file, do a save, then close it immediately afterward.

share|improve this answer

Save the following as touch.bat in your %windir%\system32 folder or add the folder in which it is saved to your PATH environment variable:

@echo off
if %1.==. goto end
if not exist %1 goto end
copy /b %1 +,, > nul
echo %1 touched!
:end

Sample usage:

touch *.cpp
touch somefile.c

Reference: Microsoft KB 69581

share|improve this answer
    
Excellent! No external stuff, just copy. And of course most of us need it as a batch stript:) – Bogdan Gavril Mar 27 '13 at 22:07

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

share|improve this answer
1  
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Yan Sklyarenko Jun 5 '14 at 14:32

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

Try

fsutil file createnew new.txt 0

share|improve this answer
2  
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

The Unix people fixed the general problem of updating the file date of any file with the touch command. However, for Windows, sometimes a simpler method is possible for special cases.

I need to update the timestamp of an application shortcut in Windows 8.1 in order to make changes to the background color of the Application Tile visible, see this SO question. Rather than implementing one of the clever tools above, I find it easier to edit the comment field of the shortcut. Most people leave this empty, but of course, a useful comment is quickly conceived. And if a comment exists, adding or removing a final period does never harm.

share|improve this answer

I appreciate this is an old question, I just discovered touch on my Windows 10 system. I downloaded and installed Git from here (I think) and it looks like touch and various other utilities are in the bin folder.

share|improve this answer

in PowerShell try:

ni fileName.txt

NI is an alias of the New-Item cmdlet.

share|improve this answer
1  
Welcome to SuperUser. Thanks for posting! Can you add a link to a website that provides more information about the ni command, and perhaps describe more what the command does? – hBy2Py Jun 17 '15 at 2:17
    
The New-Item cmdlet can't be used to update the time stamp of an existing file. – Twisty Jun 17 '15 at 2:56

You must log in to answer this question.

protected by Community Jun 11 at 15:16

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged .