compare two variables in a text file - vbscript

I have a text file with multiple lines every two lines has the foillowing information:
Hostname, IP
IP, Hostname.Domain
What I would like to do is compare line one hostname and line two hostname. If they match I want to copy the hostname to a new text document.
I am new to scripting and would appreciate any help I can get.

if you use .split("\n"), it will give you an array that contain lines
contentLines = content.split("\n");
http://jsfiddle.net/sN6XT/
Than just loop through the array for odd (or even) index, use regex to find the line you want
Hope it helps :D

Something like this should work:
Set fso = CreateObject("Scripting.FileSystemObject")
Set infile = fso.OpenTextFile("C:\path\to\input.txt")
Set outfile = fso.OpenTextFile("C:\path\to\output.txt")
Do Until infile.AtEndOfStream
hostname1 = Trim(Split(infile.ReadLine, ",")(0))
fqdn = Split(infile.ReadLine, ",")(1)
hostname2 = Trim(Split(fqdn, ".")(0))
If LCase(hostname1) = LCase(hostname2) Then outfile.WriteLine hostname1
Loop
infile.Close
outfile.Close

Related

vbscript: RegExp Replace turn capture group into variable

regex in VBscript has 3 methods, Test, Extract and Replace, but I can only seem to turn capture groups from Extract into variable.
However what I want to do is use capturing groups from 'Replace' as a variable. I can get a Regex.Replace working with no problems using $1 $2 etc for capturing groups, however I want multiply one of the capture groups.
In an xml file, I want to extract a value, times it by 15, and insert it back in. In this example the tag.
e.q.
strText = "
<rte>
<name>gpx.studio church 2 reduced.gpx</name>
<rtept lat='-33.482652' lon='150.159134'>
<ele>938.4</ele>
<desc>076</desc>
</rtept>
<rtept lat='-33.4825698175265' lon='150.159515440464'>
<ele>942.3</ele>
<desc>162</desc>
</rtept>
<rtept lat='-33.4828785376496' lon='150.159633457661'>
<ele>943.4</ele>
<desc>098</desc>
</rtept>
</rte>
</gpx>"
Dim oRegExp
Set oRegExp = New RegExp
oRegExp.Global=True
oRegExp.Multiline = True
oRegExp.Pattern = strPattern
strPattern = "(<rtept(?:(?:.|\n|\r)*?))<desc>(.*?)<\/desc>((?:(?:.|\n|\r)*?)<\/rtept>)"
strReplace = "$1<desc>$2<\/desc>$3"
' so on this line above, I want to turn the $2 into an integer and multiply it by 15 before putting back into replace.
' I have not done it here because I know it doesnt work as "$2"x1000
strNewText = oRegExp.Replace(strText, strReplace)
I want to turn the $2 into an integer and multiply it by 15 before putting back into replace.
I have tried to get the capture groups as SubMatches(1) which work with Regex.Extract method but it doesnt seem to work in Regex.Replace method, unless I am missing something....
help appreciated

VBScript code to do math based on folder contents

