Sign in to follow this  
Followers 0
wazer

Edit file with multiple lines that has same values <?

11 posts in this topic

#1 ·  Posted (edited)

Hello im trying to read and edit the path of a txt file. My problem is that there's multiple lines with same value and i only want to change 6 lines overall in each respective category.

I want to change "QuickList" "0" to "QuickList" "1" in following cats:

"CustomGames"

"SpectateGames"

"FavoriteGames"

"LanGames"

"FriendsGames"

"HistoryGames"

Note the line numbers cannot be trusted as of some of the lines may change at some point when new information is added, and therefore QuickList may go further down the in lines in each category.

"Filters"
{
"gamelist"  "favorites"
"Filters"
{
  "CustomGames"
  {
   "ping"  "0"
   "location"  "0"
   "NoFull"  "0"
   "NoEmpty"  "0"
   "NoPassword"  "0"
   "Secure"  "0"
   "QuickList"  "0"
   "appid"  "0x0000000000000000"
  }
  "SpectateGames"
  {
   "ping"  "0"
   "location"  "0"
   "NoFull"  "0"
   "NoEmpty"  "0"
   "NoPassword"  "0"
   "Secure"  "0"
   "QuickList"  "0"
   "gametype"  "notags"
   "appid"  "0x0000000000000000"
  }
  "FavoriteGames"
  {
   "ping"  "0"
   "NoFull"  "0"
   "NoEmpty"  "0"
   "NoPassword"  "0"
   "Secure"  "0"
   "QuickList"  "0"
   "gametype"  "notags"
   "appid"  "0x0000000000000000"
  }
  "LanGames"
  {
   "ping"  "0"
   "NoFull"  "0"
   "NoEmpty"  "0"
   "NoPassword"  "0"
   "Secure"  "0"
   "QuickList"  "0"
   "gametype"  "notags"
   "appid"  "0x0000000000000000"
  }
  "FriendsGames"
  {
   "ping"  "0"
   "NoFull"  "0"
   "NoEmpty"  "0"
   "NoPassword"  "0"
   "Secure"  "0"
   "QuickList"  "0"
   "gametype"  "notags"
   "appid"  "0x0000000000000000"
  }
  "HistoryGames"
  {
   "ping"  "0"
   "NoFull"  "0"
   "NoEmpty"  "0"
   "NoPassword"  "0"
   "Secure"  "0"
   "QuickList"  "0"
   "gametype"  "notags"
   "appid"  "0x0000000000000000"
  }
}
}

This have driving me insane for last 6 hours now.

I have searched and searched every little bit on the forums. Everytime i thought i had found a good answer i tried it but without success!

My old solution was this was to implent the serverbrowser.vdf file into the .exe. Extract it and filecopy it to the correct dir found via reg.

But after i found out that the file was/could be changed with extra options by the user, i have to remove this when i got a new solution with editing the file instead.

