how to replace #VARIABLE in text file with value of Emails in xml file - vbscript

How i can read this xml file
<Subs>
<Sub Report="BusinessSummarySubs" EMails="lalla#yahoo.com; haha#yahoo.com">
<Sub Report="PlayerSubs" EMails="hehe#hotmail.com">
</Subs>
and replace #VARIABLE in BusinesSummarySubs.txt with EMails value in
Here is the content(part of the content) from BusinessSumarySubs.txt
CType(extensionParams(0),ParameterValue).Name = "TO"
CType(extensionParams(0),ParameterValue).Label = ""
CType(extensionParams(0),ParameterValue).Value = "#VARIABLE"

If you look here, you'll see how to search for and to access attributes. Follow the link chain to 'the same for text' and do a mental diff, if you want to get a skeleton for a minimal XML processing script to use for your next task.
Single placeholder substitution in VBScript is easy: just use Replace:
>> attr = "lalla#yahoo.com; haha#yahoo.com"
>> content = "... .Value = ""#VARIABLE"" ..."
>> ph = "#VARIABLE"
>> WScript.Echo Replace(content, ph, attr)
>>
... .Value = "lalla#yahoo.com; haha#yahoo.com" ...
>>

Something like this i suposed
set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")
for each Emails in xmlDoc.documentElement.childNodes
document.write(Emails .nodename)
document.write(": ")
document.write(Emails .text)
next

Related

Batch Commands to find and Replace string

I want batch commands to find and replace string in word file and also renaming that file with same string and that too for a folder.
Multiple files needs to be searched and replaced with string and at the same time file name should be checked also.
There exists no integrated funtction in batch.
Powershell has such functions, but i would consider using fart.exe, which is easier to use.
Here is the link -> http://fart-it.sourceforge.net/
//EDIT: Looks like i have not recognized the "word file".
If thats the case i don't know any possibility to do this with batch/cmd.
Here is a macro script by Allen Wyatt that can do this.
Source
Public Sub MassReplace()
With Application.FileSearch
.LookIn = "C:\" ' where to search
.SearchSubFolders = True ' search the subfolders
.FileName = "*.doc" ' file pattern to match
' if more than one match, execute the following code
If .Execute() > 0 Then
' for each file you find, run this loop
For i = 1 To .FoundFiles.Count
' open the file based on its index position
Documents.Open FileName:=.FoundFiles(i)
' search and replace the address
selection.Find.ClearFormatting
selection.Find.Replacement.ClearFormatting
With selection.Find
.Text = "OldAddress"
.MatchCase = True
.Replacement.Text = "NewAddress"
End With
selection.Find.Execute Replace:=wdReplaceAll
' replace e-mail address
With selection.Find
.Text = "Oldemail"
.Replacement.Text = "Newemail"
End With
selection.Find.Execute Replace:=wdReplaceAll
' save and close the current document
ActiveDocument.Close wdSaveChanges
Next i
Else
' if the system cannot find any files
' with the .doc extension
MsgBox "No files found."
End If
End With
End Sub
Change these 3 lines based on your own needs:
.LookIn = "C:\" ' where to search
.SearchSubFolders = True ' search the subfolders
.FileName = "*.doc" ' file pattern to match
Aside from that, doing this from batch file (specifically because you are talking word documents) is outside of CMD's abilities.

VB Script for Automating File Creation (On-Demand Opportunity!)

