Sign in to follow this  
Followers 0
mcgill

INI delete lines after a keyword is found

8 posts in this topic

Good day,

I would like to read into an INI file and search for a section then a keyword. Once keyword has been found I would like to delete every other line below this keyword in the ini file and save and close the file. Right now I am doing this with send key with file being opened and the user sees it happenning. How can I do it without the user seeing the file being opened in notepad, thanks

$var = IniRead("C:\Program Files\Lotus\Notes\notes.ini", "Notes", "Timezone", "NotFound")

Share this post


Link to post
Share on other sites



Try using _FileReadToArray then search each array element for the line, then overwrite the file and write the lines you want.

Share this post


Link to post
Share on other sites

There is no sorting to an INI file. There is no guarantee that you won't accidentally delete data you wanted to keep if you search for a keyword and then delete the rest of the file. If you know which keys you need to delete, delete them and only them. Otherwise, if you know which keys you need to keep, you could delete all keys not matching them (This is dangerous, though). Just searching for a keyword and deleting all lines after that line could result in deleting almost the entire file.

Share this post


Link to post
Share on other sites

I understand it can be dangerous to delete certain lines in a ini file. All I need is a few lines(keys) after this section is found. The [Notes] section is the first and only section in the file. So After the Timezone text I want it to delete all the other lines in the ini file and save and close. This is a fix for lotus notes, thats why I want to do it.

[Notes]

KitType=1

Directory=C:\Program Files\lotus\notes\data

InstallType=6

FaultRecovery_Build=Release 6.5.1

Timezone=6

Share this post


Link to post
Share on other sites

I think it's easier to delete (or better rename) the old 'notes.ini' and then to write a new one.

Share this post


Link to post
Share on other sites

I understand it can be dangerous to delete certain lines in a ini file. All I need is a few lines(keys) after this section is found. The [Notes] section is the first and only section in the file. So After the Timezone text I want it to delete all the other lines in the ini file and save and close. This is a fix for lotus notes, thats why I want to do it.

[Notes]

KitType=1

Directory=C:\Program Files\lotus\notes\data

InstallType=6

FaultRecovery_Build=Release 6.5.1

Timezone=6

What happens if your file appears like this?

[Notes]
Timezone=6
KitType=1
Directory=C:\Program Files\lotus\notes\data
InstallType=6
FaultRecovery_Build=Release 6.5.1

Can you gurantee that the keys will be in the order you think 100% of the time?

Are you really trying to say that you want to delete all the sections except for the Notes section? If that is what you are saying, see the IniReadSectionNames() function to get a list of all the section names. You can then iterate that list deleting all the sections except "Notes". Otherwise, you should look at IniReadSection() to enumerate the keys in a particular section and prune only the keys you don't need.

Share this post


Link to post
Share on other sites

Why bother with AutoIT - just do it in DOS - two lines!

Echo [Notes] > newinifile.ini

Type oldininfile.ini | Find "Timezone" >> newinifile.ini

Note the single chevron (>) in the first line to create a new file (or overwrite an existing one), and a double chevron (>>) on the second line to append any lines that contain the word Timezone in the oldinifile.ini. The pipe (|) character just before the Find statement is a vertical bar, found above the backward slash (\) on my keyboard - it may be in a different place on yours.

Share this post


Link to post
Share on other sites

Why bother with AutoIT - just do it in DOS - two lines!

Echo [Notes] > newinifile.ini

Type oldininfile.ini | Find "Timezone" >> newinifile.ini

Note the single chevron (>) in the first line to create a new file (or overwrite an existing one), and a double chevron (>>) on the second line to append any lines that contain the word Timezone in the oldinifile.ini. The pipe (|) character just before the Find statement is a vertical bar, found above the backward slash (\) on my keyboard - it may be in a different place on yours.

That does not do what he wants, as it only copies lines containing "Timezone", all other lines drop in the bit bucket. To do this in CMD shell script still requires a For /F loop that appends each line until it sees "Timezone", then exits. Certainly doable, but not any easier than AutoIT, and still doesn't address the issue of being SURE that everything after "Timezone" really should be deleted.

If you can make a list of offending keys, it would be much more reliable to just delete those keys directly.

:whistle:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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