Create an empty file on the commandline in windows (like the linux touch command)

On a windows machine I get this error

'touch' is not recognized as an internal or external command, operable program or batch file.

I was following these instructions which seem to be linux specific, but on a standard windows commandline it does not work like this:

touch index.html app.js style.css

Is there a windows equivalent of the 'touch' command from the linux / mac os / unix world ? Do I need to create these files by hand (and modify them to change the timestamp) in order to implement this sort of command? I am working with node and that doesn't seem very ... node-ish...


An easy way to replace the touch command on a windows command line like cmd would be:

type nul > your_file.txt

This will create 0 bytes in the your_file.txt file.

This would also be a good solution to use in windows batch files.

Another way of doing it is by using the echo command:

echo.> your_file.txt

echo. - will create a file with one empty line in it.


If you need to preserve the content of the file use >> instead of >

>   Creates a new file
>>  Preserves content of the file

Example

type nul >> your_file.txt

You can also use call command.

Calls one batch program from another without stopping the parent batch program. The call command accepts labels as the target of the call.

Example:

call >> your_file.txt

--- or even if you don't want make it hard you can Just install Windows Subsystem for Linux (WSL). Then, type.

wsl touch

or

wsl touch textfilenametoedit.txt

Quotes are not needed.


Windows does not natively include a touch command.

You can use any of the available public versions or you can use your own version. Save this code as touch.cmd and place it somewhere in your path

@echo off
    setlocal enableextensions disabledelayedexpansion

    (for %%a in (%*) do if exist "%%~a" (
        pushd "%%~dpa" && ( copy /b "%%~nxa"+,, & popd )
    ) else (
        type nul > "%%~fa"
    )) >nul 2>&1

It will iterate over it argument list, and for each element if it exists, update the file timestamp, else, create it.