I could use some help in troubleshooting my code, which should run a report based on files in a folder, which meet certain criteria, read specific lines from those files and sum all the read values. And do some math later on.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
relativePath = wshShell.CurrentDirectory
Set ReportPath = objFSO.GetFolder(relativePath & "\KPI_STATS\")
count = 0
Now I'd like to check each file's filename in ReportPath if it matches current month...
For Each file In ReportPath.files
If instr(LCase(file.name), LCase(Right("0" & DatePart("m",Now),2))) > 0 Then
for i=0 to n
SIDENOTE: Now I don't understand why if I run msgbox(file) for testing only it gives me whole path to the folder with filename instead of filename only.
(let's continue)...and when it does, it should read only 14th line of text (last line and there are only numbers in it) of each file and store it in variable. I use Split for reading certain lines from files and arrays to hold read values but I can use as well something like sum = sum + sum(n-1).
a = Split(objFSO.OpenTextFile(file, ForReading).ReadAll, vbCrLf)
For j = 14
If UBound(a) = j Then
And now I'd like to write those values to separate Arrays
ar(i) = Array(a(j))
End If
Next
next
count = count + 1
End If
Next
I think I will manage to add and divide all the values as soon as I have all arrays in place but there i a problem. It doesn't work :'(
Thanks a lot.

Reading line by line from one file and Writing to another file using VBScript

I am trying to split a tab delemeted file into pieces with similar header. I have my logic in place. However, I am trying to read input file line by line and writing it to another file. When I open the outptut file it doesn't contain any data. Here is my code.
Can some one help me whats going wrong here?
Note: The below code doesn't contain actual logic of splitting the file
Wscript.Echo "Begin"
InputFile = "test.txt"
Set InputFSO = CreateObject("Scripting.FileSystemObject")
Set InputFileObject = InputFSO.OpenTextFile(InputFile)
HeaderLine = InputFileObject.ReadLine
Do While InputFileObject.AtEndOfStream <> True
strTemp = InputFileObject.SkipLine
Loop
TotalLines = InputFileObject.Line-1
Set OutputFSO = CreateObject("Scripting.FileSystemObject")
Set OutputFileObject = OutputFSO.CreateTextFile("out.txt")
#Code for reading line by line and writing it to another file
Do While not InputFileObject.AtEndOfStream
line = InputFileObject.Readline
OutputFileObject.WriteLine(line)
Loop
Set InputFileObject = Nothing
Set OutputFileObject = Nothing
Wscript.Echo "Completed"
You're looping through the entire input file in the first Do loop when you are trying to get the line count. So the InputFileObject is already "AtEndOfStream" when you hit the second Do loop. Therefore, none of the code inside the second loop is executing.
Consider eliminating the first Do loop and count the lines in the file at the end of the other loop (unless in your real program the logic in the first loop is required?).
The alternative is to close the input file and reopen it. The problem in this case is that you'll wind up reading the file twice.
'Close and reopen the file from the top...
InputFileObject.Close
Set InputFileObject = InputFSO.OpenTextFile(InputFile)

Extracting strings and writing to a new file

I was googling around but didn't find the right answer, perhaps people from here are willingly and able to help me.
I'm very new to VBS or WSH and I like to have a solution for this problem:
I'm searching for textstrings within a file without a line break (only one line). The textstrings I'm looking for start always with the same content "jpgline" and ends with the three letters "qbm". How can we extract each sentence (the strings are always 64 chars long) containg "jpgline....qbm" into a separate file.
I'm looking for a solution in Visual Basic Script as I use Windows 7.
Thanks in advance
M i k e
Use a regular expression:
Set re = New RegExp
re.Pattern = "^jpgline.*qbm$"
re.IgnoreCase = True
Set fso = CreateObject("Scripting.FileSystemObject")
Set inFile = fso.OpenTextFile("C:\path\to\input.txt")
Set outFile = fso.OpenTextFile("C:\path\to\output.txt", 2, True)
Do Until inFile.AtEndOfStream
line = inFile.ReadLine
If re.Test(line) Then outFile.WriteLine line
Loop
inFile.Close
outFile.Close
As your input file has no lines, use .ReadAll() to load its entire content into a string variable. Apply a RegExp to get all parts (Matches) defined by the pattern "jpgline.{N}qbm" where N is either 64 or 64 - the length of the pre/suffix. Ansgar has shown how to open and write to the output file.
Use the RegExp Docs to learn about .Execute and how to loop over the resulting match collection. The docs will tell you about .Test too.

VB script + change word in VB script on text file

how to change word in text file by VB script (like sed in unix)
You can use the FileSystemObject Object. Some notes:
Set fs = CreateObject("Scripting.FileSystemObject")
sf = "C:\Docs\In.txt"
Set f = fs.OpenTextFile(sf, 1) ''1=for reading
s = f.ReadAll
s = Replace(s, "Bird", "Cat")
f.Close
Set f = fs.OpenTextFile(sf, 2) ''2=ForWriting
f.Write s
f.Close
Following steps: (when tackling a computing problem, divide and conquer!)
Open the text file
Save file contents to string variables
Close the text file!
Search variable for the word
Replace the word(s)
Save the variable as a text file
overwriting old one
With the help of Google, you should be able to search and discover how to achieve all of the above points.

Resources