I have a strange result with this ASP code :
<%
Dim TEST_variable
TEST_variable = "0"
TEST_variable = CBool("0")
Response.Write "1- TEST_variable :"
Response.Write TEST_variable
Response.Write "<br/>"
Response.Write "2- TEST_variable :" & TEST_variable
%>
This Code display this result :
1- TEST_variable :False
2- TEST_variable :Faux
Why this Response.Write "2- TEST_variable :" & TEST_variable translate false to faux ?
beacause of the string concatenation vbscript "casts" the variant of subtype bool (TEST_variable) to a variant of the subtype string. during this cast a bool is translated to the language of the webserver.
Related
this my code
SqlStr = "Select Test From Tbl_Test Where Test ='" & TextBox1 & "'"
how add Like in Where Test and TextBox1 to TextBox1*?
SqlStr = "Select Test From Tbl_Test Where Test Like='" & TextBox1* & "'"
This code gives an error .
You need to remove the '=' and add appropriate '%'s (which refer to any number of any character) depending on your needs.
The resulting string should be like so:
Select Test From Tbl_Test Where Test Like '%yoursearchhere%'
e.g.
SqlStr = "Select Test From Tbl_Test Where Test Like '%" & TextBox1.Text & "%'"
For Reference:
https://learn.microsoft.com/en-us/sql/t-sql/language-elements/like-transact-sql?view=sql-server-ver15
How do I convert, in Classic ASP, this (example) comma separated Querystring
Illinois, Iowa, Wisconsin, Indiana, Kentucky,Missouri to
'Illinois', 'Iowa', 'Wisconsin', 'Indiana', 'Kentuck','Missouri'
I am using Dreamweaver and need this format to select records from a table which contain one of the states in the string Using IN clause.
If you need just the conversion here it is:
origstring = "Illinois, Iowa, Wisconsin, Indiana, Kentucky, Missouri"
convertedstring = "'" &replace(origstring, ", ","', '") &"'"
But let's say you (or anyone else) have a querystring with states:
strinclause = ""
arrstates = Split(Request.QueryString("states"), ",")
if(Ubound(arrstates) > 0) Then
for i=0 to Ubound(arrstates)
'VALIDATE/CLEAN YOUR ARRAY ITEMS AND BUILD YOUR STRING IN THIS WAY:
strinclause = strinclause & "'" &arrstates(i)& "',"
next
'NOW REMOVE LAST COMA
strinclause = Left(strinclause,Len(strinclause)-1)
end if
'HERE YOU CAN USE YOUR STRING THAT WILL BE IN THE FORMAT YOU ASKED
Jut spitballing, but you could do something like this:
<%
Dim qs : qs = Trim("" & Request.QueryString("states") ' e.g. "Illinois, Iowa, Wisconsin, Indiana, Kentucky, Missouri"
Dim cmd : Set cmd = Server.CreateObject("ADODB.Command")
cmd.ActiveConnection = db.connection ' defined elsewhere
cmd.ActiveConnection.CursorLocation = adUseClient ' 3
cmd.CommandType = adCmdText ' 1
cmd.CommandTimeout = 90
dim inp : inp = Split(qs, ",") ' states
dim sql, i : ReDim sql (ubound(inp)) ' for sql
for i = 0 to ubound(inp)
sql(i) = "select ?" ' parameter in string
cmd.Parameters.Append cmd.CreateParameter("#p" & i, adVarChar, adParamInput, 100, inp(i)) ' actual typed parameter object
next
cmd.CommandText = "select id, stock from warehouse where state in (" & Join(sql, " union ") & ")"
Set rs = cmd.Execute
Do While Not rs.eof
response.write "<p>" & rs("id") & ": " & rs("stock") & "</p>"
rs.MoveNext
Loop
Set rs = Nothing
Set cmd = Nothing
%>
I need to find the string "Test Case " & index in a txt file.
I give you an example of the lines you can find in this file:
<tr><td>Log_ in_U1A1</td></tr>
<tr><td>Form1</td></tr>
As you can see in the second line I have an occurrence of the string "Test Case".
What I want to do is to add another particular string in the line which preceeds the one where "Test Case 5" appears. For example:
<tr><td>Log_ in_U1A1</td></tr>
<tr><td>Beginning_of_DD_TC5</td></tr>
<tr><td>Form1</td></tr>
It's also important that the line I add has an index i which depends on the Test Case number, and i need to add it before the first occurrence of "Test Case" & i, i dont care about the following occurrences.
I tested if InStr function worked with an example:
Dim objFSO, filepath, objInputFile, tmpStr, substrToFind
Set objFSO = CreateObject("Scripting.FileSystemObject")
filepath = "C:\VBS\filediprova.txt"
substrToFind = "<tr><td><a href=" & chr(34) & "../Test case 5"
Set objInputFile = objFSO.OpenTextFile(filepath)
tmpStr = objInputFile.ReadLine
If InStr(tmpStr, substrToFind) <= 0 Then
WScript.Echo "No matches"
Else
WScript.Echo "Found match"
End If
And it works, it recognizes my substring. In this small example the txt file only contans the followingline:
<tr><td>Form1</td></tr>
Now, when I try to loop over a file with much more lines I have some problem, I use the same InStr function.
I wrote the following loop:
Do until objInputFile.AtEndOfStream
strToAdd = "<tr><td>Beginning_of_DD_TC" & CStr(index) & "</td></tr>"
substrToFind = "<tr><td><a href=" & chr(34) & "../Test case " & index
firstStr = "<?xml version" 'my file always starts like this
tmpStr = objInputFile.ReadLine
If InStr(tmpStr, substrToFind) <= 0 Then
If Instr(tmpStr, firstStr) > 0 Then
text = tmpStr 'to avoid the first empty line
Else
text = text & vbCrLf & tmpStr
End If
Else
text = text & vbCrLf & strToAdd & vbCrLf & tmpStr
index = index + 1
End If
Loop
What's wrong?
I'd recommend using a regular expressions instead of string operations for this:
Set fso = CreateObject("Scripting.FileSystemObject")
filename = "C:\VBS\filediprova.txt"
newtext = vbLf & "<tr><td>Beginning_of_DD_TC5</td></tr>"
Set re = New RegExp
re.Pattern = "(\n.*?Test Case \d)"
re.Global = False
re.IgnoreCase = True
text = f.OpenTextFile(filename).ReadAll
f.OpenTextFile(filename, 2).Write re.Replace(text, newText & "$1")
The regular expression will match a line feed (\n) followed by a line containing the string Test Case followed by a number (\d), and the replacement will prepend that with the text you want to insert (variable newtext). Setting re.Global = False makes the replacement stop after the first match.
If the line breaks in your text file are encoded as CR-LF (carriage return + line feed) you'll have to change \n into \r\n and vbLf into vbCrLf.
If you have to modify several text files, you could do it in a loop like this:
For Each f In fso.GetFolder("C:\VBS").Files
If LCase(fso.GetExtensionName(f.Name)) = "txt" Then
text = f.OpenAsTextStream.ReadAll
f.OpenAsTextStream(2).Write re.Replace(text, newText & "$1")
End If
Next
Wow, after few attempts I finally figured out how to deal with my text edits in vbs. The code works perfectly, it gives me the result I was expecting. Maybe it's not the best way to do this, but it does its job.
Here's the code:
Option Explicit
Dim StdIn: Set StdIn = WScript.StdIn
Dim StdOut: Set StdOut = WScript
Main()
Sub Main()
Dim objFSO, filepath, objInputFile, tmpStr, ForWriting, ForReading, count, text, objOutputFile, index, TSGlobalPath, foundFirstMatch
Set objFSO = CreateObject("Scripting.FileSystemObject")
TSGlobalPath = "C:\VBS\TestSuiteGlobal\Test suite Dispatch Decimal - Global.txt"
ForReading = 1
ForWriting = 2
Set objInputFile = objFSO.OpenTextFile(TSGlobalPath, ForReading, False)
count = 7
text=""
foundFirstMatch = false
Do until objInputFile.AtEndOfStream
tmpStr = objInputFile.ReadLine
If foundStrMatch(tmpStr)=true Then
If foundFirstMatch = false Then
index = getIndex(tmpStr)
foundFirstMatch = true
text = text & vbCrLf & textSubstitution(tmpStr,index,"true")
End If
If index = getIndex(tmpStr) Then
text = text & vbCrLf & textSubstitution(tmpStr,index,"false")
ElseIf index < getIndex(tmpStr) Then
index = getIndex(tmpStr)
text = text & vbCrLf & textSubstitution(tmpStr,index,"true")
End If
Else
text = text & vbCrLf & textSubstitution(tmpStr,index,"false")
End If
Loop
Set objOutputFile = objFSO.CreateTextFile("C:\VBS\NuovaProva.txt", ForWriting, true)
objOutputFile.Write(text)
End Sub
Function textSubstitution(tmpStr,index,foundMatch)
Dim strToAdd
strToAdd = "<tr><td>Beginning_of_CF5.0_Features_TC" & CStr(index) & "</td></tr>"
If foundMatch = "false" Then
textSubstitution = tmpStr
ElseIf foundMatch = "true" Then
textSubstitution = strToAdd & vbCrLf & tmpStr
End If
End Function
Function getIndex(tmpStr)
Dim substrToFind, charAtPos, char1, char2
substrToFind = "<tr><td><a href=" & chr(34) & "../Test case "
charAtPos = len(substrToFind) + 1
char1 = Mid(tmpStr, charAtPos, 1)
char2 = Mid(tmpStr, charAtPos+1, 1)
If IsNumeric(char2) Then
getIndex = CInt(char1 & char2)
Else
getIndex = CInt(char1)
End If
End Function
Function foundStrMatch(tmpStr)
Dim substrToFind
substrToFind = "<tr><td><a href=" & chr(34) & "../Test case "
If InStr(tmpStr, substrToFind) > 0 Then
foundStrMatch = true
Else
foundStrMatch = false
End If
End Function
This is the original txt file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type" />
<title>Test Suite</title>
</head>
<body>
<table id="suiteTable" cellpadding="1" cellspacing="1" border="1" class="selenium"><tbody>
<tr><td><b>Test Suite</b></td></tr>
<tr><td>TC_Environment_setting</td></tr>
<tr><td>TC_Set_variables</td></tr>
<tr><td>TC_Set_ID</td></tr>
<tr><td>Log_in_Admin</td></tr>
<tr><td>Set_Roles_Dispatch_Decimal</td></tr>
<tr><td>Log_ in_U1A1</td></tr>
<tr><td>Form1</td></tr>
<tr><td>contrD1</td></tr>
<tr><td>Logout</td></tr>
<tr><td>Log_ in_U1B1</td></tr>
<tr><td>Search&OpenApp</td></tr>
<tr><td>FormEND</td></tr>
<tr><td>Controllo END</td></tr>
<tr><td>Logout</td></tr>
<tr><td>Log_ in_U1A1</td></tr>
<tr><td>Form1</td></tr>
<tr><td>contrD1</td></tr>
<tr><td>Logout</td></tr>
<tr><td>Log_ in_U1B1</td></tr>
<tr><td>Search&OpenApp</td></tr>
<tr><td>FormEND</td></tr>
<tr><td>Controllo END</td></tr>
<tr><td>Logout</td></tr>
<tr><td>Log_ in_U1A1</td></tr>
<tr><td>Form1</td></tr>
<tr><td>Controllo DeadLetter</td></tr>
<tr><td>Logout</td></tr>
<tr><td>Set_Roles_Dispatch_Decimal</td></tr>
<tr><td>Logout_BAC</td></tr>
</tbody></table>
</body>
</html>
And this is the result I'm expecting
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type" />
<title>Test Suite</title>
</head>
<body>
<table id="suiteTable" cellpadding="1" cellspacing="1" border="1" class="selenium"><tbody>
<tr><td><b>Test Suite</b></td></tr>
<tr><td>TC_Environment_setting</td></tr>
<tr><td>TC_Set_variables</td></tr>
<tr><td>TC_Set_ID</td></tr>
<tr><td>Log_in_Admin</td></tr>
<tr><td>Set_Roles_Dispatch_Decimal</td></tr>
<tr><td>Log_ in_U1A1</td></tr>
<tr><td>Beginning_of_CF5.0_Features_TC5</td></tr>
<tr><td>Form1</td></tr>
<tr><td>Form1</td></tr>
<tr><td>contrD1</td></tr>
<tr><td>Logout</td></tr>
<tr><td>Log_ in_U1B1</td></tr>
<tr><td>Search&OpenApp</td></tr>
<tr><td>FormEND</td></tr>
<tr><td>Controllo END</td></tr>
<tr><td>Logout</td></tr>
<tr><td>Log_ in_U1A1</td></tr>
<tr><td>Beginning_of_CF5.0_Features_TC6</td></tr>
<tr><td>Form1</td></tr>
<tr><td>contrD1</td></tr>
<tr><td>Logout</td></tr>
<tr><td>Log_ in_U1B1</td></tr>
<tr><td>Search&OpenApp</td></tr>
<tr><td>Controllo END</td></tr>
<tr><td>Logout</td></tr>
<tr><td>Log_ in_U1A1</td></tr>
<tr><td>Beginning_of_CF5.0_Features_TC7</td></tr>
<tr><td>Form1</td></tr>
<tr><td>Controllo DeadLetter</td></tr>
<tr><td>Logout</td></tr>
<tr><td>Set_Roles_Dispatch_Decimal</td></tr>
<tr><td>Logout_BAC</td></tr>
</tbody></table>
</body>
</html>
Try to change like this ..
firstStr = "<?xml version" 'my file always starts like this
Do until objInputFile.AtEndOfStream
strToAdd = "<tr><td>Beginning_of_DD_TC" & CStr(index) & "</td></tr>"
substrToFind = "<tr><td><a href=" & chr(34) & "../Test case " & trim(cstr((index)))
tmpStr = objInputFile.ReadLine
If InStr(tmpStr, substrToFind) <= 0 Then
If Instr(tmpStr, firstStr) > 0 Then
text = tmpStr 'to avoid the first empty line
Else
text = text & vbCrLf & tmpStr
End If
Else
text = text & vbCrLf & strToAdd & vbCrLf & tmpStr
End If
index = index + 1
Loop
I'm looking for an ASP script that can parse a referral string from a search engine and extract just the keywords from it. I know this can be done using PHP, but I happen to working on a website that uses ASP.
Can this be done using ASP? Or, would it be better to find a Javascript/jQuery version?
Yes, it can be done in Classic ASP. E.g.:
<%
Dim i, url, querystring, keywords
url = Request.ServerVariables("HTTP_REFERER")
If url <> "" Then
Response.Write "Referrer: "
Response.Write url
Response.Write "<br>"
querystring = Request.ServerVariables("QUERY_STRING")
If querystring <> "" Then
keywords = Split(querystring, "&")
If IsArray(keywords) Then
For i = 0 To UBound(keywords)
Response.Write keywords(i)
Response.Write "<br>"
Next
End If
End If
End If
%>
varitem = match.SubMatches(1)
varitemthis = p_variables_list.Item(varitem)
result of
response.write match.Value &" "& varitemthis &" "&varitem & "<br>"
is
{{name}} rrrr name
{{site_root}} www.site.com/ site_root
{{mail}} sdddddsssdfffrrrsdf#ssrsssr.com mail
{{code}} code
result of
p_template = Replace(p_template, match.Value, varitem)
response.write p_template
all right, but
p_template = Replace(p_template, match.Value, varitemthis)
response.write p_template
nothing... why? what's wrong?
A couple of things...
Remember that the Replace function is case sensitive
Do some basic trouble-shooting by making sure you have values.
Response.Write "Original p_template = " & p_template
Response.Write "match.Value = " & match.Value
Response.Write "varitemthis = " & varitemthis
p_template = Replace(p_template, match.Value, varitemthis)
Response.Write "New p_template = " & p_template
In Classic ASP the Replace function is not case sensitive.