Page 1 of 2

Complicated Search and Replace using VBScript

Posted: 29 Oct 2013 21:13
by pditty8811
foxidrive created a vbscript for me awhile back that is really fast. Much faster than the batch script equivalents.
I do not know much about vbscript yet, so I need some help.


variable names I will be using, with example values.
set !varNation!=German
set /a !varDate!=19401010

All text that is to the left of an "=" will remain static, while everything to the right of it changes per entry. Entries are separated by blank lines, so blank lines need to be kept in the output file. The entries represent units on a global map of a simulation game.

The variable we are finding is actually in the "Origin=" field of every entry, the two entries above are both German. I would like a variable we call !varNation! to be the nationality we are searching for. So that would be Origin=!varNation!.

If the entry is of Origin=!varNation! I would like 10 lines below that field, the "GameExitDate=" field of that entry to be changed to another date. A variable I set. "GameExitDate=!varDate!"
So for the sake of it lets call the date variable I want to set all !varNation! entries to is called !varDate!.

And thats really it. Spit everything out with only the changes to the GameExitDate= field, keeping all blank lines and everything else the same.

I'll run over this one more time. Search for "Origin=!varNation!" entries. If found, move 10 lines down to the "GameExitDate=" field of that entry and change it to "GameExitDate=!varDate!". then move on to the next "Origin=!varNation!".

VBscript would be preferred, as for some reason it is faster than batch. Becasue the file will be 6mb, I prefer vbsscript. Batch scripts will take way too long for this.

Thank you.

Re: Complicated Search and Replace using VBScript

Posted: 30 Oct 2013 02:42
by foxidrive
Will more than one set of entries be changed at the one time?

So if Origin=Japan will more than one Origin=Japan entry be found, or is each set of entries for a different language?

Re: Complicated Search and Replace using VBScript

Posted: 30 Oct 2013 03:14
by pditty8811
Yes, more than one entry will be changed at a time. All of a specific origin will be changed.

I will be incrementing or de-incrementing the date, for specific origins. But I will take care of the incrementing deincrementing part as I know how to do it. I just need the base vbs written first.

The origin field actually represents the nationality of that specific unit in the game.

Thanks.

Re: Complicated Search and Replace using VBScript

Posted: 30 Oct 2013 03:45
by pditty8811
One thing I did forget, would it be possible to return the original value that is at "GameExitDate=", before it is changed to !varDate! ?

That way I can test the value that is there, to determine what I should set !varDate! to for that particular entry.
I know in the example I had varDate set to a specific value, but it wont be. It will be determined by a equation I haven't figured out yet, and perhaps will take months of testing for me to get where I want it.

The exact equation I will use to determine the varDate value hasn't been decided upon yet, so I will have to do some testing in-game to figure out what that equation will be. But in the mean time, could I have GameExitDate= original value be returned in some section of the code for me to adjust later?

Thanks.

Re: Complicated Search and Replace using VBScript

Posted: 30 Oct 2013 05:10
by foxidrive
Here's example script. The batch file sets the variables and calls the VBS script with them, and the VBS script processes the input file.

It takes around 3 seconds to process a 6 MB file here.
Errorlevel 1 is set if the file format is not as described (10 lines after the processed Origin= lines, a GameExitDate= line must be found)

Code: Select all

@echo off
setlocal enabledelayedexpansion
set "varNation=Chinese"
set "varDate=11112233"
echo %time%
cscript /nologo "ChangeGameINI.vbs" !varNation! !varDate!
echo %time%
if errorlevel 1 echo Error:&pause
pause


ChangeGameINI.vbs

Code: Select all

  Const ForReading = 1, ForWriting = 2

  infile = "infile.txt"   '<= Set input file here
  outfile = "out.txt"   '<= Set output file here

  Set fso = CreateObject("Scripting.FileSystemObject")
  Set f1 = fso.OpenTextFile(infile, ForReading)
  Set f2 = fso.OpenTextFile(outfile, ForWriting, True)
  Set objArgs = WScript.Arguments

  Do While not F1.AtEndOfStream
  line=F1.readline
  if line = "Origin="+objArgs(0) then
     f2.writeline line
        for count = 1 to 9: line=F1.readline: f2.writeline line: next
     line=F1.readline
        if not left(line,13) = "GameExitDate=" then
           WScript.Echo("The file format is not correct: an instance of 'GameExitDate=' was not found")
           f2.close
           Set f2 = fso.GetFile(outfile)
           f2.Delete
           WScript.Quit (1)
         else
           f2.writeline "GameExitDate="+objArgs(1)
        end if
  else
     f2.writeline line
  end if
  Loop
  f1.Close
  f2.Close

Re: Complicated Search and Replace using VBScript

Posted: 30 Oct 2013 12:31
by pditty8811
This does exactly what I told you to do. Fantastic. Does all Windows xp or greater OS have vbs script installed?

Re: Complicated Search and Replace using VBScript

Posted: 30 Oct 2013 12:57
by pditty8811
Now, if I was to input a value to increment by months, and not a date for varDate.

How would that look?

I found this vbscript that handles incrementing the date via DateAdd function, but with the format yyyymmdd, which is the format I am working with.

Code: Select all

dim sStrDate, day, month, year

