I am writing a utility to collect all system information from all devices on a network to an XML document, and one of the values is a certain software version number. Unfortunately the version number is only stored in a single text file on each machine (c:\master.txt) the even more fun part is, each text file is formatted differently depending on the image that was used.
One could say
Product Number: 11dsSt2 BRANDONII,STNS6.0.2.200
The next
Ver: 22335TS BOX2 S6.1.3.011,STN
and so on.
What I did, is create a VBS that looks for a number pattern that matches the version pattern, which is
[A-Z]#.#.#.###
This works, however I just want to output that pattern, not the entire line. Here is my code. Any Suggestions?
Const ForReading = 1
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "[A-Z]{1}[0-9]{1}.[0-9]{1}.[0-9]{1}.[0-9]{3}"
objregex.global = true
objregex.ignorecase = true
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\master.txt", ForReading)
Do Until objFile.AtEndOfStream
strSearchString = objFile.ReadLine
set colMatches = objRegEx.Execute(strSearchString)
If colMatches.Count > 0 Then
Addmatch
End If
Loop
objFile.Close
Sub Addmatch 'Creates a text file with the part number
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile1 = objFSO.OpenTextFile("C:\test.txt", ForAppending, True)
objFile1.Writeline strSearchString
objFile1.Close
end sub
You're storing the entire line you read from the file (that you have in strSearchString) instead of just the matched text. Use something like this instead (untested!):
if ColMatches.Count > 0 then
AddMatch(ColMatches(0)) ' Just grab the matched text and pass to AddMatch
End If
Sub AddMatch(strMatchedText) ' Change to accept parameter of text to write out
' Other code
objFile1.Writeline strMatchedText
objFile1.Close
End Sub
Use the first/one and only match to obtain the value, and pass this to a slightly modified version of Addmatch:
If colMatches.Count > 0 Then
Addmatch colMatches(0).Value
End If
...
Sub Addmatch(sWhatToWriteLn) ' some truthful comment
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile1 = objFSO.OpenTextFile("C:\test.txt", ForAppending, True)
objFile1.Writeline sWhatToWriteLn
...
Related
I'm getting the following error for this code. Please could you advise where it is wrong? Line 71 is "urls2 = objInputFile.ReadAll".
Line 71
Character 1
Error: Input past end of file
Code: 800A003E
Source: Microsoft VBScript runtime error.
inputfile = "C:\Evernote.html"
outputfolder = "c:\"
msgbox("launched. press ok to continue")
'create urls1.txt
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set objOutputFile = objFileSystem.CreateTextFile(outputfolder & "urls1.txt", TRUE)
'read inputfile (evernote exported html)
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set objInputFile = objFileSystem.OpenTextFile(inputfile, 1)
html = objInputFile.ReadAll
objInputFile.Close
'split html var
html = Split(html, "<tr><td><b>Source:</b></td><td><a href=""")
'loop through html array and clean up the results so you get just the urls
'and write them to urls1.txt
For i = 1 To UBound(html)
checkA = InStr(html(i), """")
if checkA > 1 then
html(i) = Split(html(i), """")
urls = html(i)(0)
objOutputFile.WriteLine(urls)
end if
Next
'remove duplicates
'create urls2.txt
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set objOutputFile = objFileSystem.CreateTextFile(outputfolder & "urls2.txt", TRUE)
'read urls1.txt and remove duplicates and write results to urls2.txt
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = outputfolder & "urls1.txt"
Set objFile = objFS.OpenTextFile(strFile)
Set d = CreateObject("Scripting.Dictionary")
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If Not InStr(strLine,"--------") >0 Then
If Not d.Exists(strLine) Then
d.Add strLine , 0
End If
End If
Loop
x=d.Items
For Each strKey In d.keys
objOutputFile.WriteLine(strKey)
Next
'sort alphabetically
'read urls2.txt and sort everything alphabetically
'read urls2.txt
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set objInputFile = objFileSystem.OpenTextFile(outputfolder & "urls2.txt", 1)
urls2 = objInputFile.ReadAll
objInputFile.Close
'split each line into array
urls2 = Split(urls2, VBCrLf)
'sort urls2 array by alphabet with bubble sort method
For i = (UBound(urls2) - 1) to 0 Step -1
For j= 0 to i
If UCase(urls2(j)) > UCase(urls2(j+1)) Then
strHolder = urls2(j+1)
urls2(j+1) = urls2(j)
urls2(j) = strHolder
End If
Next
Next
'write the sorted version of urls2.txt in urlsfinal.txt
'create urlsfinal.txt
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set objOutputFile = objFileSystem.CreateTextFile(outputfolder & "urlsfinal.txt", TRUE)
'write all sorted vars from urls2 array to urlsfinal.txt
For i = 0 to UBound(urls2)
objOutputFile.WriteLine(urls2(i))
next
msgbox("all done")
The problem is your source file urls2.txt is empty. The reason for this is you are not closing your files after you write to them. You need to add this after you have finished writing out to urls1.txt and urls2.txt.
objOutputFile.Close
Also, you don't need to continually recreate the instance of objFileSystem every time you access the files. You can instantiate it once at the top.
Be sure to be a good memory citizen and destroy all objects you set in your code.
Set objFileSystem = Nothing
I have to update test.txt with new values i,e. delete value {"AAA":"777" , "BBB":"888"} and add {"ABC:"123","GHN:"246"} in tag
but my script append the value as {"AAA":"777" , "BBB":"888"} {"ABC:"123","GHN:"246"
strFile="test.txt"
Const ForAppending = 8
set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.OpenTextFile(strFile, ForAppending , True)
objFile.Write("{")
Dim dict
Set dict= CreateObject("Scripting.Dictionary")
dict.Add "ABC","123"
dict.Add "GHN","246"
k=dict.Keys
v=dict.Items
For j=0 to dict.Count-1
objFile.Write(chr(34)&k(j)&":"&chr(34)&v(j)&chr(34)&",")
Next
==============
Test.txt
[Valid]
api = https://test.com
Alpha = FALSE
tag = {"AAA":"777" , "BBB":"888"}
I've never used the dictionary object/don't know how, so this may not be a viable solution for you depending on whether you need certain things that the object provides... but without resorting to regex, could you not simply do something like this?
Dim fso, filecontent, file
Set fso = CreateObject("Scripting.FileSystemObject")
file = "C:\temp\test.txt"
filecontent = fso.OpenTextFile(file, 1, False).ReadAll
filecontent = Replace(filecontent, "{""AAA"":""777"" , ""BBB"":""888""}", "{""ABC:""123"",""GHN:""246""}")
fso.OpenTextFile(file, 2, True).Write filecontent
I'm trying to make a settings file for my. Vbs happens that it also creates settings for my. Bat file and add new text to invest the same place the text on the same line the file creates a new line.
Ex:
CBNC-Blacklist =TEST
When I add a new text with the old it is well
CBNC-Blacklist =TEST
SDSADADA
Sub Main()
Dim Blist
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("A\a00_Blacklist.ini")
If objFile.Size > 0 Then
Set objReadFile = objFSO.OpenTextFile("A\a00_Blacklist.ini", 1)
strContents = objReadFile.ReadAll
objReadFile.Close
End if
Set WshShell = CreateObject("WScript.Shell")
Const ForReading = 1, ForWriting = 2, ForAppending = 8, CreateIfNeeded = True
Blist = Inputbox(vbcrlf & "Digite abaixo os itens que você deseja adicionar" & vbcrlf & "na Blacklist separando os mesmos com espaços:", "NoCheating", strContents)
If Blist = "" Then
msgbox "A Blacklist NÃO foi alterada!",vbExclamation,"NoCheating"
Else
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objMiFichero = objFSO.OpenTextFile("A\a00_Blacklist.ini", ForWriting, CreateIfNeeded)
objMiFichero.WriteLine( Blist )
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objMiFichero = objFSO.OpenTextFile("A\a00_Config_2.ini", ForWriting, CreateIfNeeded)
objMiFichero.WriteLine( "CBNC-Blacklist="&Blist )
msgbox "A Blacklist foi atualizada!",vbInformation,"NoCheating"
End if
End Sub
On Error Resume Next
Main
If Err.Number Then
WScript.Quit 4711
End If
Unfortunately, VBScript doesn't have a "seek" statement to position a file pointer. And you can't combine any of the file modes (ForReading, ForWriting, ForAppending) so that prevents you from "reading" the file until you get to a certain position and then writing at that position. (I've always wondered why they chose bit-position values (1, 2, 8) when you can't combine the values anyway. They could have just used 1, 2, 3).
What you can do, however, is parse your original ini file and write to a new one. If nothing should change, just write the original line to the new file. If the value should change, write a new value to your new file. For example, if you wanted to change the value of CBNC-Blacklist from TEST to NEW:
Set FileIn = objFSO.OpenTextFile("c:\settings.ini", ForReading)
Set FileOut = objFSO.CreateTextFile("c:\settings-new.ini", True)
Do Until FileIn.AtEndOfStream
' Read a line from the original file...
strLine = FileIn.ReadLine()
' Is this the line we want to change?
If Left(strLine, 15) = "CBNC-Blacklist=" Then
' Write the new value to the new file.
FileOut.WriteLine "CBNC-Blacklist=NEW"
Else
' Just write the original line to the new file.
FileOut.WriteLine strLine
End If
Loop
FileIn.Close
FileOut.Close
' Finally, replace the original settings file with the new one...
objFSO.CopyFile "c:\settings-new.ini", "c:\settings.ini", True
Of course you could make this more modular by creating a subroutine that accepts the "key" and "value" as parameters. I just wanted to keep things simple for this example.
I got what I wanted and I have to say it was very difficult anyway thanks for the help and the code below is the code ... I could always read the first line. Ini file and always writes in the first well.
Sub Main()
Dim Blist
Const ForReading = 1, ForWriting = 2, ForAppending = 8, CreateIfNeeded = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("A\a00_Blacklist.ini", ForReading)
For i = 4 to 3
objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Set WshShell = CreateObject("WScript.Shell")
Blist = Inputbox(vbcrlf & "Digite abaixo os itens que você deseja adicionar" & vbcrlf & "na Blacklist separando os mesmos com espaços:","NoCheating",strLine)
If Blist = "" Then
msgbox "A Blacklist NÃO foi alterada!",vbExclamation,"NoCheating"
Else
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objMiFichero = objFSO.OpenTextFile("A\a00_Blacklist.ini", ForWriting, CreateIfNeeded)
objMiFichero.WriteLine( Blist )
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objMiFichero = objFSO.OpenTextFile("A\a00_Config_2.ini", ForWriting, CreateIfNeeded)
objMiFichero.WriteLine( "CBNC-Blacklist="&Blist )
msgbox "A Blacklist foi atualizada!",vbInformation,"NoCheating"
End if
End Sub
On Error Resume Next
Main
If Err.Number Then
WScript.Quit 4711
End If
I have two text files with following content:
file1.txt:
Windows 1.36
Linux 2.78
MacOS 3.45
Ubuntu 4.12
FreePhysicalMemory 30.12
TotalVisibleMemorySize 48.00
file2.txt:
MacOS 6.39
Windows 4.42
Linux 5.76
Android 3.46
FreePhysicalMemory 31.65
TotalVisibleMemorySize 48.00
output.xls:
OPERATING SYSTEM SERVER1 SERVER2
Windows 1.36 4.42
Linux 2.78 5.76
MacOS 3.45 6.39
Ubuntu 4.12 0.00
Android 0.00 3.46
FreePhysicalMemory 30.12 31.65
TotalVisibleMemorySize 48.00 48.00
I want to achieve following two things in a VBScript program or in any, which is appropriate to run on windows server:
Insert the contents of both file1.txt and fil2.txt in a excel
sheet output.xls like above.
Mail the content of output.xls file as a body in email.
I think, Point2 is being achieved using below code but not getting how can i achieve Point1 in same program.
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const FileToBeUsed = "c:\output.xls"
Dim objCDO1
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(FileToBeUsed, ForReading)
Set objCDO1 = CreateObject("CDO.Message")
objCDO1.Textbody = f.ReadAll
f.Close
objCDO1.TO ="sunny#abc.com"
objCDO1.From = "dontreply#abc.com"
objCDO1.Subject = "Server Memory"
objCDO1.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration /sendusing") = 2
objCDO1.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtpb.intra.abc.com"
objCDO1.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration /smtpserverport") = 25
objCDO1.Configuration.Fields.Update
objCDO1.Send
Set f = Nothing
Set fso = Nothing
Any suggestion is highly appreciated.
Note:My purpose here is to send email with properly formatted and aligned data.
EDIT1:
This is all i could do regarding points 1,2,3.
Don't know how to consolidate all this in a HTML email Body format..:(
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fso,f,objFSO,objFile,objRE, FileName
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
const strFileName1 = "D:\file1.txt"
const strFileName2 = "D:\file2.txt"
Set objFile = objFSO.OpenTextFile(strFileName1, fsoForReading) + objFSO.OpenTextFile(strFileName2, fsoForReading)
objobjFile.Close
Set objFile = Nothing
Set objFSO = Nothing
Set objRE = New RegExp
With objRE
.Pattern = "[A-Z][0-9]"
.IgnoreCase = False
.Global = False
End With
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
const strFileName1 = "D:\file1.txt"
const strFileName2 = "D:\file2.txt"
Set objFile = objFSO.OpenTextFile(strFileName1, fsoForReading) + objFSO.OpenTextFile(strFileName2, fsoForReading)
Do Until objFile.AtEndOfStream
strSearchString = objFile.ReadLine
Set colMatches = objRegEx.Execute(strSearchString)
If colMatches.Count > 0 Then
For Each strMatch in colMatches
Wscript.Echo strSearchString
Next
End If
Loop
objFile.Close
Class memory1class
Public operatingsystem, memory
End Class
Dim memory1dict: Set memory1 = CreateObject("Scripting.Dictionary")
Dim memory2dict: Set memory2 = CreateObject("Scripting.Dictionary")
Dim memory1: Set memory1 = new memory1class
With memory1
.operatingsystem = "?"
.memory = "?"
End With
memory1dict.Add "1", memory1
Dim memory1details: Set memory1details = memory1dict.Item("1")
WScript.StdOut.WriteLine("operatingsystem:" & memory1details.first & " " & memory1details.memory & " )
I don't want to directly give you code, because this is a good opportunity for you to learn more about the different options you have available for solving the problem yourself, but I can give you a rough outline of what I would do.
Open the two text files for reading. Windows provides a "Scripting" library which contains the FileSystemObject. You would declare an instance of it as below, and you can google for the specifics of how to use it to open a text file:
Set fso = CreateObject("Scripting.FileSystemObject")
Read each file in a line at a time. Parse each line to get the "name" part and the "number" part as distinct values. You can do this by combining the InStr, InStrRev, and Mid functions or you can use the more powerful RegExp library (VBScript's Regular Expression class).
Set re = New RegExp
Store each "name" and "value" into a data structure for later use - I recomment an associative array such as VBScript's Dictionary class. Using a separate one for each text file will allow you to cross-reference them later.
Set dictStats1 = CreateObject("Scripting.Dictionary")
To display the data formatted correctly in the email, I suggest using an HTML table. In addition to the TextBody, the CDO.Message object has an HTMLBody property to allow you to give the email structured formatting instead of just raw text. Extrapolating from the simple example on w3school.com, you can construct a function that will accept the two dictionaries and use them to construct the HTML table and return it as a string to be loaded into the email via the HTMLBody property.
I hope that helps! Let me know if you have any questions about the specifics.
How can we read and write some string into a text file using VBScript? I mean I have a text file which is already present so when I use this code below:-
Set fso = CreateObject("Scripting.FileSystemObject" )
Set file = fso.OpenTextFile("C:\New\maddy.txt",1,1)
This opens the file only for reading but I am unable to write anything
and when I use this code:-
Set fso = CreateObject("Scripting.FileSystemObject" )
Set file = fso.OpenTextFile("C:\New\maddy.txt",2,1)
I can just use this file for writing but unable to read anything. Is there anyway by which we can open the file for reading and writing by just calling the OpenTextFile method only once.
I am really new to VBScript. I am only familiar with C concepts.
Is there any link to really get me started with VBScript?
I guess I need to have a good knowledge of the objects and properties concepts.
You can create a temp file, then rename it back to original file:
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file.txt"
strTemp = "c:\test\temp.txt"
Set objFile = objFS.GetFile(strFile)
Set objOutFile = objFS.CreateTextFile(strTemp,True)
Set ts = objFile.OpenAsTextStream(1,-2)
Do Until ts.AtEndOfStream
strLine = ts.ReadLine
' do something with strLine
objOutFile.Write(strLine)
Loop
objOutFile.Close
ts.Close
objFS.DeleteFile(strFile)
objFS.MoveFile strTemp,strFile
Usage is almost the same using OpenTextFile:
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file.txt"
strTemp = "c:\test\temp.txt"
Set objFile = objFS.OpenTextFile(strFile)
Set objOutFile = objFS.CreateTextFile(strTemp,True)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
' do something with strLine
objOutFile.Write(strLine & "kndfffffff")
Loop
objOutFile.Close
objFile.Close
objFS.DeleteFile(strFile)
objFS.MoveFile strTemp,strFile
Find more about the FileSystemObject object at http://msdn.microsoft.com/en-us/library/aa242706(v=vs.60).aspx. For good VBScript, I recommend:
Option Explicit to help detect typos in variables.
Function and Sub to improve readilbity and reuse
Const so that well known constants are given names
Here's some code to read and write text to a text file:
Option Explicit
Const fsoForReading = 1
Const fsoForWriting = 2
Function LoadStringFromFile(filename)
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filename, fsoForReading)
LoadStringFromFile = f.ReadAll
f.Close
End Function
Sub SaveStringToFile(filename, text)
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filename, fsoForWriting)
f.Write text
f.Close
End Sub
SaveStringToFile "f.txt", "Hello World" & vbCrLf
MsgBox LoadStringFromFile("f.txt")
You could open two textstreams, one for reading
Set filestreamIn = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Test.txt,1)
and one for appending
Set filestreamOUT = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Test.txt,8,true)
The filestreamIN can read from the begining of the file, and the filestreamOUT can write to the end of the file.
Don't think so...you can only use openTextFile for reading (1), writing (2), or appending (8). Reference here.
If you were using VB6 instead of VBScript, you could do:
Open "Filename" [For Mode] [AccessRestriction] [LockType] As #FileNumber
Using the Random mode. For example:
Open "C:\New\maddy.txt" For Random As #1
You could put it in an Excel sheet, idk if it'll be worth it for you if its needed for other things but storing info in excel sheets is a lot nicer because you can easily read and write at the same time with the
'this gives you an excel app
oExcel = CreateObject("Excel.Application")
'this opens a work book of your choice, just set "Target" to a filepath
oBook = oExcel.Workbooks.Open(Target)
'how to read
set readVar = oExcel.Cell(1,1).value
'how to write
oExcel.Cell(1,2).value = writeVar
'Saves & Closes Book then ends excel
oBook.Save
oBook.Close
oExcel.Quit
sorry if this answer isnt helpful, first time writing an answer and just thought this might be a nicer way for you
You could also read the entire file in, and store it in an array
Set filestreamIN = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Test.txt",1)
file = Split(filestreamIN.ReadAll(), vbCrLf)
filestreamIN.Close()
Set filestreamIN = Nothing
Manipulate the array in any way you choose, and then write the array back to the file.
Set filestreamOUT = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Test.txt",2,true)
for i = LBound(file) to UBound(file)
filestreamOUT.WriteLine(file(i))
Next
filestreamOUT.Close()
Set filestreamOUT = Nothing
Regardless of what you're trying to do there should be no need to read to and write to a file at the same time. It would also use more memory which should always be avoided. I'd suggest reading the entire file using the .ReadAll method and then close it and do whatever you need to do with the data (assuming you read the contents into a variable) and then do a write to the same file and overwrite the file. If you're concerned with having something go wrong when over-writing the current file you could always try to write it to a different file and throw an error if that doesn't work before trying to over-write the original.
Below is some simple code to execute this:
sLocation = "D:\Excel-Fso.xls"
sTxtLocation = "D:\Excel-Fso.txt"
Set ObjExl = CreateObject("Excel.Application")
Set ObjWrkBk = ObjExl.Workbooks.Open(sLocation)
Set ObjWrkSht = ObjWrkBk.workSheets("Sheet1")
ObjExl.Visible = True
Set FSO = CreateObject("Scripting.FileSystemObject")
Set FSOFile = FSO.CreateTextFile (sTxtLocation)
sRowCnt = ObjWrkSht.usedRange.Rows.Count
sColCnt = ObjWrkSht.usedRange.Columns.Count
For iLoop = 1 to sRowCnt
For jLoop = 1 to sColCnt
FSOFile.Write(ObjExl.Cells(iLoop,jLoop).value) & vbtab
Next
Next
Set ObjWrkBk = Nothing
Set ObjWrkSht = Nothing
Set ObjExl = Nothing
Set FSO = Nothing
Set FSOFile = Nothing
This is for create a text file
For i = 1 to 10
createFile( i )
Next
Public Sub createFile(a)
Dim fso,MyFile
filePath = "C:\file_name" & a & ".txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile(filePath)
MyFile.WriteLine("This is a separate file")
MyFile.close
End Sub
And this for read a text file
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Set dict = CreateObject("Scripting.Dictionary")
Set file = fso.OpenTextFile ("test.txt", 1)
row = 0
Do Until file.AtEndOfStream
line = file.Readline
dict.Add row, line
row = row + 1
Loop
file.Close
For Each line in dict.Items
WScript.Echo line
WScript.Sleep 1000
Next