vbscript text file search a line with multiple strings - scripting

Need small modification in the below vbscript...
Const ForReading = 1
Dim strSearchFor, set1, set2
strSearchFor = "10/17/2012"
set1= app1
set2 =app2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("mylogfile.log", ForReading)
do until objTextFile.AtEndOfStream
strLine = objTextFile.ReadLine()
If InStr(strLine, strSearchFor and set1 or set2) <> 0 then
Wscript.Echo "we found current date with app1 or current date with app2"
Else
Wscript.Echo "We did not found current date"
End If
loop
objTextFile.Close
My actual mylogfile.log sample file has below in the text file.
working on 10/17/2012 starting something ending
closing on started app1
working on 10/17/2012 starting something app1
working on 10/17/2012 starting something app2
closing on 10/17/2012 starting something ending
Thanks.. in advance for your valuable time ..

If InStr(strLine, strSearchFor and set1 or set2) <> 0 then
It doesn't work this way. You can only search for one substring for each Instr call.
If InStr(strLine, strSearchFor) <> 0 and (InStr(strLine, set1) or InStr(strLine, set2)) then
Also, there's a problem with your loop, your script will echo for each line where you can't satisfy the condition. Use a flag or something similar to keep track whether the line was found and display the error at the end.

Related

How can I remove rows starting with ' in a variant?

