Jump to content

FileWriteLine


Recommended Posts

i'm new at this and I'm writing a script that reads from a txt file and find the line that has string "Not Found" in it then verify some info then write back to that line but replacing "Not Found" with "Found" or delete that entire line depends on the verification returns value.

The problem I have is I can't write back using FileWriteLine even when I know the line number or delete that line

any idea?

thank you!

Link to comment
Share on other sites

i'm new at this and I'm writing a script that reads from a txt file and find the line that has string "Not Found" in it then verify some info then write back to that line but replacing "Not Found" with "Found" or delete that entire line depends on the verification returns value.

The problem I have is I can't write back using FileWriteLine even when I know the line number or delete that line

any idea?

thank you!

<{POST_SNAPBACK}>

sounds to me like you're trying to open file for read and write access....whick autoit doesn't like....what i would do is to open the source file for read, and open another output file to send corrected text to. after you are done with source file, can close both, delete the source and rename the correction.

Link to comment
Share on other sites

In AutoIT, it is much easier to just read both files into memory, set them into an array, and then work with that, when you are done, write the array back into a file, or overwrite the old one.

Quick code from memory...

$file1=_getfile("c:\myfile1.txt")
$file1array=StringReplace($file1,@lf,"")
$file1array=StringSplit($file1array,@cr)

for $i=1 to $file1array[0]
msgbox(1,"Line " & $i,$file1array[$i])
next

func _getfile($_filename)
  $_fullfile=fileread($_filename,filegetsize($_filename))
return $_fullfile
endfunc

edit, Larry was quicker. There are quite a few UDFs to do stuff like this as well, and Larry writes good ones. :)

Simple enough to make your own for this application, and I find leaving the file open and read/writes tends to be slower.

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

