Windows equivalent of the Linux command 'touch'?

375,703

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.

Share:
375,703

Related videos on Youtube

facepalmd
Author by

facepalmd

Updated on September 17, 2022

Comments

  • facepalmd
    facepalmd almost 2 years

    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.

  • Tall Jeff
    Tall Jeff almost 16 years
    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.
  • facepalmd
    facepalmd almost 15 years
    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.
  • Joey
    Joey almost 15 years
    And don't forget copy nul some_file to create an empty file (which is what I see touch most often used for).
  • jweede
    jweede almost 15 years
    can't live on windows without cygwin.
  • Admin
    Admin almost 15 years
    This will be slow if the files are large.
  • pyccki
    pyccki over 14 years
    Now that's just messed up syntax. Seriously, what were they thinking? Also note the same documentation says "Destination: Required."... I'm amazed.
  • quillbreaker
    quillbreaker over 14 years
    This doesn't seem to even work in Vista... I wonder if they came to their senses?
  • Ahe
    Ahe over 14 years
    And cygwin without mintty is pretty lame.
  • user1696603
    user1696603 about 14 years
    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
  • Imran
    Imran over 13 years
    Works in Windows 7
  • Dan7119
    Dan7119 over 13 years
    Ack! Zip file inaccessible (something about brinkster22.com needing to be the referring site). Jon, can you update this?
  • Admin
    Admin about 13 years
    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.
  • Mike T.
    Mike T. almost 13 years
    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.
  • matt
    matt almost 13 years
    PowerShell FTW. I mean, seriously, unix is cool but PS is cooler.
  • Admin
    Admin almost 13 years
    You can update the last time write for multiple files / folders as well
  • FrinkTheBrave
    FrinkTheBrave over 12 years
    It worked in Windows Server 2008 but only if you are in the folder containing the file
  • Admin
    Admin over 12 years
    The FSUTIL utility requires that you have administrative privileges. -- and it doesn't behave like touch for existing files.
  • Abel
    Abel about 12 years
    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.
  • Admin
    Admin about 12 years
    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.
  • Eli Algranti
    Eli Algranti over 11 years
    This will work only on one file. For multiple files: ls * | ForEach-Object {$_.LastWriteTime = Get-Date}
  • Admin
    Admin over 11 years
    @Jamie yes, I also meet this problem.
  • BrainSlugs83
    BrainSlugs83 over 11 years
    If you want to do it recursively, this works pretty well: gci -recu -inc "." | % { $_.LastWriteTime = Get-Date }
  • BrainSlugs83
    BrainSlugs83 over 11 years
    Not 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
    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
    Bogdan Gavril MSFT over 11 years
    Excellent! No external stuff, just copy. And of course most of us need it as a batch stript:)
  • Admin
    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
    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
    alldayremix about 11 years
    @BrainSlugs83: use backticks ` for code: gci -recu -inc "*.*" | % { $_.LastWriteTime = Get-Date }
  • Admin
    Admin almost 11 years
    I created a GIST combining this answer with this one to automatically push/pop destination directory
  • Admin
    Admin over 10 years
    @TJB batch files might consider starting with @echo OFF
  • Admin
    Admin over 10 years
    If 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 - where Source includes the full path to the file
  • void
    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
    RomanSt about 10 years
    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"
  • Admin
    Admin about 10 years
  • Yan Sklyarenko
    Yan Sklyarenko about 10 years
    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
    Yan Sklyarenko about 10 years
    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.
  • Brad Cupit
    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
    hBy2Py about 9 years
    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?
  • I say Reinstate Monica
    I say Reinstate Monica about 9 years
    The New-Item cmdlet can't be used to update the time stamp of an existing file.
  • Admin
    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 the copy command in Windows has the touch command specially coded in, per the Technet article. +1 Great find; I never knew this one.
  • Synetech
    Synetech almost 9 years
    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).
  • barlop
    barlop over 8 years
    gnuwin32 has it. but indeed, gnuwin32's also fails for unicode characters.
  • fixer1234
    fixer1234 over 8 years
    This is a repeat of Gish Domains's answer from 2 yrs prior, without the explanation.
  • Jonathan Mee
    Jonathan Mee over 8 years
    I wish that I could give this enough upvotes that it would catch the big players in popularity.
  • Developer Marius Žilėnas
    Developer Marius Žilėnas about 8 years
    Unix touch is great for it's simplicity.
  • Egor Skriptunoff
    Egor Skriptunoff almost 8 years
    Works on Win7. Does not work on WinXP: file time remains the same after executing "copy".
  • Joshua Meyers
    Joshua Meyers over 7 years
    This doesn't create an empty file though, file1.txt has "text" in it.
  • Jonas
    Jonas about 7 years
    Less verbose but yet using only standard Powershell shorthands: ls * -recurse | % { $_.LastWriteTime = Get-Date }
  • MD XF
    MD XF about 7 years
    Pointlessly complex, bulky and non-portable. -1
  • Gatonito
    Gatonito about 7 years
    While it doesn't update timestamp, it does create a new file, which is what touch does. +1 from me.
  • Admin
    Admin almost 7 years
    One small tiny little problem: it does copy all the files; it places them into CWD.
  • Admin
    Admin almost 7 years
    Windows XP doesn't like copying the file over itself from outside CWD. For this, use pushd "%~dp1%" before the CWD "touch", and popd afterward. (In case you're stuck supporting one of the millions of remaining XP machines...)
  • Davos
    Davos over 6 years
    Update 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
    Admin over 5 years
    This 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
    user9399 over 5 years
    This 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
    Admin over 5 years
    This is not an adequate replacement for the touch command, as it doesn't create the file if it doesn't exist.
  • 0xC0000022L
    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
    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
    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 with sudo/su, too). Everything in WSL (v1) is running as your NT user and has the same privileges, no matter if you use sudo or su within WSL. It's a sandboxed environment, comparable to unprivileged LXD/LXC containers. Very limiting. You see now?
  • 0xC0000022L
    0xC0000022L almost 5 years
    These 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
    Davos almost 5 years
    Sure 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
    Davos almost 5 years
    "Very limiting", depends on what you're up to doesn't it.
  • Admin
    Admin almost 5 years
    this is not as useful - the most important feature, copying the timestamps from file1 to file2 with touch file2 -r file1, isn't handled.
  • Nate
    Nate almost 5 years
    This method does NOT update the Modified Time on the following platforms: Windows Server 2012 R2, Windows Server 2016, Windows 10
  • Mike Rosoft
    Mike Rosoft over 4 years
    So: 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
    Admin about 4 years
    Know this is a very old thread but easiest way now with a PowerShell is New-Item's Alias: ni <filename>
  • Admin
    Admin almost 4 years
    thanks. Clearly a hack by MS, poor quality and poorly documented since "help copy" does not mention it
  • Admin
    Admin over 3 years
    why is the /b parameter needed?
  • jez
    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
    Admin about 3 years
    @Youda008 - /b tells copy 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
    Admin almost 3 years
    My preferred approach by far
  • RufusVS
    RufusVS almost 3 years
    I usually use touch to create files, but occasionally to update timestamps (to force a remake for example)