Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Possible to create a batch file to delete the contents of a text file?

  • 07-06-2007 05:18PM
    #1
    Registered Users, Registered Users 2 Posts: 2,726 ✭✭✭


    Im using a program which writes ten lines of data to a text file called a.txt but the next time I use this program, it adds another 10 lines of data instead overwriting the original 10 lines. So im wondering can anyone tell me is their any way to create a batch file to erase all the data in a txt file without deleting the actual file itself. So I can run a batch file, and their will only be 10 lines at most in the text file after I run this program.

    if anyone can help it would be appreciated!


Comments

  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    I presume you're adding to the file using piping? Delete and recreate the file?


  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    Easy enough.
    REM -- First thing we need to do is delete the file. We're going to rename it instead of deleting it, just in case we need it.
    
    rename a.txt a.txt.bak
    
    REM -- Now we need to create a new blank file called "a.txt"
    
    cmd /c > a.txt
    
    REM -- Now we can run our program
    
    program.exe
    


  • Registered Users, Registered Users 2 Posts: 2,726 ✭✭✭kaisersose77


    thanks for the replies

    well this below seems to work fine

    copy /Y a2.txt a.txt

    with a2.txt being an empty file

    so a.txt is emptied when the batch file is run


  • Registered Users, Registered Users 2, Paid Member Posts: 21,529 ✭✭✭✭Alun


    thanks for the replies

    well this below seems to work fine

    copy /Y a2.txt a.txt

    with a2.txt being an empty file

    so a.txt is emptied when the batch file is run

    Easier way is to do a COPY /Y NUL a.txt

    No need for a separate file a2.txt then :)


Advertisement