Thanks all, but that's not what i'm trying to do but I found a way..(but haven't test it yet ) :)

1. read txt file line by line

2. write to temp file line by line if "not found" is not in that line

3. if found "Not found" that line - do a qualifier then update the linevar read earlier has "not found' with either "found" or "not Require" then write it to the temp file (keep the exact line only replace "Not Found" with something else)

4. Move on to read for the next line.

sample of text file:

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

Machine Name Product Bulletin Q Number Reason Status

PERSONAL-W36OO3 (172.20.5.91) WINDOWS XP PROFESSIONAL SP2 MS04-038 834707 File version is less than expected. [C:\WINDOWS\system32\browseui.dll, 6.0.2900.2180 < 6.0.2900.2518] NOT Found

PERSONAL-W36OO3 (172.20.5.91) WINDOWS XP PROFESSIONAL SP2 MS04-041 885836 A required registry key does not exist. It is necessary in order for this patch to be considered installed. [sOFTWARE\Microsoft\Windows NT\CurrentVersion\Hotfix\KB885836\Installed] NOT Found

PERSONAL-W36OO3 (172.20.5.91) WINDOWS XP PROFESSIONAL SP2 MS04-043 873339 File version is less than expected. [C:\WINDOWS\system32\hypertrm.dll, 5.1.2600.2180 < 5.1.2600.2563] NOT Found

PERSONAL-W36OO3 (172.20.5.91) WINDOWS XP PROFESSIONAL SP2 MS04-044 885835 File version is less than expected. [C:\WINDOWS\system32\lsasrv.dll, 5.1.2600.2180 < 5.1.2600.2525] NOT Found

PERSONAL-W36OO3 (172.20.5.91) WINDOWS XP PROFESSIONAL SP2 MS05-001 890175 File version is less than expected. [C:\WINDOWS\system32\hhctrl.ocx, 5.2.3790.1194 < 5.2.3790.1280] NOT Found

Here is my script:

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

AutoItSetOption ( "TrayIconHide",1)

Global $EVPscanIni = 'EVPscan.ini'

Global $MBSAResults = 'Results.txt'

Global $EVResults = 'EVResults.txt'

Global $FileReadLnVar = ''

;//=====================================================================//

;//=====================================================================//

;//=====================================================================//

; Read results.txt line by line for "NOT FOUND"

$TotalLine = FileCountLines($MBSAResults)

$LineCt = 1

$FH = FileOpen($MBSAResults, 0)

If $FH = -1 Then ; Check if file opened for reading OK

Exit

EndIf

do ; read file until end of file (totallines)

$FileReadLnVar = FileReadLine($MBSAResults, $LineCt)

If @error = -1 Then

ExitLoop ; end of file

If StringInstr ($FileReadLnVar, 'NOT Found') then

Global $ARTNumber = StringMid($FileReadLnVar,StringInStr($FileReadLnVar, 'MS'),8)

Global $ARTtype = Iniread ($EVPscanIni, $ARTNumber, 'TYPE','NOTFOUND')

if $ARTtype = 'NotFound' Then

FileWriteLine($EVResults, $FileReadLnVar)

else

select

case $ARTType = 'IE' or $ARTType = 'OFFICE'

App_Patch($ARTNumber, $ARTType)

case $ARTType = 'OS'

OS_PATCH($ARTNumber, $ARTType)

endselect

EndIf

else

FileWriteLine($EVResults, $FileReadLnVar)

endif

$LineCt = $LineCt + 1

Until $cline = $totalLine

FileClose($MBSAResults)

Exit (0)

;//=====================================================================//

;//=====================================================================//

;//=====================================================================//

Func FileCountLines($FileName) ; Count total lines in results.txt

Local $N = FileGetSize($FileName) - 1

If @error Or $N = -1 Then Return 0

Return StringLen(StringAddCR(FileRead($FileName, $N))) - $N + 1

EndFunc

;//=====================================================================//

Func App_Patch($ARTNumber, $ARTType)

$ReqTypeVer = IniRead($EVPscanIni,$ARTNumber, 'TypeVersion','Notfound')

$xReqTypeVer = Stringsplit($ReqTypeVer, ';')

$x = 1

;//----- Type Version(s) loop starts ----------------------------------//

for $x = 1 to $xReqTypeVer[0] step 1

; Check Type SP level if applicable

$ReqTypeSP = IniRead($EVPscanIni, $ARTNumber&'-'&$xReqTypeVer[$x], 'TypeSP','Notfound')

$xReqTypeSP = Stringsplit($ReqTypeSP, ';')

$SysFileVer = GetFileVer($xReqTypeSP[1])

if CmbFileVer($SysFileVer) >= CmbFileVer($xRegTypeSP[2]) then

if CmbFileVer($SysFileVer) < CmbFileVer($xReqTypeSP[3]) then

; Check UpdatedFiles

$fTotal = IniRead($EVPscanIni, $ARTNumber&'-'&$xReqTypeVer[$x], 'Count','Notfound')

;//------ File(s) Check loop starts -----------------------------------//

for $fileCt = 1 to $fTotal step 1

$FileVar = StringSplit(iniRead($EVPscanIni, $ARTNumber&'-'&$xReqTypeVer[$y], $count,'NF'),';')

$Filecheck = CmpFileVer($count,$fileVar[1], $Filevar[2])

If $Filecheck = 1 then

FileWriteLine($EVResults, $FileReadLnVar)

Return()

endif

Next

UpdateResults('Found')

;//----- File(s) Check loop ends --------------------------------------//

else

; call update value

UpdateResults('Patch Not Require')

Return()

EndIf

else

; call update value

UpdateResults('Patch Not Require')

Return()

Endif

next

;//----- Type Version(s) loop ends ----------------------------------//

EndFunc

;//=====================================================================//

FUNC OS_PATCH($ARTNumber, $ARTType)

$SysSPVer = StringRight (@OSservicepack, 1)

if $sysSPver = '' then

$sysSPver = 0

endif

$ReqTypeSP = IniRead($EVPscan.ini, $ARTNumber&'-'@OSbuild, 'TypeSP', 'NotFound')

$xReqTypeSP = Stringsplit($RegTypeSP, ';')

if $SysSPVer >= $xReqTypeSP[1] Then

if $SysSPVer < $xReqTypeSP[2] then

$fTotal = IniRead ($EVPatchIni, $ARTNumber&'-'@OSbuild, 'count', 'NOTFOUND')

;//------ File(s) Check loop starts -----------------------------------//

for $fileCt = 1 to $fTotal step 1

$FileVar = StringSplit(iniRead($EVPscanIni, $ARTNumber&'-'&$xReqTypeVer[$y], $count,'NF'),';')

$Filecheck = CmpFileVer($count,$fileVar[1], $Filevar[2])

If $Filecheck = 1 then

FileWriteLine($EVResults, $FileReadLnVar)

Return()

endif

Next

UpdateResults('Found')

;//----- File(s) Check loop ends --------------------------------------//

else

UpdateResults('Patch Not Require')

Return()

endif

else

UpdateResults('Patch Not Require')

Return()

endif

endfunc

;//=====================================================================//

Func CmpFileVer($count, $fileName, $FileVersion)

$SysFileVersion = Get_File_ver($fileName)

if CmbFileVer($SysFileVersion) >= CmbFileVer($Fileversion) then

IniWrite('EVPatchDebug.ini', 'INFO', 'SystemFile'&$count, $fileName&';'&$SysFileVersion)

msgbox(0,'','match')

return (0)

endif

if CmbFileVer($SysFileVersion) < CmbFileVer($Fileversion) then

IniWrite('EVPatchDebug.ini', 'INFO', 'SystemFile'&$count, $fileName&';'&$SysFileVersion)

msgbox (0,'','less')

return (1)

Endif

endfunc

;//=====================================================================//

Func GetFileVer($x)

$i = Stringsplit($x,'%') ; splitting PathVar '%' - [2] is envvar - [3] path and file

$p = EnvGet($i[2]) ; get environment path

$FileName = ($p&$i[3]) ; combine path and file name

$FileVer = FileGetVersion($FileName) ; get version for file

return($FileVer) ; return version #

endfunc

;//=====================================================================//

Func CmbFileVer($ver)

$v = StringReplace ($ver,'.','','3') ; combine version(0.0.0.0) to (0000)

return($v)

endfunc

;//=====================================================================//

Func UpdateResults($value)

$N = StringInStr($FileReadLnVar, 'File')

$L1 = Stringleft ($FileReadLnVar, $N)

$NewReadVar = ($FileReadLnVar&@tab&$value)

FileWriteLine($EVResults, $NewReadVar)

return()

EndFunc

Edited by autoITnewbie
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...