Jump to content

Read file, format file, append file to new file, & execute VBS


Recommended Posts

Hi all,

I am newbie to AutoIt and I need your help in writing script. This is what the script should do:

1. Script reads a text file one line at a time.

2. Script will trim left 22 characters from each line.

3. Script will add " (quote sign) at the beginning and at the end of each line.

4. Script will continue loop until the EOF reached.

5. Script will append the formatted text file to VBS script.

So, we start with something like this:

[2006-01-05 23:04:48] Reset host:10.2.120.60

[2006-01-05 23:04:48] host is down:10.2.120.60

and end with something like this:

"Reset host:10.2.120.60"

"host is down:10.2.120.60"

I have problem with adding " (quote sign) at the beginning and at the end of the line. This is what I have so far:

$File = Fileopen ("c:\messages.txt", 0)

$FileOut = FileOpen ("C:\messages1.VBS",1)

While 1

$line = FileReadLine($file)

If @error = -1 Then ExitLoop

$result = StringTrimLeft($Line, 22)

If StringinStr ($Line, "S") > 0 then FileWriteLine ($FileOut, $result)

Run(@ComSpec & " /c " & 'wscript C:\messages1.VBS', "", @SW_HIDE)

Wend

FileClose ($File)

FileClose ($FileOut)

Any help/suggestion is highly appreciated.

Thanks.

Link to comment
Share on other sites

maybe

$addstuff = "WHAT YOU WANT TO ADD"
$File = Fileopen ("c:\messages.txt", 0)
$FileOut = FileOpen ("C:\messages1.VBS",1)
While 1
$line = FileReadLine($file)
If @error = -1 Then ExitLoop
$result = StringTrimLeft($Line, 22)
$result = '"' & $addstuff & $result & '"'
If StringinStr ($Line, "S") > 0 then FileWriteLine ($FileOut, $result); not in your list

Wend

FileClose ($File)
FileClose ($FileOut)

; i had to turn this off
;Run(@ComSpec & " /c " & 'wscript C:\messages1.VBS', "", @SW_HIDE)

8)

NEWHeader1.png

Link to comment
Share on other sites

Hi all,

I am newbie to AutoIt and I need your help in writing script. This is what the script should do:

1. Script reads a text file one line at a time.

2. Script will trim left 22 characters from each line.

3. Script will add " (quote sign) at the beginning and at the end of each line.

4. Script will continue loop until the EOF reached.

5. Script will append the formatted text file to VBS script.

So, we start with something like this:

[2006-01-05 23:04:48] Reset host:10.2.120.60

[2006-01-05 23:04:48] host is down:10.2.120.60

and end with something like this:

"Reset host:10.2.120.60"

"host is down:10.2.120.60"

I have problem with adding " (quote sign) at the beginning and at the end of the line. This is what I have so far:

$File = Fileopen ("c:\messages.txt", 0)

$FileOut = FileOpen ("C:\messages1.VBS",1)

While 1

$line = FileReadLine($file)

If @error = -1 Then ExitLoop

$result = StringTrimLeft($Line, 22)

If StringinStr ($Line, "S") > 0 then FileWriteLine ($FileOut, $result)

Run(@ComSpec & " /c " & 'wscript C:\messages1.VBS', "", @SW_HIDE)

Wend

FileClose ($File)

FileClose ($FileOut)

Any help/suggestion is highly appreciated.

Thanks.

