0

At the good old Windows command prompt, I want to ensure a file exists and create it if it doesn't. I don't want to download any additional utilities (like touch) to accomplish this.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
John K
  • 28,441
  • 31
  • 139
  • 229
  • 2
    if you're running the command on Windows cmd then [it's **not** DOS](https://superuser.com/q/451432/241386). They have different capabilities and syntaxes – phuclv Nov 03 '21 at 07:50

1 Answers1

1

Here is one-liner solution:

As an example, I want to ensure a file named App_Offline.htm exists in my website root, but without overwriting that file if it already exists. This is the native DOS command I successfully used with append:

(echo empty > nul) >> App_Offline.htm

Note the word empty can be any text you want because it is simply a dummy placeholder piped through nul and is lost; therefore no value is effectively appended to an existing file, and if a file doesn't already exist then it is created empty.

This solution did not update the Modified time stamp of the existing file.

John K
  • 28,441
  • 31
  • 139
  • 229
  • 4
    I am sure this cannot work in MS-DOS since parenthesised command blocks are not supported there, also the file name is too long for MS-DOS. Do you actually mean the Window command Prompt (`cmd.exe`)? Anyway, what about `set _=>>file.ext`? – aschipfl Oct 31 '21 at 18:50
  • Thanks @aschipfl, you're right - I corrected the question to tighten it up, was throwing around the terms too loosely. – John K Nov 03 '21 at 01:57
  • and DOS doesn't support code blocks `()` so `(echo ...)` won't work – phuclv Nov 03 '21 at 07:51