Deleting a line in a .txt file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Haxs4Life
Posts: 13
Joined: 02 Jan 2015 23:43
Contact:

Deleting a line in a .txt file

#1 Post by Haxs4Life » 25 Jul 2015 19:53

OK, so I'm working on an RPG batch file game, and you can create accounts to save your game progress and stats and such. On the menu there's a part where you can view all user accounts created, this is possible because every time someone creates an account the username is wrote to an accounts.txt file, then when they go to the screen to view the accounts it types the txt file onto the screen. On the same screen you can delete the accounts, each account is saved as a separate batch file, so its easy to remove the account specified. The problem is that the account name stays in the text file. So my question is, is there a way to remove a line from the text file containing the name specified from a variable set by the player typing the account name that needs to be removed?
If you need more info just ask please, I will give you any info needed. Thank you for reading this and helping me out! It's very appreciated :)

Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Re: Deleting a line in a .txt file

#2 Post by Meerkat » 26 Jul 2015 00:38

Hope this helps! :D :)

Sample.txt

Code: Select all

1 2
2 4
3 7
4 9  00
5 9
6
79 9
8 9 111
9
099 1


Now, put this BAT file on the same directory w/ Sample.txt

Code: Select all

@echo off
findstr /v /c:"4 9 00" /c:"1 2" Sample.txt
::/C is really strict in spaces.
::Also there is echo. below because findstr doesnt have CRLF thingy in last line...
echo.
pause


Output:

Code: Select all

2 4
3 7
4 9  00
5 9
6
79 9
8 9 111
9
099 1
Press any key to continue . . .


Now, just redirect the output to the another text file if you want. :wink:

Haxs4Life
Posts: 13
Joined: 02 Jan 2015 23:43
Contact:

Re: Deleting a line in a .txt file

#3 Post by Haxs4Life » 26 Jul 2015 18:23

Meerkat wrote:Hope this helps! :D :)

Sample.txt

Code: Select all

1 2
2 4
3 7
4 9  00
5 9
6
79 9
8 9 111
9
099 1


Now, put this BAT file on the same directory w/ Sample.txt

Code: Select all

@echo off
findstr /v /c:"4 9 00" /c:"1 2" Sample.txt
::/C is really strict in spaces.
::Also there is echo. below because findstr doesnt have CRLF thingy in last line...
echo.
pause


Output:

Code: Select all

2 4
3 7
4 9  00
5 9
6
79 9
8 9 111
9
099 1
Press any key to continue . . .


Now, just redirect the output to the another text file if you want. :wink:


Not sure of this will help for what I'm asking..I will explain/post an example below.

So on my game menu theres a register section, when someone signs up they create a username which is printed/"echoed" into an accounts.txt file that looks like the following.

Zixefer
Haxs4life
test
admin
username
test2
accountName

The text file is printed on the screen when you select the "view accounts" option, with the TYPE command.
Now when someone deletes an account, their account name stay in this text file. I need a way to remove specific lines when someone removes their account.

I'm not familiar with the Findstr command, so I don't know but maybe something like the following could work?

Code: Select all

findstr /c:"%Account%" Sample.txt
Then some sort of way to turn the location into a variable and use the DEL command to remove it...I honestly don't know and thinking about that now seems stupid XD But hopefully I at least made a point and better explained what I want.

Also, if there is no way of doing this in batch, then anyone with powershell or VBS knowledge could you post a script/code that I could use to do this if possible? Thanks, Haxs4life!

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Deleting a line in a .txt file

#4 Post by ShadowThief » 26 Jul 2015 19:40

Actually, if you do what Meerkat suggested, it will work perfectly for what you want.

Haxs4Life
Posts: 13
Joined: 02 Jan 2015 23:43
Contact:

Re: Deleting a line in a .txt file

#5 Post by Haxs4Life » 26 Jul 2015 21:41

ShadowThief wrote:Actually, if you do what Meerkat suggested, it will work perfectly for what you want.

i dont understand it :/ could u explain a little bit in more detail please? Sorry if I'm asking too much, but im just not quite understanding...the thing that he showed only listed the text file without specific string showing up...instead of deleting the lines from the .txt...Please help me understand here?

Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Re: Deleting a line in a .txt file

#6 Post by Meerkat » 26 Jul 2015 23:12

:oops: Sorry for a vague suggestion.

Actually, there is no direct method in pure Batch to directly delete a line in a file. However, Batch provides an alternative that could be done.

For instance (Account.txt)

Code: Select all

Zixefer
Haxs4life
test
admin
username
test2
accountName


Hmm... and a sample demo code snippet. (Slightly edited compared to my earlier reply)

Code: Select all

@echo off
set "file_accounts=Account.txt"

:sample_loop
cls
echo.Accounts:
echo.
type "%file_accounts%"
echo.
echo.
set /p delete_usr=Enter the account to delete:

   ::The main processor
for /f "tokens=*" %%X in (
'findstr /v /e /b /c:"%delete_usr%" "%file_accounts%" ^&del "%file_accounts%"'
) do (
   echo %%X>>%file_accounts%
)
goto :sample_loop


Explanation for the main processor:
For the 'complicated' Findstr part:
The /v switch in findstr actually displays the lines that does NOT contain the string you provided in /c switch. The /c switch with quotes makes the search string a 'literal' one. The /e and /b switch I added scans for the "end" AND "beginning" of line, respectively. This will make the "strict" search for the whole line.

You can also add an /i switch if you want the search to be case-insensitive.

Then, deletes the old file. (Sigh)

Sample output:

Code: Select all

Accounts:

Zixefer
Haxs4life
test
admin
username
test2
accountName

Enter the account to delete:


After inputting 'test' (no quotes):

Code: Select all

Accounts:

Zixefer
Haxs4life
admin
username
test2
accountName


Enter the account to delete:


I do not know if there is simplier technique, but I think this helps.

Meerkat :D
Last edited by Meerkat on 27 Jul 2015 01:20, edited 1 time in total.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Deleting a line in a .txt file

#7 Post by Aacini » 26 Jul 2015 23:53

@Meerkat, it is slightly simpler this way:

Code: Select all

   ::The main processor
findstr /v /e /b /c:"%delete_usr%" "%file_accounts%" > tempfile.tmp
move /Y tempfile.tmp "%file_accounts%"
goto :sample_loop

Antonio

Haxs4Life
Posts: 13
Joined: 02 Jan 2015 23:43
Contact:

Re: Deleting a line in a .txt file

#8 Post by Haxs4Life » 27 Jul 2015 01:35

Meerkat wrote:Actually, there is no direct method in pure Batch to directly delete a line in a file. However, Batch provides an alternative that could be done.
[snip]
Meerkat :D

Thank you soooo much @Meerkat!! I didn't think of using it that way and now it seems so simple, I feel so stupid now!! XD thanks so much for explaining it to me man, and really sorry if I frustrated u for not understanding..I'm just not as advanced in batch so I have allot to learn.

Aacini wrote:@Meerkat, it is slightly simpler this way:
[snip]
Antonio

Also thank you very much @Aacini for the more compact/revised version of Meerkat's previous code. I appreciate your help very well indeed!

Post Reply