DickG

Empty lines at bottom of INI files get deleted

12 posts in this topic

I just installed v3.3.14.2 (from v3.3.10.2). I just noticed something really strange: When I try to add a new line at the bottom of an INI, it can't because something is deleting any empty lines at the bottom. I verified that it's only my code that's doing it. But whenever I run a script or compile it, the lines get deleted. Anyone else run into this problem?

Share this post


Link to post
Share on other sites



Why is that a problem?

1 person likes this

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Why is that a problem?

probably for organization so he can quickly add to a list or something 

like without having to press space

Edited by Hospital
mroe

Share this post


Link to post
Share on other sites

Why is that a problem?

Can someone confirm that this is indeed a change in "something"? After further testing, it looks like _FileCountLines() has changed how it works. It used to include ALL lines (including empty lines at bottom). Then I would know if I need to add another blank line. But with v3.3.14.2, it only looks for lines that have text. So I can no longer tell if I have to add another blank line before I add a new record.

First, create a dummy ini file with this:

[General]

[SectionA]

[LastSection]


There are two blank lines at the end.

Then try this:

#Include <Array.au3>
#Include <File.au3>

Dim $File_array

Local $Path = "<your path>Test.ini"

$Last_line = _FileCountLines($Path)
MsgBox(0, "Test", "Last line: " & $Last_line)

It will return 5, but should be 7.

I have a lot of scripts that depend on knowing if there is an empty line at the end of an INI section so that I can add a new record. But _FileWriteToLine("Path", $Last_line, "New record", False) would overwrite the last record because it thought that $Last_line was BELOW the last line, not AT the last line. Maybe there's a better way of doing this, but it's been working fine up until v3.3.14.2.

I hope this explains it.

Share this post


Link to post
Share on other sites

You shouldn't use _File* functions to read/write an INI file. To manipulate an INI file AutoIt has Ini* functions. And those functions don't care about empty lines.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Water is right, but if you disagree then you are completely allowed to build your own custom FileCountLines func
The one from File.au3 is regexp-based so you can use this little variant (try it on your dummy .ini from post #4)

#Include <File.au3>

$lines1 = _FileCountLines("1.ini")
Msgbox(0,"", $lines1)

$lines2 = UBound(StringRegExp(FileRead("1.ini"), '\R', 3))
Msgbox(0,"", $lines2)

 

Share this post


Link to post
Share on other sites

Water is right, but if you disagree then you are completely allowed to build your own custom FileCountLines func
The one from File.au3 is regexp-based so you can use this little variant (try it on your dummy .ini from post #4)

#Include <File.au3>

$lines1 = _FileCountLines("1.ini")
Msgbox(0,"", $lines1)

$lines2 = UBound(StringRegExp(FileRead("1.ini"), '\R', 3))
Msgbox(0,"", $lines2)

 

I tried those INI functions in the beginning, but was frusrated by how limited they are. I couldn't do things like insert a record below or above another record, or insert a new section in a specific place in the INI.

So I built my own set of INI functions to deal with the things I wanted it to do.

It can also simulate "nested" folders in records by using tabs (one for each level), then counting the tabs later to tell me which level it's at when building a treeview or listview to show the hierarchy. I can also insert new sections anywhere I want in the INI, move entire sections up and down, and so on.

_FileCountLines() used to count empty lines at the bottom. But no more. So I had modify all my scripts to look for the end of a section and compare it to the "last line". So I had to write my own __FileCountLines() function. It uses _FileRecToArray("INI file", $File_array). Then I set $Last_line = $File_array[0].

My INI UDFs returns $Section_start and $Section_end (as well as the Key and Val for a record) for any section. I can then compare $Section_end to $Last_line. If they are the same, it means the last record is also the last line. But to add a new line to the bottom, I had to have the "pointer" sitting below the last line. Otherwise, it wasn't writing the blank line to the end. Hence, it wasn't adding a new record to the end. So I was losing data without knowing it.

Maybe my need for more power behind manipulating INIs isn't common. But I can do powerful things with it. Like a project manager that shows the "nested" INI sections in a treeview, then shows the files contained in a selected section in a listview to the right, for example.

Anyway, I've accepted that it changed, and tweaked things to get around the problem.

Share this post


Link to post
Share on other sites

If you're worried about where in the INI file the lines are, you're using the INI file and the functions for them wrong.

The standards for INI files specifies that they are never in any type of order other than SECTION/KEYs in that section/VALUE assigned to that key. 

You are using them as flat files, which is fine to do, they're just not INI files when you treat them that way regardless of how you name them. They may look like an INI file, you may format them like one, but how you're using them is not a standardized way. 


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

why do you need to know the count if you are just adding a record to the end of the file?

FileWriteLine ("testoutput.ini", "TestKey = UnderBlankLinesAtTheEnd")

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Although I'm a big fan of INI files usually (though they can having annoying issues), you would probably be better off using XML in your case.

Either that, or do something like you are doing, and give them a different extension, as an INI file is a specific type of file, and used in a specific manner. You can still use IniRead and IniWrite with whatever extension you use instead of INI ... or just use Array functions.

Some people use CFG or LOG or INF etc ... or make up your own.

P.S. I seem to recall, that guinness wrote some special kind of INI functions or setup ... check Examples for it.

P.S.S. I've been using INI files since Win 3.1 and in those days a blank line got created between sections. Later they (Microsoft) made it so that blank lines got stripped. Some INI commands still strip blank lines, some don't.

Edited by TheSaint

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

why do you need to know the count if you are just adding a record to the end of the file?

FileWriteLine ("testoutput.ini", "TestKey = UnderBlankLinesAtTheEnd")

 

I tried that before, but I had to deal with too many FileOpen() and FileClose() instances. So I abandoned that method.

I wanted a way to write to a file without using FileOpen/Close(). _FileWriteToLine() fit the bill perfectly. But it seems like something changed in v3.3.14.2. It no longer allows empty lines at the bottom. So now I use IniWrite() only if I'm adding a new record to the bottom of the file. Otherwise, I NEED to have records and sections in a specific order for my applications.

Share this post


Link to post
Share on other sites

Although I'm a big fan of INI files usually (though they can having annoying issues), you would probably be better off using XML in your case.

Either that, or do something like you are doing, and give them a different extension, as an INI file is a specific type of file, and used in a specific manner. You can still use IniRead and IniWrite with whatever extension you use instead of INI ... or just use Array functions.

Some people use CFG or LOG or INF etc ... or make up your own.

P.S. I seem to recall, that guinness wrote some special kind of INI functions or setup ... check Examples for it.

P.S.S. I've been using INI files since Win 3.1 and in those days a blank line got created between sections. Later they (Microsoft) made it so that blank lines got stripped. Some INI commands still strip blank lines, some don't.

I could rename them. But it doesn't cause a problem for me to call them "INI" files.

I found that "INI" files were the most effective way (for me, anyway) to deal with a faux flat-file database such as financial records and other types of structured info.

If the standard way of using an INI is locked down somewhere, it will remain frustrating to deal with for me. Using XML would mean I would have to learn that syntax too. It's not structured to be easy to read at the raw file level.

I created apps to replace programs like Quicken and Info Select (which uses their own proprietary database). I could not easily export the data from them. So I got frustrated with dealing with proprietary databases and decided to make my own replacements. They track things like financial records that must be in Date order, sections that must be in a specific order, etc. I also needed a way to insert records between two records (like when adding a financial transaction before or after a certain date). I could not do that with the standard INI functions. But now I can with my own functions that handles non-standard "INI" files.

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