sStrDate = "20120410"

yearPart = Mid(sStrDate, 1, 4)
monthPart= Mid(sStrDate, 5, 2)
dayPart = Mid(sStrDate, 7, 2)

sStrDate = DateAdd("d", 80, monthPart & "/" & dayPart & "/" & yearPart)


if (len(DatePart("m", sStrDate)) < 2) then
monthPart = "0" & CStr(DatePart("m", sStrDate))
else

monthPart = CStr(DatePart("m", sStrDate))
end if


if (len(DatePart("d", sStrDate)) < 2) then
dayPart = "0" & CStr(DatePart("d", sStrDate))
else
dayPart = CStr(DatePart("d", sStrDate))
end if

yearPart = CStr(DatePart("yyyy", sStrDate))


document.write(yearPart & monthPart & dayPart )



So, if varDate was actually an integer of how many months I want to increment or de-increment (negative value) how would I do that?

I can't increment the date in the batch file because the batch file doesn't receive back the value of what is found at GameExitDate=.

The GameExitDate= value that is found for every entry will be unique for every entry, but the increment will be universal. Add 2 months to all French units, etc...

Re: Complicated Search and Replace using VBScript

Posted: 30 Oct 2013 13:15
by foxidrive
pditty8811 wrote:Does all Windows xp or greater OS have vbs script installed?


yep. Work/Corporate machines may have it locked down, but that's not an issue for a gamer I expect.

pditty8811 wrote:Now, if I was to input a value to increment by months, and not a date for varDate.
How would that look?


Please state ALL the requirements in the beginning and don't add more bits to every script. It doubles the work of writing and testing a script.

Re: Complicated Search and Replace using VBScript

Posted: 30 Oct 2013 14:25
by pditty8811
foxidrive wrote:
pditty8811 wrote:Does all Windows xp or greater OS have vbs script installed?


yep. Work/Corporate machines may have it locked down, but that's not an issue for a gamer I expect.

pditty8811 wrote:Now, if I was to input a value to increment by months, and not a date for varDate.
How would that look?


Please state ALL the requirements in the beginning and don't add more bits to every script. It doubles the work of writing and testing a script.


I'm sorry. Its all coming to me know. I thought I could do most of the work, and work around a base vbs script. I didn't realize it would be more work for you.

On top of the incrementing or de-incrementing the date, the only other requirement I could think of would be to test the "Name=" field for a value that matches values in a textfile called 1.txt. Each value in 1.txt will be on its own line.
If "Name=matches any value from 1.txt" then do not perform the date increment.
Note that Name= field is 3 lines above the Origin= field.
I tried rewriting the script you provided me to do this but for some reason I have en error somewhere. It runs but it doesn't do anything except spit out the same file.

Here is the script that I edited:

Re: Complicated Search and Replace using VBScript

Posted: 30 Oct 2013 14:35
by foxidrive
pditty8811 wrote:On top of the incrementing or de-incrementing the date, the only other requirement I could think of would be to test the "Name=" field for a value that matches values in a textfile called 1.txt. Each value in 1.txt will be on its own line.
If "Name=matches any value from 1.txt" then do not perform the date increment.

I tried rewriting the script you provided me to do this but for some reason I have en error somewhere. It runs but it doesn't do anything except spit out the same file.

Here is the script that I edited:

Code: Select all

  Do While not F1.AtEndOfStream
  line=F1.readline
   if line = "Name=" then



The issue there is that the entry Name= will always have text after it, so will be equal to Name=U-459 Resupply Boat etc.
You would need to use

Code: Select all

     if left(line,5) = "Name=" then


and it would be best to read the name file into an array and then test the array elements, rather than reading the same file for every record, which would slow it down immensely.

Re: Complicated Search and Replace using VBScript

Posted: 30 Oct 2013 14:45
by pditty8811
foxidrive wrote:and it would be best to read the name file into an array and then test the array elements, rather than reading the same file for every record, which would slow it down immensely.


This is where my understanding of vbs comes to an end.

Re: Complicated Search and Replace using VBScript

Posted: 30 Oct 2013 14:47
by pditty8811
The script is still not making edits to the file.

is this the issue then?

if not line = "Name="+line2 then

Re: Complicated Search and Replace using VBScript

Posted: 30 Oct 2013 15:15
by pditty8811
Its not getting within this statement here for some reason:

Code: Select all

if line = "Origin="+objArgs(0) then

Re: Complicated Search and Replace using VBScript

Posted: 30 Oct 2013 15:37
by foxidrive
TBH it's gone a little beyond what is on topic here, where you want everything in a VBS script.

There are Usenet VBS newsgroups, and maybe StackOverflow would be another option at stackoverflow.com Or http://forums.techguy.org or http://www.computerhope.com/forum/

Re: Complicated Search and Replace using VBScript

Posted: 30 Oct 2013 15:59
by pditty8811
foxidrive wrote:TBH it's gone a little beyond what is on topic here, where you want everything in a VBS script.

There are Usenet VBS newsgroups, and maybe StackOverflow would be another option at stackoverflow.com Or http://forums.techguy.org or http://www.computerhope.com/forum/


Understood. I guess I'm on my own now.

I did fix the script for Name=. Now I just need date adjustment implemented somehow.