In AutoIt, you can use either single OR double quotes to delimit a string, and then use the other type of quote mark (that you DON'T use to delimit a string) for other stuff.

Try this:

$File = Fileopen ('c:\messages.txt', 0)
$FileOut = FileOpen ('C:\messages1.VBS',1)
While 1
$line = FileReadLine($file)
If @error = -1 Then ExitLoop
$result = StringTrimLeft($Line, 22)
If StringinStr ($Line, 'S') > 0 then FileWriteLine ($FileOut, '"' & $result & '"' )
FileClose ($File)
FileClose ($FileOut)
Run(@ComSpec & " /c " & 'wscript C:\messages1.VBS', "", @SW_HIDE)
Wend

I also moved the FileClose to before the RUN command. You may have problems if the VBS script is still Open (especially if you have it open for writing) when you go to run it (don't really know though).

Link to comment
Share on other sites

JerryD and Valuater, thanks for your quick responses. I made changes to my script according to your suggestions. However, I now get error:

Line 4 (File.au3)

$Line = FileReadLine($File)

Error: Invalid file handle used

Also, when I open the final file, the $result line jump one line. Is there anyway that I can make the $result line stay absolutely on line # 4? This is the script again:

$File = Fileopen ("c:\messages.txt", 0)

$FileOut = FileOpen ("C:\messages1.VBS",1)

While 1

$Line = FileReadLine($File)

If @error = -1 Then ExitLoop

$result = StringTrimLeft($Line, 22)

$result = '"' & $result & '"'

If StringinStr ($Line, "S") > 0 then FileWriteLine ($FileOut, $result)

FileClose ($File)

FileClose ($FileOut)

Run(@ComSpec & " /c " & 'wscript C:\messages1.VBS', "", @SW_HIDE)

Wend

Thanks.

Link to comment
Share on other sites

$File = Fileopen ("c:\messages.txt", 0)

$FileOut = FileOpen ("C:\messages1.VBS",1)

While 1

$Line = FileReadLine($File)

If @error = -1 Then ExitLoop

$result = StringTrimLeft($Line, 22)

$result = '"' & $result & '"'

If StringinStr ($Line, "S") > 0 then FileWriteLine ($FileOut, $result)

<<<<-------------------- ...should be here!!

FileClose ($File)

FileClose ($FileOut)

Run(@ComSpec & " /c " & 'wscript C:\messages1.VBS', "", @SW_HIDE)

Wend-------------------------------->>>>> this should ....

just my 2 cents:

instead of

Run(@ComSpec & " /c " & 'wscript C:\messages1.VBS', "", @SW_HIDE)

I prefer

run ( "wscript.exe C:\messages1.VBS")

Link to comment
Share on other sites

Sksbir, thanks for ur response. Ur suggestion won't work since the VBS script needs to be executed for each line ;) . This is what I'm trying to achieve:

1. I have an application that write the application error to text file.

2. Each different error will be written on each new line

3. I am writing VBS script to transfer the application error to windows event log

what the script nees to do is:

1. read the first error line

2. append the first error line to the VBS script

3. execute the VBS script to write the first error line to windows event log

4. read the second error line and perform the same process until no error line found

I am so close....yet so very far :lmao:

Thanks.

Link to comment
Share on other sites

Sksbir, thanks for ur response. Ur suggestion won't work since the VBS script needs to be executed for each line ;) . This is what I'm trying to achieve:

1. I have an application that write the application error to text file.

2. Each different error will be written on each new line

3. I am writing VBS script to transfer the application error to windows event log

what the script nees to do is:

1. read the first error line

2. append the first error line to the VBS script

3. execute the VBS script to write the first error line to windows event log

4. read the second error line and perform the same process until no error line found

I am so close....yet so very far :lmao:

Thanks.

If you vbs needs to be executed for each line, you have to include

[fileopen,fileclose,execute] sequence for each occurence in the loop.

$File = Fileopen ("c:\messages.txt", 0)
While 1
    $Line = FileReadLine($File)
    If @error = -1 Then ExitLoop
    $FileOut = FileOpen ("C:\messages1.VBS",1)
    $result = StringTrimLeft($Line, 22)
    $result = '"' & $result & '"'
    If StringinStr ($Line, "S") > 0 then FileWriteLine ($FileOut, $result)
    FileClose ($FileOut)
    Run('wscript.exe C:\messages1.VBS')
Wend
FileClose ($File)

But a better way should be to construct one VBS script executing the whole actions, in order to avoid multiple vbs calls.

May you give here an example of vbs file ? I don't understand why you open VBS in append mode : it should be 2 = Write mode (erase previous contents) in order to execute a different vbs from one time to another.

Edited by sksbir
Link to comment
Share on other sites

Sksbir, thanks for your continuos help. This is the VB script that will transfer the error log to windows event log. The application log need to be appended to this VB script.

Const EVENT_SUCCESS = 1

Set objShell = Wscript.CreateObject("Wscript.Shell")

objShell.LogEvent EVENT_SUCCESS, _

"Store No:032 Connection is downed" <<<<<<<<<<<< from application error log

The application log looks like this:

[2006-01-06 09:09:54] Store No:032 Connection is downed

[2006-01-04 19:17:16] Store No:658 IP:10.4.158.60 Problem on header

[2005-12-30 13:09:08] Store No:034 IP:10.2.34.60 Checksum incorrect

[2005-12-26 05:16:15] Store No:944 IP:10.151.20.60 Cannot found header

[2005-12-26 05:16:13] Store No:944 IP:10.151.20.60 invalid time format

[2005-12-31 21:10:08] Store No:051 IP:10.3.151.60 time different more than 3 mins

For above example, [2006-01-06 09:09:54] Store No:032 Connection is downed is formatted as "Store No:032 Connection is downed" and the file appended to VB script and VB script is executed to write the log to windows event log.

Thanks again for your help.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...