How to delete content from a text file using windows batch script

前端 未结 5 1956
星月不相逢
星月不相逢 2020-12-28 13:03

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.

相关标签:
5条回答
  • 2020-12-28 13:18

    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.

    0 讨论(0)
  • 2020-12-28 13:19

    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.

    0 讨论(0)
  • 2020-12-28 13:20
    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.

    0 讨论(0)
  • 2020-12-28 13:26

    Command Prompt:

    1. break>c:\'file directory'\demo.txt

    PowerShell:

    1. Clear-Content c:\'file directory'\demo.txt
    0 讨论(0)
  • 2020-12-28 13:28
    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 >

    0 讨论(0)
提交回复
热议问题