;Reads steamapps\ACCOUNT\half-life\ and replacing half-life with <-> source sdk base 2007\platform\config
$source_sdk_folder = StringReplace($modfolder, "half-life", "\source sdk base 2007\platform\")
 
 
 
DirCreate($source_sdk_folder & "\config\")
FileCopy(@TempDir & "\serverbrowser.vdf", $source_sdk_folder & "\config\serverbrowser.vdf",1)
 
If DirCreate($var) Then
    ; Extract the file to the folder
    If FileInstall('serverbrowser.vdf', $var & '\') Then
EndIf
EndIf

This may look abit clumsy i just copy and pasted different lines from my "not cleaned up" script yet :graduated:

Hope to see some skilled fellas here ;)

Edited by wazer

Share this post


Link to post
Share on other sites



Hope to see some skilled fellas here :graduated:

I like your use of social engineering there.

Anyway, I noticed that each category basically has the same set of subcategory values (8), why not look for the name of a category, retrieve the line number of where it was found and determine the line number of the value to edit by looking for them withing the curly brackets?


Things that I've done..

Icon Resource Editor: icon resource editor 

AutoIt Piano: a piano

AutoIt Unlocker: unlocks files when you want to delete them

Colorful tooltips: a wrapper for the tool tips UDF

Rouge GoogleBot: a full screen animation

ASciTE text editor: a text editor written in autoit

Warning: Posts by this user are subject to change or may disappear without notice.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I like your use of social engineering there.

Anyway, I noticed that each category basically has the same set of subcategory values (8), why not look for the name of a category, retrieve the line number of where it was found and determine the line number of the value to edit by looking for them withing the curly brackets?

If you have an idea how it could be done the corret way because im usure how to do that in the way you say.

Edited by wazer

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

As i wrote with the numbers cannot be trusted, as that some people "perhaps" only use favorites and then perhaps adds loads of stuff to text file, and then suddenly it could be at line 20 instead of 8 if you know what i mean :graduated:.

So what you're saying is, in the example file given, we have this category~

"HistoryGames"

And inside that category are a few other categories and values which in your example are all exclusively and specifically eight values withing the main catagory~

"ping"  "0"--------------------------------1
   "NoFull"  "0"-----------------------------2
   "NoEmpty"  "0"--------------------------3
   "NoPassword"  "0"----------------------4
   "Secure"  "0"-----------------------------5
   "QuickList"  "0"---------------------------6
   "gametype"  "notags"-------------------7
   "appid"  "0x0000000000000000"-----8

and you're saying that other values are added at times by a user~

"ping"  "0"--------------------------------1
   "NoFull"  "0"-----------------------------2
   "NoEmpty"  "0"--------------------------3
   "NoPassword"  "0"----------------------4
   "Secure"  "0"-----------------------------5
   "QuickList"  "0"---------------------------6
   "gametype"  "notags"-------------------7
   "appid"  "0x0000000000000000"-----8
   "[INSERT VALUE NAME HERE]" "[VALUE]"---9

So we would then now end up with 9 sub categories or value names (whatever you prefer to call them).

Then instead of looking for the line number, why not perform a search for the value name after locating "HistoryGames" and searching for the value name within the "{" and not exceeding "}"?

I have a feeling you're going to now say that those aren't the values that are added and that the most likely things that are added are the main categories, if so, then I see no reason the first method I describe would not work.

Edited by THAT1ANONYMOUSEDUDE

Things that I've done..

Icon Resource Editor: icon resource editor 

AutoIt Piano: a piano

AutoIt Unlocker: unlocks files when you want to delete them

Colorful tooltips: a wrapper for the tool tips UDF

Rouge GoogleBot: a full screen animation

ASciTE text editor: a text editor written in autoit

Warning: Posts by this user are subject to change or may disappear without notice.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

What i want is this but allowing to change both Both quicklist and "x" in the same line at the same time with one simply little script..

I have a feeling that this can do it but im unsure how to set the ' " " for quicklist and the "x" value in the same line, so basicly it searches for "quicklist" "0" and replaces it with "quicklist" "1" from the line

$wzFile = "c:\file.txt"
 
$wzText = FileRead($wzFile,FileGetSize($wzFile))
$wzText = StringReplace($wzText, "QuickList  REPLACE-ME", "Quicklist     WITH-ME")
FileDelete($wzFile)
FileWrite($wzFile,$wzText)

what if i want to add "0" in the same line for REPLACE me, and "1" in WITH-ME

From

"QuickList"     "0"

To

"QuickList"     "1"

Here's an example not giving any errors but does not work..

$wzFile = "c:\file.txt"
 
$wzText = FileRead($wzFile,FileGetSize($wzFile))
$wzText = StringReplace($wzText,' "QuickList"    "0" ', ' "QuickList"    "1" ')
FileDelete($wzFile)
FileWrite($wzFile,$wzText)
Edited by wazer

Share this post


Link to post
Share on other sites

I'd expect someone who has been on a scripting forum for as long as yourself, to put a little more effort in than you have.

First, the ideal way to meet your challenge would be the employment of StringRegExpReplace() function.

There are many other ways to accomplish your goal with a few other string functions and a bit of logic.

For example StringInStr will return the position in your string where the test string is, if it is present, you then

employ some basic logic and perhaps StringMid() or StringRight() functions etc....

You are going to get a lot more help if you show your efforts.

Good luck.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I'd expect someone who has been on a scripting forum for as long as yourself, to put a little more effort in than you have.

First, the ideal way to meet your challenge would be the employment of StringRegExpReplace() function.

There are many other ways to accomplish your goal with a few other string functions and a bit of logic.

For example StringInStr will return the position in your string where the test string is, if it is present, you then

employ some basic logic and perhaps StringMid() or StringRight() functions etc....

You are going to get a lot more help if you show your efforts.

Good luck.

I was out for an accident some time ago, and im having really big problems remembering things, im doing this because the doctors said i needed to find something that i like and really let my brain work and it might recover the function's so i can start to remember normal and small things again.

So i joined up a project and now here i am trying to figure it out :graduated:

Edited by wazer

Share this post


Link to post
Share on other sites

If you have mental problems then I would not recommend StringRegExp, it will send you catatonic.

I will try to help you help yourself.

Choose a categary in your file and use StringInStr() function to find its position in the string after you Fileread.

Write that code and post it up.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Here's some code to think about:

#include <file.au3>
 
$loop = _FileCountLines("test.txt") ; Reads how many lines in the text file
 
For $i = 1 to $loop Step 1
    $line = FileReadLine("test.txt", $i)
    If StringInStr('"QuickList""0"', StringStripWS ($line, 8)) Then  ; Strip the whitespace so that the spacing between Quicklist and 0 do not have to be exact
        _FileWriteToLine("test.txt", $i, '   "QuickList"    "1" ', 1)
    EndIf
Next

#include <ByteMe.au3>

Share this post


Link to post
Share on other sites

Sorry i forgot to get in here :graduated:

Im curius about JohnOne why you didnt know about tab spaces in autoit?, my script i posted in #0 works very well just not with whitespaces that have been tabbed, autoit dont understand to read it.

I think i do alot more effort than many other people in here asking for help, and to prove that look my new code at the bottom.

----------

Here's some code to think about:

#include <file.au3>
 
$loop = _FileCountLines("test.txt") ; Reads how many lines in the text file
 
For $i = 1 to $loop Step 1
    $line = FileReadLine("test.txt", $i)
    If StringInStr('"QuickList""0"', StringStripWS ($line, 8)) Then  ; Strip the whitespace so that the spacing between Quicklist and 0 do not have to be exact
        _FileWriteToLine("test.txt", $i, '   "QuickList"    "1" ', 1)
    EndIf
Next

Thanks for the help i appriciate that ;), but was not what i was looking for as it checks line 8 only??

-------------------------

If FileExists($serverbrowservdf) Then

$szFile = $serverbrowservdf
$szText = FileRead($szFile,FileGetSize($szFile))
$pattern = '([\t ]*)"QuickList"([\t ]*)"1"'; "QuickList" & "1" is the value searching for
$szText = StringRegExpReplace($szText, $pattern, '\1"QuickList"\2"0"');"QuickList" & "0" is the value its replacing
if @error = 0 Then
FileDelete($szFile)
FileWrite($szFile,$szText)
endif

Else
    $var = @TempDir
    DirCreate($var)
    DirCreate($source_sdk_folder & "\config\")
    FileInstall('serverbrowser.vdf', $var & '\')
    FileCopy(@TempDir & "\serverbrowser.vdf", $source_sdk_folder & "\config\serverbrowser.vdf",1)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0