Deleting a line in a .txt file
Moderator: DosItHelp
Deleting a line in a .txt file
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
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
Re: Deleting a line in a .txt file
Hope this helps!
Sample.txt
Now, put this BAT file on the same directory w/ Sample.txt
Output:
Now, just redirect the output to the another text file if you want.
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.
Re: Deleting a line in a .txt file
Meerkat wrote:Hope this helps!
Sample.txtCode: 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.txtCode: 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.
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
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!
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: Deleting a line in a .txt file
Actually, if you do what Meerkat suggested, it will work perfectly for what you want.
Re: Deleting a line in a .txt file
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?
Re: Deleting a line in a .txt file
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)
Hmm... and a sample demo code snippet. (Slightly edited compared to my earlier reply)
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:
After inputting 'test' (no quotes):
I do not know if there is simplier technique, but I think this helps.
Meerkat
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
Last edited by Meerkat on 27 Jul 2015 01:20, edited 1 time in total.
Re: Deleting a line in a .txt file
@Meerkat, it is slightly simpler this way:
Antonio
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
Re: Deleting a line in a .txt file
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
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!