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.
Asked
Active
Viewed 502 times
1 Answers
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
-
4I 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
-