Windows equivalent of 'touch' (i.e. the node.js way to create an index.html)

后端 未结 19 2039
一整个雨季
一整个雨季 2020-11-28 00:47

On a windows machine I get this error

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

相关标签:
19条回答
  • 2020-11-28 01:28

    The answer is wrong, it only works when the file does not exist. If the file exists, using the first does nothing, the second adds a line at the end of the file.

    The correct answer is:

    copy /b filename.ext +,,
    

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

    0 讨论(0)
  • 2020-11-28 01:29

    Use the following command on the your command line:

    fsutil file createnew filename  requiredSize
    

    The parameters info as followed:

    fsutil - File system utility ( the executable you are running )

    file - triggers a file action

    createnew - the action to perform (create a new file)

    filename - would be literally the name of the file

    requiredSize - would allocate a file size in bytes in the created file

    0 讨论(0)
  • 2020-11-28 01:32

    From the Terminal of Visual Code Studio on Windows 10, this is what worked for me to create a new file:

    type > hello.js
    echo > orange.js
    ni > peach.js
    
    0 讨论(0)
  • 2020-11-28 01:32

    If you have Cygwin installed in your PC, you can simply use the supplied executable for touch (also via windows command prompt):

    C:\cygwin64\bin\touch.exe <file_path>
    
    0 讨论(0)
  • 2020-11-28 01:35

    As Raghuveer points out in his/her answer, ni is the PowerShell alias for New-Item, so you can create files from a PowerShell prompt using ni instead of touch.

    If you prefer to type touch instead of ni, you can set a touch alias to the PowerShell New-Item cmdlet.

    Creating a touch command in Windows PowerShell:

    From a PowerShell prompt, define the new alias.

    Set-Alias -Name touch -Value New-Item
    

    Now the touch command works almost the same as you are expecting. The only difference is that you'll need to separate your list of files with commas.

    touch index.html, app.js, style.css
    

    Note that this only sets the alias for PowerShell. If PowerShell isn't your thing, you can set up WSL or use bash for Windows.

    Unfortunately the alias will be forgotten as soon as you end your PowerShell session. To make the alias permanent, you have to add it to your PowerShell user profile.

    From a PowerShell prompt:

    notepad $profile
    

    Add your alias definition to your profile and save.

    0 讨论(0)
  • 2020-11-28 01:36

    as mentioned

    echo >> index.html
    

    it can be any file, with any extension then do

    notepad index.html
    

    this will open your file in the notepad editor

    0 讨论(0)
提交回复
热议问题