Windows equivalent of the Linux command 'touch'?
Solution 1
I've used and recommend unxutils which are native Win32 ports of lots of common Unix utilities. There is a touch
command in there.
Solution 2
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
Solution 3
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"
Solution 4
@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
Solution 5
If you feel like coding it yourself, .NET offers the File.SetLastAccessTime
, File.SetCreationTime
and File.SetLastWriteTime
methods.
Related videos on Youtube
facepalmd
Updated on September 17, 2022Comments
-
facepalmd almost 2 years
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 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.
-
Admin over 13 yearsThe four alternatives mentioned above, plus four more not mentioned here, can be found in the answer to a similar question: "Windows Recursive Touch Command"
-
Admin about 9 yearsstackoverflow.com/questions/210201/… mentions a load, including
REM.>a
-
Admin over 4 yearsnpmjs.com/package/touch-for-windows works for me
-
Tall Jeff almost 16 yearsTo 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.
-
facepalmd almost 15 yearsThanks. I missed that in my search which resulted in loads of touch screen phone related stuff. Probably needs a better tag label I guess.
-
Joey almost 15 yearsAnd don't forget
copy nul some_file
to create an empty file (which is what I seetouch
most often used for). -
jweede almost 15 yearscan't live on windows without cygwin.
-
Admin almost 15 yearsThis will be slow if the files are large.
-
pyccki over 14 yearsNow that's just messed up syntax. Seriously, what were they thinking? Also note the same documentation says "Destination: Required."... I'm amazed.
-
quillbreaker over 14 yearsThis doesn't seem to even work in Vista... I wonder if they came to their senses?
-
Ahe over 14 yearsAnd cygwin without mintty is pretty lame.
-
user1696603 about 14 yearswhen 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
-
Imran over 13 yearsWorks in Windows 7
-
Dan7119 over 13 yearsAck! Zip file inaccessible (something about brinkster22.com needing to be the referring site). Jon, can you update this?
-
Admin about 13 years
type touch.bat
is@echo off
and next linecopy /b %1 +,,
- put it intoC:\bin
or what have you for your own scripts, and then you can use it liketouch myfile.txt
. -
Mike T. almost 13 yearsIt 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.
-
matt almost 13 yearsPowerShell FTW. I mean, seriously, unix is cool but PS is cooler.
-
Admin almost 13 yearsYou can update the last time write for multiple files / folders as well
-
FrinkTheBrave over 12 yearsIt worked in Windows Server 2008 but only if you are in the folder containing the file
-
Admin over 12 years
The FSUTIL utility requires that you have administrative privileges.
-- and it doesn't behave liketouch
for existing files. -
Abel about 12 yearsIt'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. -
Admin about 12 yearsIn 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.
-
Eli Algranti over 11 yearsThis will work only on one file. For multiple files: ls * | ForEach-Object {$_.LastWriteTime = Get-Date}
-
Admin over 11 years@Jamie yes, I also meet this problem.
-
BrainSlugs83 over 11 yearsIf you want to do it recursively, this works pretty well: gci -recu -inc "." | % { $_.LastWriteTime = Get-Date }
-
BrainSlugs83 over 11 yearsNot sure why, but the asterisks aren't showing up in my above comment -- the "." should be double-quote, asterisk, dot, asterisk, double-quote (you know, "star-dot-star".) Anyway, tried to lookup the help to figure out why, but now it's not editable. Darn.
-
Admin over 11 years@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).
-
Bogdan Gavril MSFT over 11 yearsExcellent! No external stuff, just copy. And of course most of us need it as a batch stript:)
-
Admin about 11 years@Jamie : Not related to Win7, applies to all OS. You need to be in the current directory as ,, will take the very same file with no path information (see technet link from josmh comment)
-
Admin about 11 years@Gish Domains: would you like to promote your answer as Community wiki. I think it is very helpful, although not marked as answer.
-
alldayremix about 11 years@BrainSlugs83: use backticks ` for code:
gci -recu -inc "*.*" | % { $_.LastWriteTime = Get-Date }
-
Admin almost 11 years
-
Admin over 10 years@TJB batch files might consider starting with
@echo OFF
-
Admin over 10 yearsIf 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
- whereSource
includes the full path to the file -
void over 10 years
touch
sets the time to a specific one or "now" if none is given..LastWriteTime
is the same, so you can do something like.LastWriteTime = '12/30/99 23:59'
. @matt: most likely, you're not very familiar with the *nix shell; PS still seems to have a long way to go --e.g.,sed
?grep
?vi
? :p EDIT: Why are my Shift+Enter ignored? How to add new lines? -
RomanSt about 10 yearsAs 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"
-
Admin about 10 years
-
Yan Sklyarenko about 10 yearsWhile 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 about 10 yearsWhile 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.
-
Brad Cupit about 9 years@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. -
hBy2Py about 9 yearsWelcome 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? -
I say Reinstate Monica about 9 yearsThe
New-Item
cmdlet can't be used to update the time stamp of an existing file. -
Admin almost 9 years
This will be slow if the files are large.
Actually, it is not slow, it does it in less than one second, and does not even light up the HD-access LED. Clearly thecopy
command in Windows has thetouch
command specially coded in, per the Technet article. +1 Great find; I never knew this one. -
Synetech almost 9 yearsUnfortunately 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).
-
barlop over 8 yearsgnuwin32 has it. but indeed, gnuwin32's also fails for unicode characters.
-
fixer1234 over 8 yearsThis is a repeat of Gish Domains's answer from 2 yrs prior, without the explanation.
-
Jonathan Mee over 8 yearsI wish that I could give this enough upvotes that it would catch the big players in popularity.
-
Developer Marius Žilėnas about 8 yearsUnix touch is great for it's simplicity.
-
Egor Skriptunoff almost 8 yearsWorks on Win7. Does not work on WinXP: file time remains the same after executing "copy".
-
Joshua Meyers over 7 yearsThis doesn't create an empty file though, file1.txt has "text" in it.
-
Jonas about 7 yearsLess verbose but yet using only standard Powershell shorthands:
ls * -recurse | % { $_.LastWriteTime = Get-Date }
-
MD XF about 7 yearsPointlessly complex, bulky and non-portable. -1
-
Gatonito about 7 yearsWhile it doesn't update timestamp, it does create a new file, which is what touch does. +1 from me.
-
Admin almost 7 yearsOne small tiny little problem: it does copy all the files; it places them into CWD.
-
Admin almost 7 yearsWindows XP doesn't like copying the file over itself from outside CWD. For this, use
pushd "%~dp1%"
before the CWD "touch", andpopd
afterward. (In case you're stuck supporting one of the millions of remaining XP machines...) -
Davos over 6 yearsUpdate 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-win10
-
Admin over 5 yearsThis did not work for me at all unless I added additional code to CD to the dir and CD back. Without that, even the copy that is created in the local directory still had the original timestamp despite the +,, flags. I used the powershell 1-liner instead and it worked perfectly
-
user9399 over 5 yearsThis worked perfectly. From a batch file, I could use a one-liner if I just surrounded the powershell command with double-quotes:
powershell "(ls your-file-name-here).LastWriteTime = Get-Date"
or alternatively to set the date to another file's:powershell "(ls your-file-name-here).LastWriteTime = (ls the-other-file-name-here).LastWriteTime"
-
Admin over 5 yearsThis is not an adequate replacement for the
touch
command, as it doesn't create the file if it doesn't exist. -
0xC0000022L almost 5 years@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.
-
Davos almost 5 years@0xC0000022L not sure what you mean in the context of the
touch
command. It's like anything else in nix environments, you can either do it or you can sudo it, or you can't because you don't have privileges. -
0xC0000022L almost 5 years@Davos user context! Start whatever WSL flavor you have installed. Now I am assuming you have drive C: and it's NTFS (because the
%SystemDrive%
has to be). Inside your WSL shell prompt, execute the following:touch /mnt/c/Windows/System32
(of course feel free to try that as well with other commands and go right ahead and try it withsudo
/su
, too). Everything in WSL (v1) is running as your NT user and has the same privileges, no matter if you usesudo
orsu
within WSL. It's a sandboxed environment, comparable to unprivileged LXD/LXC containers. Very limiting. You see now? -
0xC0000022L almost 5 yearsThese days it's literally easier to install the code-signed package Git for Windows and use the
touch
from 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 ... -
Davos almost 5 yearsSure do that then, the MingGW that comes with git for windows is certainly a decent option. It's arguable that the WSL v1 is doing it right though, I mean what are you doing creating files in
System32
for? The windows OS is supposed to be privileged kernel space and the ability to clobber it with normal user privileges is a bizarre design decision. There are some fundamental differences between FS permissions on linux vs windows which exposes the fact that it's the norm for win users to run as default admins. A clear impedance in reconciling, for example, docker file mounts or in the WSL -
Davos almost 5 years"Very limiting", depends on what you're up to doesn't it.
-
Admin almost 5 yearsthis is not as useful - the most important feature, copying the timestamps from file1 to file2 with
touch file2 -r file1
, isn't handled. -
Nate almost 5 yearsThis method does NOT update the Modified Time on the following platforms: Windows Server 2012 R2, Windows Server 2016, Windows 10
-
Mike Rosoft over 4 yearsSo:
copy foo.txt+bar.txt qux.txt
concatenates the two files and writes the result to the third file;copy foo.txt+bar.txt
- if the destination file is omitted, it writes the result to the first file in the list;copy foo.txt+,,
- concatenate file with "nothing" (just sets the last modification time),copy foo.txt
wouldn't work;/b
parameter means to treat the file as a binary file. I have also verified that running the command on a very large file returns immediately. -
Admin about 4 yearsKnow this is a very old thread but easiest way now with a PowerShell is
New-Item
's Alias:ni <filename>
-
Admin almost 4 yearsthanks. Clearly a hack by MS, poor quality and poorly documented since "help copy" does not mention it
-
Admin over 3 yearswhy is the /b parameter needed?
-
jez about 3 years
touch
is 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. -
Admin about 3 years@Youda008 -
/b
tellscopy
that the file is binary rather than text. Also, as noted by Raymond Chen, the trailing,,
at the end of the command is superfluous. -
Admin almost 3 yearsMy preferred approach by far
-
RufusVS almost 3 yearsI usually use touch to create files, but occasionally to update timestamps (to force a remake for example)