Struggling

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
edauthier
Pro Scripter
Posts: 84
Joined: Sun Apr 13, 2003 1:26 pm
Location: USA

Struggling

Post by edauthier » Fri Oct 24, 2003 6:17 pm

Hate to ask when I haven't much assisted anyone here... but, I am trying to write vbscript to read a text file, get to the last line and make sure that the last character is a " , and if not then do something..

I have code to read thru the file and display the last line in a message>, but I want it to do the check for me and if problem then notify me..
:oops:

Thanks a lot.. Ed

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Fri Oct 24, 2003 11:57 pm

I have code to read thru the file and display the last line in a message>, but I want it to do the check for me and if problem then notify me..
:idea: Do the following on the last line:
Check the length
Use MidStr starting at length-1....something like this:
//Using Input> to test. Normally reading Str from a variable
Input>Str,Enter a line to check
Length>%Str%,Last
Sub>Last,1
MidStr>%Str%,%Last%,1,Last1
If>%Last1%=",DoThis,DoSomethingElse

Label>DoThis
MessageModal>Last character is a "
Goto>End

Label>DoSomethingElse
MessageModal>Last character is not a ", It is %Last1%.

Label>End
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

edauthier
Pro Scripter
Posts: 84
Joined: Sun Apr 13, 2003 1:26 pm
Location: USA

Thanks Bob

Post by edauthier » Thu Oct 30, 2003 7:49 pm

I guess I should've noted that I've used the incremental count in MS and it's just too slow for the amount of data lines I'm counting.

I was looking for a VB_Script way top solve the problems I'm having. As always I am appreciative for your assistance..

Thanks Again, Ed

Lumumba

Post by Lumumba » Thu Oct 30, 2003 9:29 pm

If there's something common for all lines you could "search" for it to count the lines. Let's assume it's the char "."

MacroScheduler & DOS

Let>COMSPEC=C:\WINNT\System32\cmd.exe
Let>SourceFile=C:\MyFile.txt
Let>ResultFile=C:\MyFileResult.txt

Run Program>%COMSPEC% /C find /C "." %SourceFile% > %ResultFile%

ReadLN>%ResultFile%,3,NumberOfLines
Sub>NumberOfLines,1
ReadLn>%SourceFile%,%NumberOfLines%,Str
.
.
.

Macro Scheduler

Let>ln=0

Label>CountLines
Add>ln,1
ReadLN>%SourceFile%,%ln%,line
If>line##EOF##,CountLines

Sub>ln,1
ReadLn>%SourceFile%,%ln%,Str
.
.
.

:arrow: both followed by Bob's validation

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Fri Oct 31, 2003 3:22 am

I just developed this to give an almost instant line count using the old DOS command FILE with the /c /v switches and redirecting the output to a file where the line count can be read from.

:idea:
//This routine is to count all lines in a file.
//It uses FIND command with output redirected to a temporary file
//It uses /c and /v switches to count lines with a non-existent string.
//User is prompted for full path and filename of file to have lines counted.
//Default value for non-existent string is "qqqxxx", which can be changed.

Label>UserVariables
//Source is the file path\name to have the lines counted
Input>Source,Enter FULL Path and Filename to be counted%CRLF%Format is X:\Path\Filename.ext
//Seek is set to a non-existent string
Input>Seek,Accept default or change the unique value.%CRLF% Wanted is a value that will NOT be in the file,qqqxxx

Label>MacroVariables
Let>RP_WAIT=1
Let>RP_WINDOWMODE=0
/CountFile is the file name to hold the results of the line count
Let>CountFile=%TEMP_DIR%lines.txt
//Line count value is at position equal to length of Full Source path and file name, plus fourteen.
Len>%Source%,FileNameLength
Let>Position=%FileNameLength%+14

Label>Start
//Remove old file that might exist
DeleteFile>%CountFile%
Run Program>command.com /c find /c /v "%Seek%" %Source%>>%CountFile%

Label>DisplayCount
//Results of Find are on the second line
ReadLn>%CountFile%,2,Result
MidStr>%Result%,%Position%,5,Result2
MessageModal>There are %Result2% lines in this file

Label>End
Notes:
1. I believe that you need to use cmd vs. command.com on NT, 2000, XP systems.
2. Be sure to include the quotes around %Seek% on the Run Program line.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Fri Oct 31, 2003 3:30 am

Aha, I see that Lumumba made a similar routine while I was working on mine.

His is much shorter. :D Mine is longer because of comments, and ability to modify on the fly, but we both took the same approach. :D

:idea: Where my version differs is by including the /v switch which counts lines that DO NOT MATCH the string. By using "qqqxxx" we force it to count every line.

You could add /v and "qqqxxx" or some other string to his solution.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

edauthier
Pro Scripter
Posts: 84
Joined: Sun Apr 13, 2003 1:26 pm
Location: USA

Well that solves it.. Both sets of code are excellent

Post by edauthier » Fri Oct 31, 2003 4:31 pm

I wanted to share some VB_scripts I was working with to resolve my problems:

'This will look at all services running and report them in a text file denoted in the script.. 2000/xp

************************************************************
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
("c:\service_status.txt", ForAppending, True)
Set colServices = GetObject("winmgmts:").ExecQuery _
("Select * from Win32_Service")
For Each objService in colServices
objTextFile.WriteLine(objService.DisplayName & vbTab & _
objService.State)
Next
objTextFile.Close
***********************************************************

'current machine IP address
********************************************************
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set IPConfigSet = objWMIService.ExecQuery _
("Select IPAddress from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each IPConfig in IPConfigSet
If Not IsNull(IPConfig.IPAddress) Then
For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
WScript.Echo IPConfig.IPAddress(i)
Next
End If
Next
*************************************************************
'counts lines in the 'named file'. Appears in on screen window.

const filename="x:\your file\wildcard.log"
set fso=createobject("scripting.filesystemobject")
set ofile=fso.opentextfile(filename)
strcontents=ofile.readall
ofile.close
set ofile=nothing
set fso=nothing
acontents=split(strcontents,vbcrlf)
wscript.echo "LOCATION : " & cstr(ubound(acontents)+1)

*************************************************************

Thanks for all the help!

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Fri Oct 31, 2003 8:32 pm

Hello Edauthier.....

:!: You first wrote:
when I haven't much assisted anyone here


Can't say that anymore! :D

Thanks for the code, I am sure this will be helpful to me.

I appreciate the work,
Bob Hansen

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts