255

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.

facepalmd
  • 664

32 Answers32

444

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

169

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

Greg Hewgill
  • 5,619
115

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
ThiagoAlves
  • 1,271
70
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"
bobbogo
  • 1,283
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

phuclv
  • 30,396
  • 15
  • 136
  • 260
Faredoon
  • 430
23

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

23

I tried this to create an empty file in my batch script. You can use this:

ECHO text>file1.txt
Pang
  • 1,017
pkm
  • 354
22

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}
roger
  • 321
17

The GnuWin32 project has Windows ports of the Gnu versions of the Unix command line utilities.

It comes as a number of separate packages and you can install just the commands you need with no other dependencies. For touch you would need the CoreUtils package.

David Webb
  • 12,294
15

Here's a simple regfile I wrote to add 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
15

cygwin comes with touch. 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 the touch.exe file, and leave the rest of the framework.

serg10
  • 349
13

Native win32 ports of many unix commands, including touch.

I've used it before and it works well - no installation, no DLLs, etc

Adam Davis
  • 4,405
10

Try this one from CodeProject.

  • No need to install.
  • If you want, you can even modify the source.
Prakash
  • 209
10

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). Copy all 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\" +,,"
9

You could also install Cygwin which gives you Touch as well as a plethora of other *NIX commands.

5

There are Windows ports of many Unix utilities. Have a look at unxutils or GnuWin32 projects.

VladV
  • 121
5

From a similar question on Stack Overflow.

For updating timestamps (ignoring the other functionality of touch), I'd go with:

copy /b filename.ext +,,
5

in PowerShell try:

ni fileName.txt

NI is an alias of the New-Item cmdlet.

Monfa.red
  • 101
4

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

4

If you are using git for one or more projects, the mingw based git-bash for Windows has the touch 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.

Superole
  • 1,829
4

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.

3

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

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

WhoIsRich
  • 464
2

Well, if you really want to have the touch command available, you can put this in a batch file called touch.bat and stick it in C:\Windows:

TYPE NUL >>%1

Simple enough.

MD XF
  • 224
2

In powershell:

New-Item .\file.txt -ItemType File

I prefer to write touch 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-File

Note: you can edit your profile by running this command

notepad $PROFILE
Kellen Stuart
  • 618
  • 3
  • 10
  • 20
2

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 off
if %1.==. goto end
if not exist %1 goto end
copy /b %1 +,, > nul
echo %1 touched!
:end

Sample usage:

touch *.cpp
touch somefile.c

Reference: Microsoft KB 69581

2

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.

Jason
  • 201
1

I appreciate this is an old question, I just discovered touch on my Windows 10 system. I downloaded and installed Git from here (I think) and it looks like touch and various other utilities are in the bin folder.

0909EM
  • 119
1

Try

fsutil file createnew new.txt 0

1

The five alternatives mentioned above, plus three more not mentioned here, can be found on SuperUser: "Windows Recursive Touch Command"

JdeBP
  • 27,556
  • 1
  • 77
  • 106
1

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 a GUI (builds into explorer shell):

http://www.jddesign.co.uk/products/touchpro/touchpro.htm

rustyx
  • 1,118
0

Because I needed this also and a number of other things, I've created my own shell extension that does this: AllSorts I've also open-sourced this here

0

The Unix people fixed the general problem of updating the file date of any file with the touch command. However, for Windows, sometimes a simpler method is possible for special cases.

I need to update the timestamp of an application shortcut in Windows 8.1 in order to make changes to the background color of the Application Tile visible, see this SO question. Rather than implementing one of the clever tools above, I find it easier to edit the comment field of the shortcut. Most people leave this empty, but of course, a useful comment is quickly conceived. And if a comment exists, adding or removing a final period does never harm.

Roland
  • 427