The Windows equivalent of "touch" (ie. The way node.js to create index.html)

On a windows machine, I get this error

"touch" is not recognized as an internal or external command, a running program, or a batch file.

when i follow the instructions :

touch index.html app.js style.css 

Is there an equivalent to using "touch" on Windows? Do I need to create these files manually (and modify them to change the timestamp) to implement this kind of command? It doesn't seem very ... node ...

+148
cmd
May 03 '15 at 7:20
source share
15 answers

in a window like cmd:

 type nul > your_file.txt 

This will create 0 bytes in your_file.txt file.

Another way to do this is to use the echo command:

 echo.> your_file.txt 

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

Edited 2019-04-01:

If you need to save the contents of the file, use >> instead of>

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

example

 type nul >> your_file.txt 
+216
Jun 10 '16 at 20:43
source share

Windows does not include the touch command.

You can use any of the available public versions or use your own version. Save this code as touch.cmd and put 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 the argument list, and for each element, if it exists, update the timestamp of the file, otherwise create it.

+57
May 03, '15 at 20:27
source share

You can use this command: ECHO β†’ filename.txt

it will create a file with the specified extension in the current folder.

UPDATE:

to use an empty file: copy NUL filename.txt

+30
Feb 23 '16 at 21:42
source share

The answer is incorrect, it only works when the file does not exist. If the file exists, using the first does not give anything, the second adds a line to the end of the file.

Correct answer:

 copy /b filename.ext +,, 

I found this here: https://superuser.com/questions/10426/windows-equivalent-of-the-linux-command-touch/764721#764721

+24
Mar 15 '18 at 20:04
source share

Use the following command on the command line:

 fsutil file createnew filename requiredSize 

Parameter Information:

fsutil - file system utility (the executable you are using)

file - starts a file action

createnew - action to execute (create a new file)

filename - will literally be the file name

requiredSize - allocates the file size in bytes in the created file

+10
May 03 '15 at 7:44
source share

I am surprised how many answers here are simply incorrect. If you don’t insert anything into the file, the file will be filled with something like ECHO is ON , and an attempt to map $nul to the file will literally put $nul in the file. Also, for PowerShell, repeating $null in a file does not actually result in a 0K file, but something encoded as UCS-2 LE BOM , which can lead to confusion if you need to make sure your files do not have byte order marks.,

After testing all the answers here and referring to some similar ones, I can guarantee that they will work for each console shell. Just change FileName.FileExtension to the full or relative path to the file you want to touch :

CMD

if not exist FileName.FileExtension(fsutil file CreateNew FileName.FileExtension 0) else (copy/b FileName.FileExtension +,)

This will create a new file with the name no matter what you put instead of FileName.FileExtension with a length of 0 bytes. It will not do anything with this file other than updating the timestamp if it already exists using the else copy operation, it seems, but probably not quite the way touch works. I would say that this is more of a workaround than 1: 1 functionality, but I don’t know of any built-in tools for CMD that could update the file’s timestamp without changing any other content.

Powershell

if (!(Test-Path FileName.FileExtension -PathType Leaf)) {New-Item FileName.FileExtension -Type file} else {(ls FileName.FileExtension).LastWriteTime = Get-Date}

This has the same functionality as the CMD version. It will not link to the existing file except its timestamp, and will create a new empty file of what you decided to use instead of FileName.FileExtension . And yes, it will work in the console as a single line; no need to put it in a PowerShell script file.

What if I do not want to change the timestamp?

If you do not want to change the timestamp of an existing file, we can simply remove the else -clauses.

CMD

if not exist FileName.FileExtension fsutil file CreateNew FileName.FileExtension 0

Powershell

if (!(Test-Path FileName.FileExtension -PathType Leaf)) {New-Item FileName.FileExtension -Type file}

+8
Nov 13 '18 at 1:34
source share

You can play the touch functionality with the following command:

 $>>filename 

We are talking about trying to run a program called $ , but if $ does not exist (or is not an executable file that produces output), then it does not output it. This is essentially a hack in functionality, however you will receive the following error message:

'$' is not recognized as an internal or external command, operating program, or batch file.

If you do not want an error message, you can do one of two things:

 type nul >> filename 

Or:

 $>>filename 2>nul 

The type command tries to display the contents of nul, which does nothing but returns EOF (end of file) when reading.

2>nul sends an error output (pin 2) to nul (which ignores all input when writing to). Obviously, the second command (with 2>nul ) is redundant with the type command, since it is faster to type. But at least you now have a choice and knowledge.

+6
Jul 01 '17 at 9:40 on
source share

No command β€” neither type nor echo β€” is required to emulate the Unix / Mac OS X "touch" command in a Windows Powershell terminal. Just use the following abridged version:

 $null > filename 

This will create an empty file named 'filename' at your current location. Use any file name extension you may need, for example. '.txt'.

Source: https://superuser.com/questions/502374/equivalent-of-linux-touch-to-create-an-empty-file-with-powershell (see comments)

+5
Mar 15 '17 at 10:17
source share

In Windows Power Shell, you can use the following command:

 New-Item <filename.extension> 

or

 New-Item <filename.extension> -type file 

Note. You can replace the new item with its alias ni.

+5
Nov 05 '18 at 7:05
source share

You can also use copy con [filename] in the Windows command window (cmd.exe):

 C:\copy con yourfile.txt [enter] C:\CTRL + Z [enter] //hold CTRL key & press "Z" then press Enter key. ^Z 1 Files Copied. 

This will create a file called yourfile.txt in the local directory.

+3
Sep 13 '16 at 21:08
source share

as mentioned

 echo >> index.html 

it can be any file with any extension

 notepad index.html 

this will open your file in notepad editor

+3
Jul 20 '18 at 12:42
source share

If you have Cygwin installed on your computer, you can simply use the attached executable file for the touch screen (also via the Windows command line):

 C:\cygwin64\bin\touch.exe <file_path> 
0
Jun 12 '19 at 8:37
source share
 ni <file>.<extension> 
0
Jul 10 '19 at 13:09 on
source share

Easy example with TXT file

 echo $null >> filename.txt 
-one
Feb 22 '18 at 20:20
source share

Yes, you can use Node for Touch, I just use this and everything works fine in Windows Cmd or Gitbash

enter image description here

-one
Apr 11 '18 at 20:09
source share



All Articles