I have a demo.txt file. I need to delete content in that file using a batch file. Please tell me the command to delete content for demo.txt file.
-
4Do you want to clear the content in the demo.txt or just want to delete specific text in the file? – Chelseawillrecover Oct 28 '13 at 11:36
-
Word of caution, make sure you specify the exact file else if you point to the wrong file, you may not be able to recover the content except you have a backup or create one before running script. – Chelseawillrecover Oct 28 '13 at 11:54
5 Answers
break>demo.txt
Try this.it will set an empty file on the place of demo.txt. As break
is internal command that does nothing it should be pretty fast.Also the break command can produce output only with /?
argument so this makes this method pretty robust.

- 55,367
- 18
- 148
- 187
Command Prompt:
break>c:\'file directory'\demo.txt
PowerShell:
Clear-Content c:\'file directory'\demo.txt

- 16,580
- 5
- 54
- 111

- 2,596
- 1
- 31
- 51
-
9Thanks for the added PowerShell command! CMD's `break>` doesn't work when the file is in use by a process. Since I couldn't end that process at the time, nor open it in Notepad++ to empty it manually because the file was too big, PowerShell's `Clear-Content` came in very handy and worked like a charm, so thanks! – Kevin Cruijssen Apr 30 '18 at 12:20
type nul > demo.txt
works and also works in JPSoft's TakeCommand TCC.EXE command shell (where "break" will output "BREAK is ON" rather than nothing as it does in Microsoft CMD.EXE)
The general idea is to find a command that outputs Nothing and redirect that to the file using >

- 93
- 1
- 2
-
1This worked, but seemed to take longer than the "Break>" command. Any idea why? – dgo Feb 15 '18 at 16:49
This seems most intuitive to me:
copy /y nul demo.txt
NOTE: Unfortunately, like the other methods provided here, this fails if demo.txt is currently in use by another process. In such a case it is sometimes possible to open the file in a text editor and delete all the contents, even though the file is in use. I don't know of a way to do this from the command line.

- 8,310
- 4
- 56
- 50
-
-
2It should display the output "1 file(s) copied", but the resulting file should be 0 length. Works for me, I just checked. Make sure you are not redirecting output. – yoyo Feb 16 '18 at 02:22
-
1you are right - I was redirecting it. I tried again using your method (correctly), and it worked. Very creative solution. Thanks – dgo Mar 11 '18 at 20:44
If the file is used by another application command prompt redirection may fail (as it requires more file access then necessary). In that case you can use powershell:
PS> Set-Content file.txt $null
Note: do not expect that it will allow access to exclusively opened files.

- 7,101
- 1
- 41
- 51