Complicated Search and Replace using VBScript
Moderator: DosItHelp
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Complicated Search and Replace using VBScript
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.
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.
Last edited by pditty8811 on 30 Oct 2013 23:41, edited 2 times in total.
Re: Complicated Search and Replace using VBScript
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?
So if Origin=Japan will more than one Origin=Japan entry be found, or is each set of entries for a different language?
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Complicated Search and Replace using VBScript
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.
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.
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Complicated Search and Replace using VBScript
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.
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
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)
ChangeGameINI.vbs
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
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Complicated Search and Replace using VBScript
This does exactly what I told you to do. Fantastic. Does all Windows xp or greater OS have vbs script installed?
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Complicated Search and Replace using VBScript
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.
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...
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
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.
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Complicated Search and Replace using VBScript
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:
Last edited by pditty8811 on 30 Oct 2013 23:41, edited 1 time in total.
Re: Complicated Search and Replace using VBScript
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.
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Complicated Search and Replace using VBScript
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.
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Complicated Search and Replace using VBScript
The script is still not making edits to the file.
is this the issue then?
is this the issue then?
if not line = "Name="+line2 then
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Complicated Search and Replace using VBScript
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
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/
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/
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Complicated Search and Replace using VBScript
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.
Last edited by pditty8811 on 30 Oct 2013 23:42, edited 1 time in total.