I'm a Financial person and not quite the VB scripting guru, but I'm wondering if someone could create a sample vb script based on my requirements.
Whoever provides the solution first and the solution works on my end will have an opportunity (paid of course) to create more of these custom solutions where I work at. It wouldn't be full-time position, but more of an On-Demand opportunity.
Requirements:
To be able to read a text file that is delimited by comma and has various entries
For example, in text file...
SEC_E_All_Entities,HSII,SL_DIMENSION,READWRITE,#IDESCENDANTS,N
SEC_E_ENT_Americas,Americas,SL_DIMENSION,READ,MEMBER,N
And perform the following...
Create an XML file per entry, based on the first value
For example:
Create SEC_E_All_Entities.XML and SEC_E_ENT_Americas.XML
Within each file, write the contents where you can see how the values match each tag.
For example:
In the SEC_E_All_Entities.XML file, write...
<?xml version="1.0" encoding="UTF-8" ?>
<acls>
<acl>
<name>SEC_E_All_Entities</name>
<objectName>HSII</objectName>
<objectType>SL_DIMENSION</objectType>
<accessMode>READWRITE</accessMode>
<flag>#IDESCENDANTS</flag>
<isUser>N</isUser>
</acl>
</acls>
In the SEC_E_All_Americas.XML file, write...
<?xml version="1.0" encoding="UTF-8" ?>
<acls>
<acl>
<name>SEC_E_ENT_Americas</name>
<objectName>Americas</objectName>
<objectType>SL_DIMENSION</objectType>
<accessMode>READ</accessMode>
<flag>MEMBER</flag>
<isUser>N</isUser>
</acl>
</acls>
Regards,
Judy
Sample script
'Step 1 - Read the file and store the content in memory (an array)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Users\pankaj.jaju\Desktop\StackOverflow\acls.txt", 1)
arrFile = Split(objFile.ReadAll, vbCrLf) 'will store one line in each array index
objFile.Close
'Step 2 - Load the XML template in memory
Set xmlTemplate = CreateObject("MSXML2.DOMDocument.6.0")
With xmlTemplate
.ValidateOnParse = True
.Async = False
.LoadXML "<?xml version=""1.0"" encoding=""UTF-8"" ?>" & _
"<acls>" & _
"<acl>" & _
"<name></name>" & _
"<objectName></objectName>" & _
"<objectType></objectType>" & _
"<accessMode></accessMode>" & _
"<flag></flag>" & _
"<isUser></isUser>" & _
"</acl>" & _
"</acls>"
End With
'Step 3 - Load the relevant fields for which the data is to be set from csv file
Set nodeFields = xmlTemplate.DocumentElement.SelectNodes("/acls/acl/*")
'Step 4 - Read each line of text and create XML
For i = LBound(arrFile) To UBound(arrFile)
arrLine = Split(arrFile(i), ",") 'will split the line into various fields (to be used to create the xml)
For j = LBound(arrLine) To UBound(arrLine) 'set values for each field
nodeFields(j).Text = arrLine(j)
Next
Set xmlNew = xmlTemplate
xmlNew.Save objFSO.BuildPath("C:\Users\pankaj.jaju\Desktop\StackOverflow\", nodeFields(0).Text & ".xml") 'copy modified template as new xml file
Set xmlNew = Nothing
Next

Heading image or string?

I just created a page which the user can choose the mode to show, such as icon (image) or just temperature (text). Those are my code:
<?lc
#tell the browser that we will send binary data (png)
put header("Content-type:image/png")
#get ip address
if $_GET["ip"] = "" then
put $_SERVER["REMOTE_ADDR"] into ipAddress
else
put $_GET["ip"] into ipAddress
end if
#get the mode
put $_GET["mode"] into mode
#get longtitude and latitude data
put url ("http://vo.afteroffice.com/r/v/public/pub/geoloc?ip=" & ipAddress) into geoInfo
split geoInfo by cr and ":"
#input lon and lat data into url
put url ("http://api.yr.no/weatherapi/locationforecast/1.8/?lat=" & geoInfo["latitude"] & ";lon=" & geoInfo["longtitude"]) into weatherDetail
#create tree from XML data
put revCreateXMLTree(weatherDetail, false, true, false) into tLocation
#get data about temperature and clouds
put revXMLAttribute(tLocation,"weatherdata/product/time/location/temperature", "value") into temperature
#create the output
if mode = "temperature" then
put "The temperature is " & temperature
else if mode = "icon" then
put URL("binfile:icon/hot01.png")
else if mode = "" then
put "Oke"
end if
?>
My image & text problems are:
If I don't put put header("Content-type:image/png") in my code and I call mode=icon, the result will be "‰PNG IHDRPPŽò­tEXtSoftwareAdobe ImageReadyqÉe
If I put put header("Content-type:image/png") in my code and I call mode=temperature, the result will be "Te image cannot displayed, cause it contains error"
Could you help me? Thank you.
And how about adding something like
if mode = "temperature" then
# do not put header
else if mode = "icon" then
put header("Content-type:image/png")
end if
at the position where you have your content type header now?
You just have to add the definition of mode at the beginning instead of the middle of your script

VBS Readline - using instr(), to match data whilst ignoring extra spaces

I'm trying to find a way to enhance the reliability of my script. It already works but can be thrown off with a simple extra space in the imported text file.
So I'd like to change my script to Readline if I can find a way to do something like:
Example of text in the .txt file:
FLIGHTS OVER TUSKY PLEASE FILE:
AT OR WEST OF A LINE RBV..LLUND..BAYYS..PUT..DIRECT
FLIGHTS OVER EBONY PLEASE FILE:
AT OR WEST OF A LINE RBV..LLUND..BAYYS..PUT..DIRECT
I know the following doesn't work but if there was a simple modification this would be good.
set WshShell = WScript.CreateObject("WScript.Shell")
Return = WshShell.Run("C:\Downloads\software\putty.exe -load "testing")
set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.OpenTextFile("C:\Users\AW\Desktop\Entries1.txt")
strLine = objFile.ReadAll
If InStr(strLine1, "OVER TUSKY PLEASE") and InStr(strLine2, "BAYYS..PUT..DIRECT") Then
trans307="TUSKY"
ind306="4"
WHAT I'M USING NOW:
I edit the text file in notepad++ to FIND & REPLACE "\n" with "" and "\r" with " " and then it's all one text string and I search for strings within that string.
If InStr(strLine, "FLIGHTS OVER TUSKY PLEASE FILE: AT OR WEST OF A LINE ..RBV..LLUND..BAYYS..PUT..DIRECT") _
or InStr(strLine, "FLIGHTS OVER TUSKY PLEASE FILE: AT OR WEST OF A LINE RBV..LLUND..BAYYS..PUT...DIRECT") Then
trans308C="TUSKY"
ind308C="4"
Problem: If the creators of the text file put another space " " anywhere in this line "AT OR WEST OF A LINE RBV..LLUND..BAYYS..PUT..DIRECT" the script will not identify the string. In the above example I have had to create another or InStr(strLine, "") statement with an extra space or with a couple of dots.
UPDATE:
I will try something like:
set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.OpenTextFile("C:\Users\AW\Desktop\Entries1.txt")
strLine1 = objFile.Readline(1)
strLine2 = objFile.Readline(2)
If InStr(strLine1, "FLIGHTS OVER TUSKY") and InStr(strLine2, "RBV..LLUND..BAYYS..PUT..DIRECT") Then
trans1="TUSKY"
ind1="4"
and see if I can get that to read 2 lines at a time, and loop through the text file.
If you're scared of regex and looking for an alternative, you could create a clunky function to add to your script. Based on your samples, it would seem that fullstops are also never normally used for normal purposes and tend to represent spaces. (I would recommend using Regex instead!)
Using these presumptions, you could create a clunky function like this, that looks for fullstops, and converts them to spaces, removing extra spaces.. Obviously, this relies heavily on your input source files not changing too much - you really should be using a regex to work this stuff out properly.
You could test for the basic expected results using something like the function below.
For example say you had a line of text set in firLine with multiple spaces or fullstops, the function would recognize this:
firLine = "THIS.IS.A.TEST..YOU...SEE MULTIPLE SPACES"
if instr(sanitize(firLine),"THIS IS A TEST YOU SEE MULTIPLE SPACES") then
wscript.echo "Found it"
End If
Here's the clunky function that you could just paste at the end of your script:
Function sanitize(srStr)
Dim preSanitize, srC, spaceMarker
preSanitize = ""
for srC = 1 to len(srStr)
if mid(srStr, srC, 1) = "." then
preSanitize = preSanitize & " "
else
preSanitize = preSanitize & mid(srStr, srC, 1)
End If
spaceMarker = false
sanitize = ""
for srC = 1 to len(preSanitize)
If mid(preSanitize, srC, 1) = " " then
if spaceMarker = false then
sanitize = sanitize & mid(preSanitize, srC, 1)
spaceMarker = true
End If
else
sanitize = sanitize & mid(preSanitize, srC, 1)
spaceMarker = false
End If
Next
End Function
InStr() is a good tool for checking whether a strings contains a fixed/literal string or not. To allow for variation, you should use Regular Expressions (see this or that).
First of all, however, you should work on your specs. Describe in plain words and with some samples what you consider (not) to be a match.
E.g.: A string containing the words "FLIGHTS", "OVER", and "TUSKY" in that order with at least one space in between is a match - "FLIGHTS OVER TUSKY", "FLIGHTS OVER TUSKY"; "FLIGHTS OVER TUSKANY" is a 'near miss' - what about "AIRFLIGHTS OVER TUSKY"?
GREAT NEWS! I finally figured out how to do this.
Here is a snippet from "Entries1.txt"
FLIGHTS OVER BRADD KANNI PLEASE FILE:
VIA J174.RIFLE..ACK..DIRECT
OR RBV.J62.ACK..DIRECT
FLIGHTS OVER KANNI WHALE PLEASE FILE:
VIA J174.RIFLE..ACK..DIRECT OR
FLIGHTS OVER WHALE PLEASE FILE:"
ETC, ETC
set WshShell = WScript.CreateObject("WScript.Shell")
set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.OpenTextFile("C:\Users\AW\Desktop\Entries1.txt")
Do until objFile.AtEndOfStream
firLine = objFile.ReadLine
If InStr(firLine, "FLIGHTS OVER KANNI WHALE PLEASE") Then
secLine = objFile.ReadLine
If InStr(secLine, "J174.RIFLE..ACK..DIRECT") Then
'I'm going to change the below once I piece it all together.
WScript.Echo "works"
Else WScript.Echo "Not found"
'cut, paste and modify all my "IF" statements below
End If
End If
loop

VBScript - using IF statements in a mail script?

I really need some quick tips here.
I've got this VBScript script which sends an e-mail. And I want to do several checks to see if an attribute is true and if it is, write an additional line in the mail.
How can I do this? This is part of the script:
obMessage.HTMLBody = ""_
& "<MENU>"_
& "<LI type = square>This is a line</i>."_
I want something which looks like this:
obMessage.HTMLBody = ""_
& "<MENU>"_
If statement1 = true Then
& "<LI type = square>This is an additional line</i>."_
end if
Preferrably, could some select statements be made? I don't really mind what the code looks like, I just want it to work as soon as possible :)
It will look like spaghetti code no matter how you do it. This is one of the most straight forward approach:
obMessage.HTMLBody = & "<MENU>"
if statement1 then
obMessage.HTMLBody = obMessage.HTMLBody & "<LI type=""square"">This is a line</LI>."
end if
if statement2 then
obMessage.HTMLBody = obMessage.HTMLBody & "<LI type=""square"">This is another line</LI>."
end if
However, I suggest that you concatenate the lines to a temporary string, the assign the resulting string to obMessage.HTMLBody, such as:
Dim Foo
Foo = "<MENU>"
if statement1 then
Foo = Foo & "<LI type=""square"">This is a line</LI>."
end if
.
.
.
obMessage.HTMLBody = Foo
Something like:
obMessage.HTMLBody = "Begin Text" & _
IIf(statement1 = true, "<LI type = square>This is an additional line</i>.", "") & _
"Further text"
Should work ok.

Resources