Jump to content

Help with IniRead and Multiple Files


Recommended Posts

Hello Everyone,

Much like most posts here start - I am new to AutoIt and struggling with a particular script I am attempting.

What I have done so far is create a script that copies a certain .ini file I need to a directory for me from multiple users machines. I than have a second script that reads the .ini file for the key I need inserts it into a text file.

CODE
$var = IniRead("C:\Documents and Settings\Desktop\New Folder\*.*", "OS", "OS", "NotFound")

WinActivate("Untitled - Notepad")

Send($var)

Send("{enter}")

The thing is I have many, many .ini files. Does anyone know of a way to set this script up to scan through ALL .ini files in a directory for the same key? Any help in the right direction is appreciated.

Link to comment
Share on other sites

CODE
$var = IniRead("C:\Documents and Settings\Desktop\New Folder\*.*", "OS", "OS", "NotFound")

Forgot to mention - I have "*.*" as the filename in the iniread in an attempt to get it to scan through all .ini files but it does not work for me. I am lost as to what to do.

Link to comment
Share on other sites

First off, Welcome to the forums! :party:

Okay here's a "Simi-Complex" script that will scan "$Path" and then "Detect" and read all the ".ini" files.. and store the values in "$Data" and at the end it will open notepad and set the values of the ".ini" files in the format of "1 value per line.." hehe..

So here it is :

#Include <File.Au3>
#Include <String.Au3>
Opt ('WinTitleMatchMode','2')
$Path = 'C:\Documents and Settings\Desktop\New Folder'
$List = _FileListToArray ($Path)
$Data = ''

For $A = '1' To $List['0']
$Current = $Path & '\' & $List[$A]
$Type = _FileGetType ($List[$A])
If StringInStr ($Type, '.ini') <> '0' Then 
$Read = IniRead ($Current, 'OS','OS','NotFound')
If $A = '1' Then 
$Data = $Read 
Else 
$Data = $Data & @LF & $Read
EndIf 
EndIf 
Next

ShellExecute ('Notepad')
Sleep ('500')
$Title = WinGetTitle ('Untitled - Notepad')
$Handle = WinGetHandle ($Title)
ControlSend ($Handle, '','', $Data)


Func _FileGetType ($iFile)
$String = _StringReverse ($iFile)
$String = StringSplit ($String, '.')
$String = '.' & _StringReverse ($String['1'])
Return $String 
EndFunc

Tell me if you have any problems and please feel free if you have a question. ^^

Hope it helps! :)

- John

Edit : I also tested this with a folder on my desktop with 6 ".ini" files and it read all the values correctly and avoided any files that were not ".ini"s...

Edited by John2006

Latest Projects :- New & Improved TCP Chat

Link to comment
Share on other sites

First off, Welcome to the forums! :party:

Okay here's a "Simi-Complex" script that will scan "$Path" and then "Detect" and read all the ".ini" files.. and store the values in "$Data" and at the end it will open notepad and set the values of the ".ini" files in the format of "1 value per line.." hehe..

So here it is :

#Include <File.Au3>
#Include <String.Au3>
Opt ('WinTitleMatchMode','2')
$Path = 'C:\Documents and Settings\Desktop\New Folder'
$List = _FileListToArray ($Path)
$Data = ''

For $A = '1' To $List['0']
$Current = $Path & '\' & $List[$A]
$Type = _FileGetType ($List[$A])
If StringInStr ($Type, '.ini') <> '0' Then 
$Read = IniRead ($Current, 'OS','OS','NotFound')
If $A = '1' Then 
$Data = $Read 
Else 
$Data = $Data & @LF & $Read
EndIf 
EndIf 
Next

ShellExecute ('Notepad')
Sleep ('500')
$Title = WinGetTitle ('Untitled - Notepad')
$Handle = WinGetHandle ($Title)
ControlSend ($Handle, '','', $Data)


Func _FileGetType ($iFile)
$String = _StringReverse ($iFile)
$String = StringSplit ($String, '.')
$String = '.' & _StringReverse ($String['1'])
Return $String 
EndFunc

Tell me if you have any problems and please feel free if you have a question. ^^

Hope it helps! :)

- John

Edit : I also tested this with a folder on my desktop with 6 ".ini" files and it read all the values correctly and avoided any files that were not ".ini"s...

I can't thank you enough! That worked perfectly.

I must say - I have been learning AutoIt simply by playing around and reading the forums. Everyone here rocks!

Link to comment
Share on other sites

That's how I learned :) now I can do almost anything in autoit... also do you got an msn?.. or yahoo if so add me!

msn : pk4fun@live.com

yahoo : pk4fun@yahoo.com

Thanks again for the help! I will add you and maybe we can chat about Autoit?

I have another question if you don't mind - how would I manipulate your script to pull two keys out of each of the ini files?

Link to comment
Share on other sites

I dont know exactly what sections and keys you're after, or what you want to do with them, but, here's an offering...

#Include <File.Au3>
$Path = @WindowsDir; directory containing .ini files

$Fileout = FileOpen(@ScriptDir & "\output.txt", 2)
$Filelist = _FileListToArray($Path, "*.ini", 1); load .ini files from directory into array
For $i = 1 To $Filelist[0]; loop through array and process each .ini file
    $Current = $Path & '\' & $Filelist[$i]
    $key1 = IniRead($Current, "drivers", "wave",'')
    $key2 = IniRead($Current, "drivers", "timer",'')
    If ($key1) and ($key2) Then; output only if both keys found
        FileWriteLine($Fileout, $Current & ": " & $key1 & " - " & $key2)
    EndIf
Next
FileClose($Fileout)

Edit: That ought ot work for your .ini files. Now I'm off to inquire about why it doesnt behave correctly with system.ini

Edit2: The odd behavior I'm getting apparently only occurs with "system-component initialization files" such as win.ini, system.ini, control.ini, etc. They are remapped to the registry and the values returned may not match those on disk. The IniRead() function will work fine with your user-created .ini files.

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