Following the answer to this question, I execute different queries stored in a .txt file using the following code:
Dim vSql As Variant
Dim vSqls As Variant
Dim strSql As String
Dim intF As Integer
intF = FreeFile()
Open "MyFile.txt" For Input As #intF
strSql = Input(LOF(intF), #intF)
Close intF
vSql = Split(strSql, ";")
On Error Resume Next
For Each vSqls In vSql
DoCmd.RunSQL vSqls
Next
The queries are separated by ;, and each query is preceded by a comment line, starting with ' and ending with ;(so that it gets split by vSql = Split(strSql, ";") ).
The resulting vSql variant is composed of valid SQL statements, interspersed with comment lines starting with '. The current code works, but I would like to remove the On Error Resume Next so that a faulty SQL statement returns an error instead of being ignored.
How can I remove the rows starting with ' in the variant before executing the For Each loop? Or is there another way to accomplish my goal? The comment character can be changed if necessary.
Alternatively, this should do it to:
For Each vSqls In vSql
If Not CStr(vSqls) Like "'*" Then DoCmd.RunSQL vSqls
Next
It depends on exactly how the file is stored, but the simplest way is to find the first return character and take the rest of the text.
vSqls=mid(vSqls = Mid(vSqls, InStr(vSqls, vbCrLf) + 2)
or
vSqls=mid(vSqls = Mid(vSqls, InStr(vSqls, vbCr) + 1)
Bear in mind that this will only remove the first line so if there are blanks you will need to check whether the next line starts with a '.
And you might find that your queries don't work if they are spread across more than one line that you might need to deal with that by replacing any remaining carriage returns with a space.

VBA deleting name throws object required

I just want to delete a few names that get created every time a querytable gets created. They are all in 3 sheets starting with 0048,0114,0715, so I would just delete all names that start with any of them. However, I get the rejection "object required" in the if clause when I use rName.Delete. Without this, the code runs fine and prints all the names. Also, if I do range(rName).delete it would delete the ranges in the workbook (not what I want, though).
Sub delNames()
Dim strStartString(0 To 2) As String
strStartString(0) = "'0048'!mta"
strStartString(1) = "'0114'!mta"
strStartString(2) = "'0715!'mta"
For Each rName In ActiveWorkbook.Names
For Each ss In strStartString
If rName.Name Like ss & "*" Then
Debug.Print rName.Name
rName.Delete
End If
Next ss
Next rName
End Sub
Any idea what I am doing wrong here?
Posting Tim's comment as solution
Modifying a collection while looping through it can cause problems. Try using a for next loop counting back through the names in reverse - For x = ActiveWorkbook.Names.Count to 1 Step -1 : ActiveWorkbook.Names(x).Delete – Tim Williams 49 mins ago

Split text File into Access DB using SQL

All,
This is an example of three lines in my text File NEW_SCANNING.txt:
I;05/29/2013;06:55:37;3124480071200;1;801;1;;1
I;05/29/2013;06:56:05;0049004004827;1;801;1;;1
I;05/29/2013;06:56:09;54491069;1;801;1;;1
I want to be able to select what's between the 3rd and 4th " ; ", in this case it would be
3124480071200
0049004004827
54491069
So what I need the program to do is to search every line that starts with the letter I and select what's between the 3rd and 4th ;.
Then it has to put the first selection in a combobox named SelBarc. If it has done this for the first, it has to move on to the second and so on and so on ...
Anyone who can help me?
Open, read, split to lines then loop;
dim lines() as string,i as long
Open "c:\xxx\NEW_SCANNING.txt" for input as #1
lines = split(Input$(lof(1), #1), vbcrlf)
close #1
For i = 0 to ubound(lines)
if Left$(lines(i), 1) = "I" then combobox.additem split(lines(i), ";")(3)
Next

VBS Add numbers for each loop

For my VBS script, for each loop, I want it to add the number of which loop it's on. Like if it's a site and has an ID, then if it's on loop 1 adds 1 to the ID. I already declared "site" for my script.
What do I do? Do you need to see some of the script?
Not sure what kind of collection you're working with, but you could do something simple like this
Set objFilesystem = CreateObject("Scripting.FileSystemObject")
Set objInputFolder = objFileSystem.GetFolder("c:\temp")
i = 1
For Each objInputFile In objInputFolder.Files
WScript.Echo SITE + i
i = i + 1
Next

VBA count emails module!

This is a follow-up question of one which I posted yesterday. I feel I am getting quite close to creating a module in VBA that will count the number of e-mails sent on a particular day of the week. For the moment the day chosen is Monday.
However, the code is not yet working, and Outlook refuses to see the particular module.
I am sure there are a couple of errors in it. If someone could point these out, I would greatly appreciate it.
I also think that such code could be useful for others for future reference as the code for this kind of module does not seem to be readily available on the internet (I've looked!) and yet forms a type of search parameter that many will find useful!
Sub Count2(Optional dteDate As Date)
Dim objOutlook As Object, objnSpace As Object, objFolder As Object
Dim EmailCount As Integer
Set objOutlook = CreateObject("Outlook.Application")
Set objnSpace = objOutlook.GetNamespace("MAPI")
On Error Resume Next
Set objFolder = objnSpace.Folders("My Personal Emails").Folders("spam")
If Err.Number <> 0 Then
Err.Clear
MsgBox "No such folder."
Exit Sub
End If
Select Case Weekday(dteDate)
Case vbMonday
dteDate = Date
End Select
For Each MapiItem In MapiFolderInbox.Messages
If MapiItem.TimeReceived = Date Then
Count = Count + 1
Next MapiItem
End If
EmailCount = objFolder.Items.Count
Set objFolder = Nothing
Set objnSpace = Nothing
Set objOutlook = Nothing
MsgBox "Number of emails in the folder: " _
& EmailCount, , "Number of spam messages sent on a Monday: " & Count
End Sub
Whilst debugging drop the on error resume next.
It hides the errors. You can put it back later if you have to.
It's not a great idea to ignore errors, better to handle the errors explicitly.
One thing that struck me:
For Each MapiItem In MapiFolderInbox.Messages
If MapiItem.TimeReceived = Date Then
Count = Count + 1
Next MapiItem
End If
Should be
For Each MapiItem In MapiFolderInbox.Messages
If MapiItem.TimeReceived = Date Then
Count = Count + 1
End If
Next MapiItem
Other than that it looks OK to me.
i think time received is more of a timestamp where in addition to date it would have time too. you should probably use it like this;
For Each MapiItem In MapiFolderInbox.Messages
If MapiItem.TimeReceived > YTS And MapiItem.TimeReceived < TTS Then
Count = Count + 1
End If
Next MapiItem
where YTS and TTS are timestamps where it would have yesterday's timestamp and today timestamp
for eg 01:06:2011:23:59:00 and 02:06:2011:23:59:00
you should comfirm this by debuging your code. hope this helps.
Outlook only sees the module when the parameters are left empty... as opposed to containing (Optional dteDate As Date).
Other than that, following the suggestion by Johan, the Module runs, but only ever has Count = 1. That is, that the result of emails received on a Monday is always 1 regardless of the input.
I also tried adbanginwar's suggestion, but in this case a compile error of 'Expected: Then or GoTo' is displayed.