Sunday, June 15, 2008

touch command for DOS/command line


Following command would simulate UNIX touch command in DOS:
copy /b test.txt +,,
(command was changed following to the comments).

16 comments:

Robert Campion said...

Ye, or....

just erases the file!

Alex Pinsker said...

No, it doesn't erases that file, it just refreshes the date.

Anonymous said...

I just tried this command from the cmd line in Windows XP and it erased the file. The following command is working:

copy /b test.txt +,,

Matt said...

For me, the file size becomes 0. Anyway, the other command works fine.

wshatner32 said...

When you copy NULL to a file, the file is still there, but it's now empty. The file is not much use after that.

Alex Pinsker said...

The original command is updated; it's ok now.

Server-India said...

Thanks for the update, newbies still need to work hard to get it done right ;)

StephenF said...

Thanks - I took it and made a wrapper, mytouch.cmd, which is in a folder in my PATH env var. If using on files with embedded spaces, just enclose the filename in quotes on the command line, it seems to work nicely:

mytouch.cmd:

@echo off
set arg1=%*
%arg1%
copy /b %arg1% +,,


SAMPLE USAGE:
mytouch "some old file.txt"


It simply results as you have indicated, in the file's datestamp being updated to current datetime.

COOL. Thanks again for the simple syntax for the copy with "/b" flag.

Anonymous said...

thanks. it's working.. But, how to make it to run for the whole folder?? Any idea?

Unknown said...

@echo off
rem Creates empty files

if not '%1'=='' goto loop
echo Usage: touch file...
echo creates empty files
goto done

:loop
type nul >> %1
shift
if not '%1'=='' goto loop

:done





////////
Put the above code into a batch script. Place it in a file area for personal batch scripts. Call that file touch. Make sure the file area is on the system PATH or the local user PATH variable. Then you can use the command to make blank files.

Anonymous said...

Interesting... I used mytouch.cmd on a PowerPoint document, and it opened the file. Of course, when I closed it, the timestamp had changed. (The simple copy command worked with no such interesting behavior.)

Anonymous said...

Hey Read it before you use it!

What do you guess will %arg1% do?
Be happy it was not makeClean.bat you where using it.

I think the idea of a wrapper is OK but:
why not just copy %* +,,

The main trick is the "+,," thing, thanks for that!

Anonymous said...

Gary's script works if there is no BLANK in the filename, maybe he can improve it so it accepts BLANKS in the filename

Anonymous said...

Good one!

Anonymous said...

Good one!

Anonymous said...

Good One! Thanks!