Sign in to follow this  
Followers 0
Zithen

ini read problem

8 posts in this topic

have a script that some of you helped with works great, but one small problem.

using ini read to see if a file has been read and if so skips that file, but for some reason it skips the first file every time with out fail, but will read the rest if there not in the ini file.

here is the code any light would be good

$search = FileFindFirstFile("*.txt") 
While 1
    $numfiles = $Numfiles +1
    $filename = FileFindNextFile($search) 
    If @error Then ExitLoop
        
       $file = FileOpen($filename, 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

$namearray= _arraycreate("")
$matcharray = _arraycreate("")
$matches = 0
$result = FileOpen("C:\Documents and Settings\grifgd\Desktop\logs\result.csv", 1)
$var = IniRead($rootpath & "asset.ini","Asset Tag", $filename, "0")
If $var == 0 Then

; Read in lines of text until the EOF is reached

$asset = StringTrimRight($filename, 4)
$ip = FileReadLine($file, 12)
$ip2 = StringTrimLeft($ip, 37)
$username = FileReadLine($file, 22)
$username2 = StringTrimLeft($username, 12)
$read = $read + 1
FileWriteLine($result, $asset & "," & $ip2 & "," & $username2)
IniWrite($rootpath & "asset.ini","Asset Tag", $filename, "1")



Else
    $skip = $skip + 1
    EndIf



FileClose($file)

FileClose($result)
$rootpath= "C:\Documents and Settings\grifgd\Desktop\logs\"
$fn = $rootpath & $filename
$movepath = $rootpath & "Read\*.txt"
Filemove($fn, $movepath,1)
WEnd

PS i know the code looks like crap, but oh well lol

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I'm about to post my own question on .ini in a second, today is my first reading on it.

But a quick glance at yours:

$var = IniRead($rootpath & "asset.ini","Asset Tag", $filename, "0")

the $filname should = "Key" name you set I believe. What ever you've decided to make "Key" in your .ini.

Example:

[section] = [Asset Tag] the way you have it set up

and

["Key"] = ?? the way you have it

So ini should look like:

[Asset Tag]

Key=

Replace $filname with "Key" in your IniRead (this was an edit)

Anyway that's the way I'm reading the .ini help today... hope it helps

Edited by ronsrules

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

using ini read to see if a file has been read and if so skips that file, but for some reason it skips the first file every time with out fail, but will read the rest if there not in the ini file.

$var = IniRead($rootpath & "asset.ini","Asset Tag", $filename, "0")

how is $rootpath defined before this command? If you posted the whole script, it is not defined at all. That's why IniRead fails. As far as I can see you define $rootpath at the end of the While loop. Define it at the beginning (or even better outside) and your script will work!

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

your trying to read an ini file

with find next file

this will not work

You have combined two separate file searches we worked on. One was to find files and put them in a list. The second was to read the list

(if i remember correctly)

your script is really a mess right now

8(

Edited by Valuater

NEWHeader1.png

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

your trying to read an ini file. with find next file this will not work

You have combined two separate file searches we worked on. One was to find files and put them in a list. The second was to read the list (if i remember correctly)

??? Did you read the script he posted ???

He lists all *.txt files. Then he reads asset.ini to check if the current file has already been read. If not, he reads in the current file and writes the content to result.csv. Then he updates asset.ini to mark the file as read. Finally he moves the processed file into another location.

As I guessed, the problem is that $rootpath is not set when he first calls IniRead(), thus IniRead("" & "asset.ini","Asset Tag", $filename, "0") will return 0, as asset.ini is not found. Which in turn leads to the effect, that the frist file is skipped. After the first round, $rootpath gets set (at the end of the while loop) and the script works.

EDIT: Added last sentence

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

sorry took so long to reply didnt have the script at home..

moved the root path to the top and still skiping the first file

heres the changed code..tried a few other things but still nothing worked

#include <Date.au3>
#include <Array.au3>

; Shows the filenames of all files in the current directory
;DriveMapAdd("v:", "\\dcnvitm03\apps")
$rootpath= "C:\Documents and Settings\grifgd\Desktop\logs"
$linesread = 0
$begin = TimerInit()
$numfiles = 0
$read = 0
$skip = 0

$search = FileFindFirstFile("*.txt")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $numfiles = $Numfiles +1
    $filename = FileFindNextFile($search) 
    If @error Then ExitLoop
        
       $file = FileOpen($filename, 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf



$namearray= _arraycreate("")
$matcharray = _arraycreate("")
$matches = 0
$result = FileOpen("C:\Documents and Settings\grifgd\Desktop\logs\result.csv", 1)
$var = IniRead($rootpath & "asset.ini","Asset Tag", $filename, "0")
If $var == 0 Then

;fileopen res
;$result = FileOpen("C:\Documents and Settings\grifgd\Desktop\logs\result.csv", 1)

; Read in lines of text until the EOF is reached

$asset = StringTrimRight($filename, 4)
$ip = FileReadLine($file, 12)
$ip2 = StringTrimLeft($ip, 37)
$username = FileReadLine($file, 22)
$username2 = StringTrimLeft($username, 12)
$read = $read + 1
FileWriteLine($result, $asset & "," & $ip2 & "," & $username2)
IniWrite($rootpath & "asset.ini","Asset Tag", $filename, "1")



Else
    $skip = $skip + 1
    EndIf



FileClose($file)

FileClose($result)
$rootpath= "C:\Documents and Settings\grifgd\Desktop\logs\"
$fn = $rootpath & $filename
$movepath = $rootpath & "Read\*.txt"
Filemove($fn, $movepath,1)
WEnd

MsgBox(4096,"Results", "Files Read:" & $read & @CR & "Files Skiped:" & $skip)

Share this post


Link to post
Share on other sites

sorry took so long to reply didnt have the script at home..

moved the root path to the top and still skiping the first file

heres the changed code..tried a few other things but still nothing worked

Change definition of $rootpath or IniRead concatenation of strings. There is no path: C:\Documents and Settings\grifgd\Desktop\logsasset.ini

Change $rootpath to:

$rootpath= "C:\Documents and Settings\grifgd\Desktop\logs\" ; !! TRAILING \

OR change IniRead to:

$var = IniRead($rootpath & "\" & "asset.ini","Asset Tag", $filename, "0")

Cheers

Kurt


__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

doh...**slaps head** cant belived i didnt see that, grr oh well thanks a lot

Share this post


Link to post
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
Sign in to follow this  
Followers 0