Home » Questions » Computers [ Ask a new question ]

Windows equivalent of the Linux command 'touch'?

Windows equivalent of the Linux command 'touch'?

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

Asked by: Guest | Views: 58
Total answers/comments: 5
Guest [Entry]

"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)."
Guest [Entry]

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

"@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"
Guest [Entry]

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

"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}"