The Computer Oracle

Windows equivalent of the Linux command 'touch'?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Track title: CC C Schuberts Piano Sonata No 13 D

--

Chapters
00:00 Question
00:53 Accepted answer (Score 431)
02:06 Answer 2 (Score 168)
02:23 Answer 3 (Score 109)
02:39 Answer 4 (Score 67)
03:20 Thank you

--

Full question
https://superuser.com/questions/10426/wi...

Question links:
[here]: https://stackoverflow.com/questions/5143...
[WSH]: http://en.wikipedia.org/wiki/Windows_Scr...

Accepted answer links:
[update the date/time on the file]: http://technet.microsoft.com/en-us/libra...

Answer 2 links:
[unxutils]: http://unxutils.sourceforge.net/

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#windows #fileattributes #touch

#avk47



ACCEPTED ANSWER

Score 440


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).




ANSWER 2

Score 168


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




ANSWER 3

Score 69


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"



ANSWER 4

Score 33


@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