On a windows machine I get this error
\'touch\' is not recognized as an internal or external command, operable program or batch file.
For a very simple version of touch
which would be mostly used to create a 0 byte file in the current directory, an alternative would be creating a touch.bat
file and either adding it to the %Path%
or copying it to the C:\Windows\System32
directory, like so:
touch.bat
@echo off
powershell New-Item %* -ItemType file
Creating a single file
C:\Users\YourName\Desktop>touch a.txt Directory: C:\Users\YourName\Desktop Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 2020-10-14 10:28 PM 0 a.txt
Creating multiple files
C:\Users\YourName\Desktop>touch "b.txt,c.txt" Directory: C:\Users\YourName\Desktop Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 2020-10-14 10:52 PM 0 b.txt -a---- 2020-10-14 10:52 PM 0 c.txt
Also
New-Item : The file 'C:\Users\YourName\Desktop\a.txt' already exists.
C:\Users\YourName\Desktop>touch d.txt,e.txt,f.txt C:\Users\YourName\Desktop>touch "g.txt, 'name with spaces.txt'"
You can replicate the functionality of touch with the following command:
$>>filename
What this does is attempts to execute a program called $
, but if $
does not exist (or is not an executable that produces output) then no output is produced by it. It is essentially a hack on the functionality, however you will get the following error message:
'$' is not recognized as an internal or external command, operable program or batch file.
If you don't want the error message then 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 an EOF (end of file) when read.
2>nul
sends error-output (output 2) to nul (which ignores all input when written to). Obviously the second command (with 2>nul
) is made redundant by the type
command since it is quicker to type. But at least you now have the option and the knowledge.
You can use this command: ECHO >> filename.txt
it will create a file with the given extension in the current folder.
UPDATE:
for an empty file use: copy NUL filename.txt
I'm surprised how many answers here are just wrong. Echoing nothing into a file will fill the file with something like ECHO is ON
, and trying to echo $nul
into a file will literally place $nul
into the file. Additionally for PowerShell, echoing $null
into a file won't actually make a 0kb file, but something encoded as UCS-2 LE BOM
, which can get messy if you need to make sure your files don't have a byte-order mark.
After testing all the answers here and referencing some similar ones, I can guarantee these will work per console shell. Just change FileName.FileExtension
to the full or relative-path of the file you want to touch
; thanks to Keith Russell for the COPY NUL FILE.EXT
update:
copy NUL FileName.FileExtension
This will create a new file named whatever you placed instead of FileName.FileExtension
with a size of 0 bytes. If the file already exists it will basically copy itself in-place to update the timestamp. I'd say this is more of a workaround than 1:1 functionality with touch
but I don't know of any built-in tools for CMD that can accomplish updating a file's timestamp without changing any of its other content.
if not exist FileName.FileExtension copy NUL FileName.FileExtension
if (!(Test-Path FileName.FileExtension -PathType Leaf)) {New-Item FileName.FileExtension -Type file} else {(ls FileName.FileExtension ).LastWriteTime = Get-Date}
Yes, it will work in-console as a one-liner; no requirement to place it in a PowerShell script file.
if (!(Test-Path FileName.FileExtension -PathType Leaf)) {New-Item FileName.FileExtension -Type file}
Easy, example with txt file
echo $null >> filename.txt
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.