-1

I am trying to create an empty file using terminal in vs-code but the command line isn't in the mood to work. I don't understand whats wrong?

This is the error when I try to make the file:

touch : The term 'touch' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path 
was included, verify that the path is correct and try again.
At line:1 char:1
+ touch app.js
+ ~~~~~
    + CategoryInfo          : ObjectNotFound: (touch:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
tripleee
  • 139,311
  • 24
  • 207
  • 268
  • Where are you getting the term `touch` from? What are you trying to do – Abraham Zinala May 23 '21 at 13:05
  • 2
    If you mean to have a function you can call wityh `touch` in PowerShell, maybe [this](https://stackoverflow.com/a/51842880/9898643) is what you're looking for? – Theo May 23 '21 at 13:17
  • i am trying to create an empty file – Neel Kotkar May 23 '21 at 13:22
  • 5
    Then have a look at the [New-Item](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-item) cmdlet. – Theo May 23 '21 at 13:24
  • You can also do `$null > app.js`. Easy way of creating files. – Santiago Squarzon May 23 '21 at 14:30
  • 2
    @NeelKotkar - `touch.exe` is NOT a native windows command , nor is it a built in windows utility. the only ways to get such is to [A] install that util [the git-for-windows install does that] _OR_ [B] create a similar util as a function. – Lee_Dailey May 23 '21 at 15:17
  • Allow me to give you the standard advice to newcomers: If you [accept](https://meta.stackexchange.com/a/5235/248777) an answer, you will help future readers by showing them what solved your problem. To accept an answer, click the large ✓ symbol below the large number to the left of the answer (you'll get 2 reputation points). If you have at least 15 reputation points, you can also up-vote other helpful answers (optionally also the accepted one). If your problem isn't solved yet, provide feedback, or, if you found the solution yourself, [self-answer](http://stackoverflow.com/help/self-answer). – mklement0 May 24 '21 at 14:40
  • duplicates: [Creating new file through Windows Powershell](https://stackoverflow.com/q/45446285/995714), [Equivalent of Linux `touch` to create an empty file with PowerShell?](https://superuser.com/q/502374/241386), [Touch command not working in Terminal of VSC](https://stackoverflow.com/q/56417937/995714). Also related: [Create an empty file on the commandline in windows (like the linux touch command)](https://stackoverflow.com/q/30011267/995714), [How to create an empty file at the command line in Windows?](https://stackoverflow.com/q/1702762/995714) – phuclv May 24 '21 at 15:57

2 Answers2

2

As Lee Dailey points out, touch is neither a PowerShell command nor a standard utility (external program) on Windows; by contrast, on Unix-like platforms touch is a standard utility with dual purpose:

  • (a) When given existing file paths, the files' last-modified (.LastWriteTime) and last-accessed (.LastAccessTime) timestamps are set to the current point in time, by default.

  • (b) When given non-existing file paths, such files are created, by default.

There is no equivalent command in PowerShell (as of PowerShell 7.2), but you can use existing commands to implement (a) and (b) separately, and you can write a custom script or function that provides both (a) and (b) in a manner similar to the Unix touch utility:

# Update the last-modified and last-accessed timestamps of existing file app.js
$file = Get-Item -LiteralPath app.js
$file.LastWriteTime = $file.LastAccessTime = Get-Date
  • PowerShell implementation of (b), using the New-Item cmdlet:
# Create file app.js in the current dir.
# * If such a file already exists, an error will occur.
# * If you specify -Force, no error will occur, but the existing file will be 
# *truncated* (reset to an empty file).
$file = New-Item app.js

Note: When a file is created, it is a 0-byte file by default, but you may pass content via the -Value parameter.


Jeroen Mostert suggests the following technique to implement the conditional nature of the touch utility's file creation:

# * If app.js exists, leaves it untouched.
# * Otherwise, create it.
Add-Content app.js $null

Note:

  • To get touch-like behavior, (a) must still be implemented separately.

  • At least hypothetically the Add-Content approach can fail, namely if an existing target file is read-only - whereas manipulating such a file's timestamps may still work. (While you could suppress the error, doing so could make you miss true failures.)

The next section points to a custom function that wraps both (a) and (b), while also providing additional functionality that the Unix touch utility offers.


A custom function named Touch-File that implements most of the functionality of the Unix touch utility - which has several options to provide additional behaviors beyond the defaults described above - is available from this MIT-licensed Gist.
Assuming you have looked at the linked code to ensure that it is safe (which I can personally assure you of, but you should always check), you can directly download and define it the current session as follows, which also provides guidance on how to make it available in future sessions:

irm https://gist.github.com/mklement0/82ed8e73bb1d17c5ff7b57d958db2872/raw/Touch-File.ps1 | iex

Note: Touch is not an approved verb in PowerShell, but it was chosen nonetheless, because none of the approved verbs can adequately convey the core functionality of this command.

Sample call:

# * If app.js exists, update its last-write timestamp to now.
# * Otherwise, create it (as an empty file).
Touch-File app.js
mklement0
  • 245,023
  • 45
  • 419
  • 492
0

What about ?

Out-File -FilePath "app.js"
user10722100
  • 127
  • 8
  • 3
    Can you provide a little more context? Does this definitely work in lieu of `touch`, in the exact same way? – Clinton May 24 '21 at 00:14
  • 1
    Actually not. linux touch command creates the file if not existing or updates timestamp if existing. It may desverve to write a kind of touch-file function to mimic this as out-file as used here only create an empty file. – user10722100 May 24 '21 at 09:14
  • In _Windows PowerShell_ (up to v5.1, the Windows-only legacy edition that comes with Windows) the resulting file won't be _empty_: Because `Out-File file` there defaults to "Unicode" encoding (UTF-16LE), the file is created with a UTF-16LE _BOM_ (byte-order-mark), i.e. with the following _2 bytes_: `0xFF 0xFE`. Ditto with `$null > file`, which in effect is an alias for `Out-File`. Use `Set-Content file $null` or `New-Item file` for truly empty files. By contrast, the cross-platform _PowerShell (Core)_ edition (v6+) consistently uses BOM-less UTF-8, so `Out-File` / `$null >` work there too. – mklement0 May 24 '21 at 13:43