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

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

    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

    • Works both with PowerShell and the Command Prompt.
    • Works with existing subdirectories.
    • Does not create a file if it already exists:
    New-Item : The file 'C:\Users\YourName\Desktop\a.txt' already exists.
    
    • For multiple files, creates only the files that do not exist.
      • Accepts a comma-separated list of filenames without spaces or enclosed in quotes if spaces are necessary:
    C:\Users\YourName\Desktop>touch d.txt,e.txt,f.txt
    C:\Users\YourName\Desktop>touch "g.txt, 'name with spaces.txt'"
    

提交回复
热议问题