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

后端 未结 19 2040
一整个雨季
一整个雨季 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:22

    No command – neither typenor echo– is necessary to emulate Unix's/Mac OS X's 'touch' command in a Windows Powershell terminal. Simply use the following shorthand:

    $null > filename
    

    This will create an empty file named 'filename' at your current location. Use any filename extension that you might need, e.g. '.txt'.

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

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

    On windows Power Shell, you can use the following command:

    New-Item <filename.extension>
    

    or

    New-Item <filename.extension> -type file
    

    Note: New-Item can be replaced with its alias ni

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

    You can also use copy con [filename] in a 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 named yourfile.txt in the local directory.

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

    Assuming the file exists and you just need to update the timestamp.

    type test.c > test.c.bkp && type test.c.bkp > test.c && del test.c.bkp
    
    0 讨论(0)
  • 2020-11-28 01:25

    Yes you can use Node for Touch I just use that and its working all fine in windows Cmd or gitbash

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

    in cmd window type:

    type nul > your_file.txt
    

    This will create 0 bytes in your_file.txt file.

    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.

    Edited on 2019-04-01:

    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
    

    Edited on 2020-04-07

    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
    
    0 讨论(0)
提交回复
热议问题