What do you use when you want to update the date-modified field of a file on Windows?
- commands accessible via C++, .NET, C#, or something native to Windows (Vista preferably)
- tools/applications preferably free, and if possible open source as well
Edit: there is already a page for applications as pointed out by CheapScotsmanhere.
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.
- 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– JdeBP2011-02-28 19:58:25 +00:00CommentedFeb 28, 2011 at 19:58
- stackoverflow.com/questions/210201/… mentions a load, including
REM.>abarlop– barlop2015-06-17 00:30:53 +00:00CommentedJun 17, 2015 at 0:30 - npmjs.com/package/touch-for-windows works for mePiotr Migdal– Piotr Migdal2020-01-29 19:46:17 +00:00CommentedJan 29, 2020 at 19:46
- I have tried all the solutions below. I have also tried using WSL but the simplest is echo '' > C:\path\filename - uses a default Windows CLI command and should be available on all Windows systems.Zac Dreyer– Zac Dreyer2023-09-05 10:55:48 +00:00CommentedSep 5, 2023 at 10:55
32 Answers32
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 +,,(wherefilename.ext is your file's name). The+,, is a special flag tocopy telling it to simplyupdate 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+,, %1This works even if the file is not in the current directory (tested on Windows 7).
- 17
type touch.batis@echo offand next linecopy /b %1 +,,- put it intoC:\binor what have you for your own scripts, and then you can use it liketouch myfile.txt.Lumi– Lumi2011-06-13 13:37:47 +00:00CommentedJun 13, 2011 at 13:37 - 23In 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– Jamie2012-06-27 07:48:23 +00:00CommentedJun 27, 2012 at 7:48
- 6@mob Note this was not slow on a large file. I used it on a >1GB file on a network drive and it returned immediately (and worked).Chadwick– Chadwick2013-01-23 19:51:46 +00:00CommentedJan 23, 2013 at 19:51
- 8If you need to do this in a remote folder (UNC path) - you must specify the destination to the copy command as well -
copy /b Source+,, Source- whereSourceincludes the full path to the fileJustin– Justin2013-12-13 22:37:18 +00:00CommentedDec 13, 2013 at 22:37 - 8
+,,:blogs.msdn.com/b/oldnewthing/archive/2013/07/10/10432879.aspxthirtydot– thirtydot2014-05-08 01:33:34 +00:00CommentedMay 8, 2014 at 1:33
I've used and recommendunxutils which are native Win32 ports of lots of common Unix utilities. There is atouch command in there.
- 8As 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"RomanSt– RomanSt2014-04-29 03:06:13 +00:00CommentedApr 29, 2014 at 3:06 - gnuwin32 has it. but indeed, gnuwin32's also fails for unicode characters.barlop– barlop2016-01-23 04:37:46 +00:00CommentedJan 23, 2016 at 4:37
- 1Update from 2017 In Windows 10 we can use the Linux subsystem for windows, which installs bash. No need for cygwin, MinGW, unxutils or any other partial unix to windows ports.docs.microsoft.com/en-us/windows/wsl/install-win10Davos– Davos2018-03-26 10:39:05 +00:00CommentedMar 26, 2018 at 10:39
- @Davos have you tried that on any file other than your (usually unprivileged) user context? Because technically the userland side of LXSS is running as unprivileged user.0xC0000022L– 0xC0000022L2019-08-21 10:22:46 +00:00CommentedAug 21, 2019 at 10:22
- 4These days it's literally easier to install the code-signed packageGit for Windows and use the
touchfrom there, as in any case you can use it in any (NT) user context, whereas your suggestion limits you entirely to files and folders owned by the user starting the WSP app (or at least those only accessible with the appropriate ACEs set to modify timestamps). The above port should also work for the same reason. Your method with WSL will only work inside the constraints of the WSL sandbox ...0xC0000022L– 0xC0000022L2019-08-21 12:23:41 +00:00CommentedAug 21, 2019 at 12:23
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- 13PowerShell FTW. I mean, seriously, unix is cool but PS is cooler.matt– matt2011-09-09 14:35:44 +00:00CommentedSep 9, 2011 at 14:35
- 16This will work only on one file. For multiple files: ls * | ForEach-Object {$_.LastWriteTime = Get-Date}Eli Algranti– Eli Algranti2012-10-02 04:52:49 +00:00CommentedOct 2, 2012 at 4:52
- 4If you want to do it recursively, this works pretty well: gci -recu -inc "." | % { $_.LastWriteTime = Get-Date }BrainSlugs83– BrainSlugs832013-01-15 01:43:30 +00:00CommentedJan 15, 2013 at 1:43
- 8@BrainSlugs83: use backticks ` for code:
gci -recu -inc "*.*" | % { $_.LastWriteTime = Get-Date }alldayremix– alldayremix2013-06-29 00:44:54 +00:00CommentedJun 29, 2013 at 0:44 - 7Unix touch is great for it's simplicity.Developer Marius Žilėnas– Developer Marius Žilėnas2016-04-27 04:48:46 +00:00CommentedApr 27, 2016 at 4:48
type nul >>file & copy file +,,- Creates
fileif it does not exist. - Leaves file contents alone.
- Just uses
cmdbuilt-ins. - Bothlast-access andcreation times updated.
UPDATE
Gah! This doesn't work on read-only files, whereastouch does. I suggest:
:touchif not exist "%~1" type nul >>"%~1"& goto :eofset _ATTRIBUTES=%~a1if "%~a1"=="%_ATTRIBUTES:r=%" (copy "%~1"+,,) else attrib -r "%~1" & copy "%~1"+,, & attrib +r "%~1"- 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– Abel2012-05-06 20:05:54 +00:00CommentedMay 6, 2012 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– Synetech2015-09-15 00:57:08 +00:00CommentedSep 15, 2015 at 0:57
- The
type nul ...has an issue with the creation file time on NTFS (Windows 8.1): if the file were erased before, then it sets the erased file time, instead of a new file time.Andry– Andry2024-03-25 01:26:07 +00:00CommentedMar 25, 2024 at 1:26 - The
copy \\?\...variant does not support long paths, whentype nul >> \\?\...does, but only for a new file.Andry– Andry2024-03-25 01:42:01 +00:00CommentedMar 25, 2024 at 1:42
@dash-tom-bang:
Here is Technet's explanation of the mysterious '+' and commas:
copy /b Source+,,The commas indicate the omission ofthe 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
- 17Now that's just messed up syntax. Seriously,what were they thinking? Also note the same documentation says "Destination: Required."... I'm amazed.sth– sth2009-11-25 13:22:39 +00:00CommentedNov 25, 2009 at 13:22
- 1This doesn't seem to even work in Vista... I wonder if they came to their senses?quillbreaker– quillbreaker2009-12-08 20:53:50 +00:00CommentedDec 8, 2009 at 20:53
- 1
- 5It 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– Abgan2011-07-17 11:24:27 +00:00CommentedJul 17, 2011 at 11:24
- 3It worked in Windows Server 2008 but only if you are in the folder containing the fileFrinkTheBrave– FrinkTheBrave2011-12-12 14:48:00 +00:00CommentedDec 12, 2011 at 14:48
I tried this to create an empty file in my batch script. You can use this:
ECHO text>file1.txt- 3This 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>.gitignoreworked a treat - thanks!Alex McMillan– Alex McMillan2014-03-07 04:02:35 +00:00CommentedMar 7, 2014 at 4:02 - 7@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– Brad Cupit2015-04-23 15:26:24 +00:00CommentedApr 23, 2015 at 15:26 - 1I wish that I could give this enough upvotes that it would catch the big players in popularity.Jonathan Mee– Jonathan Mee2016-03-03 16:56:28 +00:00CommentedMar 3, 2016 at 16:56
- 4This doesn't create an empty file though, file1.txt has "text" in it.Joshua Meyers– Joshua Meyers2017-03-15 22:06:12 +00:00CommentedMar 15, 2017 at 22:06
- 3
touchis not only used for creating empty files, and creating empty files is not the usage that the OP is asking about. The question says "What do you use when you want to update the date-modified field of a file?" This answer does not answer that question. Instead, the question this answer answers is: "how do you create a file containing arbitrary text, wiping out existing content if a file of the same name already exists?" That's very much a misleading and possibly dangerous answer to the original question.jez– jez2021-04-02 15:25:01 +00:00CommentedApr 2, 2021 at 15:25
If you feel like coding it yourself, .NET offers theFile.SetLastAccessTime,File.SetCreationTime andFile.SetLastWriteTime methods.
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}- 3Less verbose but yet using only standard Powershell shorthands:
ls * -recurse | % { $_.LastWriteTime = Get-Date }Jonas– Jonas2017-04-05 09:51:03 +00:00CommentedApr 5, 2017 at 9:51
cygwin comes withtouch. 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 thetouch.exe file, and leave the rest of the framework.
- 3To 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– Tall Jeff2008-09-09 10:58:04 +00:00CommentedSep 9, 2008 at 10:58
- 2when 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.dllmatt wilkie– matt wilkie2010-05-27 20:33:28 +00:00CommentedMay 27, 2010 at 20:33
Here's a simple regfile I wrote toadd 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- And don't forget
copy nul some_fileto create an empty file (which is what I seetouchmost often used for).Joey– Joey2009-08-27 06:28:49 +00:00CommentedAug 27, 2009 at 6:28 - Ack! Zip file inaccessible (something about brinkster22.com needing to be the referring site). Jon, can you update this?Dan7119– Dan71192011-03-28 14:22:07 +00:00CommentedMar 28, 2011 at 14:22
- Works on Win7. Does not work on WinXP: file time remains the same after executing "copy".Egor Skriptunoff– Egor Skriptunoff2016-08-25 19:26:52 +00:00CommentedAug 25, 2016 at 19:26
Native win32 ports of many unix commands, including touch.
I've used it before and it works well - no installation, no DLLs, etc
Try this one fromCodeProject.
- No need to install.
- If you want, you can even modify the source.
- 3While 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– Yan Sklyarenko2014-06-05 14:32:40 +00:00CommentedJun 5, 2014 at 14:32
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).Copyall 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\" +,,"- My preferred approach by farNeoheurist– Neoheurist2021-07-02 01:58:02 +00:00CommentedJul 2, 2021 at 1:58
You could also installCygwin which gives you Touch as well as a plethora of other *NIX commands.
- 2can't live on windows without cygwin.jweede– jweede2009-09-10 12:32:52 +00:00CommentedSep 10, 2009 at 12:32
- And cygwin without mintty is pretty lame.Ahe– Ahe2010-01-25 14:30:10 +00:00CommentedJan 25, 2010 at 14:30
- Today, I would recommend WSL in a mintty terminal.Daniel Kaplan– Daniel Kaplan2023-12-11 22:34:16 +00:00CommentedDec 11, 2023 at 22:34
From asimilar question on Stack Overflow.
For updating timestamps (ignoring the other functionality oftouch), I'd go with:
copy /b filename.ext +,,- 1This is a repeat of Gish Domains's answer from 2 yrs prior, without the explanation.fixer1234– fixer12342016-02-20 00:39:07 +00:00CommentedFeb 20, 2016 at 0:39
- 1Welcome to SuperUser. Thanks for posting! Can you add a link to a website that provides more information about the
nicommand, and perhaps describe more what the command does?hBy2Py– hBy2Py2015-06-17 02:17:08 +00:00CommentedJun 17, 2015 at 2:17 - 1The
New-Itemcmdlet can't be used to update the time stamp of an existing file.I say Reinstate Monica– I say Reinstate Monica2015-06-17 02:56:52 +00:00CommentedJun 17, 2015 at 2:56 - While it doesn't update timestamp, it does create a new file, which is what touch does. +1 from me.MikeMurko– MikeMurko2017-06-02 16:59:26 +00:00CommentedJun 2, 2017 at 16:59
- I usually use touch to create files, but occasionally to update timestamps (to force a remake for example)RufusVS– RufusVS2021-07-29 18:47:48 +00:00CommentedJul 29, 2021 at 18:47
How about codeproject "Touch for Windows":http://www.codeproject.com/KB/applications/touch_win.aspx
edit; Same question as here:https://stackoverflow.com/questions/51435/windows-version-of-the-unix-touch-command/51439
- 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– facepalmd2009-07-21 21:52:01 +00:00CommentedJul 21, 2009 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.
If you are using git for one or more projects, the mingw basedgit-bash for Windows has thetouch 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.
I wanted the 'touch' feature of cloning / duplicating the file dates from another file, natively, and be usable from a batch file.
So 'drag and drop' video file on to batch file, FFMPEG runs, then 'Date Created' and 'Date Modified' from the input file gets copied to the output file.
This seemed simple at first until you find batch files are terrible at handling unicode file names, in-line PowerShell messes up with file name symbols, and double escaping them is a nightmare.
My solution was make the 'touch' part a seperate PowerShell script which I called 'CLONE-FILE-DATE.ps1' and it contains:
param( [Parameter(Mandatory=$true)][string]$SourcePath, [Parameter(Mandatory=$true)][string]$TargetPath)(GI -LiteralPath $TargetPath).CreationTime = (GI -LiteralPath $SourcePath).CreationTime(GI -LiteralPath $TargetPath).LastWriteTime = (GI -LiteralPath $SourcePath).LastWriteTimeThen here is example usage within my 'CONVERT.BAT' batch file:
%~dp0\ffmpeg -i "%~1" ACTION "%~1-output.mp4"CHCP 65001 > nul && PowerShell -ExecutionPolicy ByPass -File "%~dp0\CLONE-FILE-DATE.PS1" "%~1" "%~1-output.mp4"I think the PowerShell is readable, so will just explain the batch speak:
%~dp0 is the current directory of the batch file.
%~1 is the path of the file dropped onto the batch without quotes.
CHCP 65001 > nul sets characters to UTF-8 and swallows the output.
-ExecutionPolicy ByPass allows you to run PowerShell without needing to modify the global policy, which is there to prevent people accidentally running scripts.
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 offif %1.==. goto endif not exist %1 goto endcopy /b %1 +,, > nulecho %1 touched!:endSample usage:
touch *.cpptouch somefile.cReference:Microsoft KB 69581
- Excellent! No external stuff, just copy. And of course most of us need it as a batch stript:)Bogdan Gavril MSFT– Bogdan Gavril MSFT2013-03-27 22:07:29 +00:00CommentedMar 27, 2013 at 22:07
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.
- Pointlessly complex, bulky and non-portable. -1MD XF– MD XF2017-05-10 17:45:21 +00:00CommentedMay 10, 2017 at 17:45
- @MDXF As is many windows command line workflows.leetbacoon– leetbacoon2022-12-08 20:39:23 +00:00CommentedDec 8, 2022 at 20:39
Well, if youreally want to have thetouch command available, you can put this in a batch file calledtouch.bat and stick it inC:\Windows:
TYPE NUL >>%1Simple enough.
- This method does NOT update the Modified Time on the following platforms: Windows Server 2012 R2, Windows Server 2016, Windows 10Nate– Nate2019-09-27 16:13:38 +00:00CommentedSep 27, 2019 at 16:13
In powershell:
New-Item .\file.txt -ItemType FileI prefer to writetouch file.txt like in linux, so I define this in my powershell profile
function New-File($filename){ New-Item -ItemType File ".\$filename"}Set-Alias -Name touch -Value New-FileNote: you can edit your profile by running this command
notepad $PROFILETry
fsutil file createnew new.txt 0
- 3
The FSUTIL utility requires that you have administrative privileges.-- and it doesn't behave liketouchfor existing files.Keith Thompson– Keith Thompson2012-02-18 01:39:43 +00:00CommentedFeb 18, 2012 at 1:39
The five alternatives mentioned above, plus three more not mentioned here, can be found on SuperUser:"Windows Recursive Touch Command"
- 2While 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– Yan Sklyarenko2014-06-05 14:32:11 +00:00CommentedJun 5, 2014 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 aGUI (builds into explorer shell):
I appreciate this is an old question, I just discovered touch on my Windows 10 system. I downloaded and installed Git fromhere (I think) and it looks like touch and various other utilities are in the bin folder.
You mustlog in to answer this question.
Explore related questions
See similar questions with these tags.




