views:

15540

answers:

14

I'm looking for a Windows port of the UNIX touch command. I don't want to install an entire MKS toolkit just for the one tool. Is there a native port available somewhere or a command in Windows that does the same thing and supports features like all files in a directory by wildcard?

Specifically I'm after changing mtime, ctime and atime for a project that reports ages of files based on... mtime, ctime and atime.

+39  A: 

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
Would upvote twice if I could. This is one of the tools I install after a fresh install, can't get out of the habit of typing ls at any command line I touch.
Jamie Penney
+5  A: 

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
A: 

@Greg: @Adam: Thanks guys, I was also looking for the touch utility, but after some unsuccessfull searches we wrote our own. :-) I will definitely have a look on unxutils.

Biri
+5  A: 

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
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.
Tall Jeff
when I try this (win7x64) I need 4 cygwin dll's in addition to touch.exe: cygiconv-2.dllcygintl-8.dllcygwin1.dllcyggcc_s-1.dll
matt wilkie
+4  A: 

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.

Dave Webb
+2  A: 

Try this one from CodeProject.

  • No need to install.
  • If you want, you can even modify the source.
Prakash
+13  A: 

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 files name)

Gish Domains
This will be slow if the files are large.
mobrule
What is this doing? Where is it documented? (Google fails to index "+,,", and the MS site is not leading me in the right direction as searching on 'copy' turns up a lot of nothing useful.) I mean, it works great and I'll take that much for granted, but I am curious.
dash-tom-bang
A: 

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, quite, force". This will open file, do a save, then close it immediately afterward.

Jason
A: 

Hey Jason,

Your answer seems to be doing a trick. Thanks :)

But ideally it would NOT be as effective as 'touch'. Is nt it?

Raghavan alias Saravanan M
this should be a comment, not an answer
Jason S
+4  A: 

@dash-tom-bang:

Here is MSDN'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 MSDN's copy command reference: http://technet.microsoft.com/en-us/library/bb490886.aspx

Now that's just messed up syntax. Seriously, *what were they thinking?* Also note the same documentation says "Destination: Required."... I'm amazed.
sth
This doesn't seem to even work in Vista... I wonder if they came to their senses?
quillbreaker
A: 

how about this

echo "" > file

Rajesh
Sets the file's contents to `""` instead of leaving it unmodified.
MiffTheFox
MiffTheFox, do this instead: echo "" >> filenameThat's convenient in that it creates the file if it doesn't exist just like touch.
David
Creating an empty file is probably the most known function of touch, but it does more. The command purpose is documented as "change file access and modification times".
Philippe A.
A: 

copy NUL YourFile.txt

Art Saisuphaluck
um.... doesn't that erase my file?
Jason S
A: 

Hi,

I just published my implementation of touch for Win32 at http://www.touchdotexe.com

It's free and open source. It supports all options listed in the POSIX:2008 spec but I also added extra touches like Unicode support, a recursive mode and a verbose mode and even pause on exit for running without cmd.exe. It also lets you change files creation time.

Let me know what you think, my contact info is on the touch.exe website.

Stéphane Duguay
A: 

Try

fsutil file createnew new.txt 0

Peter Hacke