Jump to content

Is there a function like "FileInsLine($file,$LineCount,$insLine)" ?


rudi
 Share

Recommended Posts

Hello,

I want to add a line into an INF file just after a section begins.

Searching the line, where "[sECTION]" shows up is easy.

My idea to insert a line would be to do a 2nd run, reading the original file, write it out to a dummy file, until I hit the "[section]", then write my "This line is to be inserted" to the dummy file and then read/write the rest of the original file till EOF, close both and to delete the original file, then copy over the dummy one.

This looks not too nice to me, so I want to ask, if there is a function like FileInsLine([file,LineCont,LineString) ?

Also there will be some checking necessary: Does the file exist? Can it be written to? Is the line number given valid? ...

There are sooo many au3 extras coded by the people here that the chances might be high I just will miss it :">

Thanks, regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

Hello,

I want to add a line into an INF file just after a section begins.

Searching the line, where "[sECTION]" shows up is easy.

My idea to insert a line would be to do a 2nd run, reading the original file, write it out to a dummy file, until I hit the "[section]", then write my "This line is to be inserted" to the dummy file and then read/write the rest of the original file till EOF, close both and to delete the original file, then copy over the dummy one.

This looks not too nice to me, so I want to ask, if there is a function like FileInsLine([file,LineCont,LineString) ?

Also there will be some checking necessary: Does the file exist? Can it be written to? Is the line number given valid? ...

There are sooo many au3 extras coded by the people here that the chances might be high I just will miss it :">

Thanks, regards, Rudi.

Won't INI Write Do?
Link to comment
Share on other sites

_FileReplaceLine("Test.ini", "[Section]", "New Line", 0)

Func _FileReplaceLine($sFile, $sOldLine, $sNewLine, $iMode=0)
    Local $aArray = StringSplit(FileRead($sFile), @LF), $ReplStr
    If Not IsArray($aArray) Then Return SetError(1, 0, 0)
    
    Local $hFilew = FileOpen($sFile, 2), $LF = @LF
    If $hFilew = -1 Then Return SetError(2, 0, 0)
    For $i = 1 To UBound($aArray) - 1
        If $iMode <> 0 Or ($iMode = 0 And $aArray[$i] = $sOldLine) Then $aArray[$i] = StringReplace($aArray[$i], $sOldLine, $sNewLine)
        If $i = UBound($aArray) - 1 Then $LF = ""
        FileWrite($hFilew, $aArray[$i] & $LF)
    Next
    FileClose($hFilew)
    Return 1
EndFunc

If you use 1 as last parameter, then will be preformed a standard replacement in whole file.

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

  • 2 weeks later...

Hello,

thanks for your reply! Yes, this looks very good. Is this function included in some public AU3 colletion? Which one?

May I ask some questions on that code?

_FileReplaceLine("Test.ini", "[Section]", "New Line", 0)

Func _FileReplaceLine($sFile, $sOldLine, $sNewLine, $iMode=0)
    Local $aArray = StringSplit(FileRead($sFile), @LF), $ReplStroÝ÷ Øb²z-­ê®·¶!yÉ¢°,V¬Áée¬,W(½êÛ¢ØS+*ºl¦X­)^+­¬ªºpjDZiËeËZ´²Z()à~ÚºÚ"µÍRYÝÐ^J   ÌÍØP^JH[]Ù]ÜK
BBSØØ[    ÌÍÚ[]ÈH[SÜ[    ÌÍÜÑ[KoÝ÷ Ù8b±¶¬Æ¥(¬mêÚ²)àý»azâ)Úø¥z¸ Ò7öhî²Ü(׫x!£baz¸¬h ÖÞ¶â©eËdYwöØ^¢¸ v¥~)^jëh×6 Local $LF = @LF
    If $hFilew = -1 Then Return SetError(2, 0, 0)
    For $i = 1 To UBound($aArray) - 1
        If $iMode <> 0 Or ($iMode = 0 And $aArray[$i] = $sOldLine) Then $aArray[$i] = StringReplace($aArray[$i], $sOldLine, $sNewLine)
        If $i = UBound($aArray) - 1 Then $LF = ""
        FileWrite($hFilew, $aArray[$i] & $LF)
    Next
    FileClose($hFilew)
    Return 1
EndFunc

This is the part I was looking for: With a little change I'm at my goal: $iMode = 2 ->insert before, $iMode = insert after searched line.

Thanks a lot, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

Hello,

thanks for your reply! Yes, this looks very good. Is this function included in some public AU3 colletion? Which one?

May I ask some questions on that code?

_FileReplaceLine("Test.ini", "[Section]", "New Line", 0)

Func _FileReplaceLine($sFile, $sOldLine, $sNewLine, $iMode=0)
    Local $aArray = StringSplit(FileRead($sFile), @LF), $ReplStroÝ÷ Øb²z-­ê®·¶!yÉ¢°,V¬Áée¬,W(½êÛ¢ØS+*ºl¦X­)^+­¬ªºpjDZiËeËZ´²Z()à~ÚºÚ"µÍRYÝÐ^J   ÌÍØP^JH[]Ù]ÜK
BBSØØ[    ÌÍÚ[]ÈH[SÜ[    ÌÍÜÑ[KoÝ÷ Ù8b±¶¬Æ¥(¬mêÚ²)àý»azâ)Úø¥z¸ Ò7öhî²Ü(׫x!£baz¸¬h ÖÞ¶â©eËdYwöØ^¢¸ v¥~)^jëh×6 Local $LF = @LF
    If $hFilew = -1 Then Return SetError(2, 0, 0)
    For $i = 1 To UBound($aArray) - 1
        If $iMode <> 0 Or ($iMode = 0 And $aArray[$i] = $sOldLine) Then $aArray[$i] = StringReplace($aArray[$i], $sOldLine, $sNewLine)
        If $i = UBound($aArray) - 1 Then $LF = ""
        FileWrite($hFilew, $aArray[$i] & $LF)
    Next
    FileClose($hFilew)
    Return 1
EndFunc

This is the part I was looking for: With a little change I'm at my goal: $iMode = 2 ->insert before, $iMode = insert after searched line.

Thanks a lot, Rudi.

If you ever need code, you can look at the Examples section of this forum, the download section (which should be in my sig) or even Big_Daddy's Snippet Database. In comming months, I will be releasing a UDF repository, which I have slowly been working on.

I'm not really sure on that part.

@LF = A line feed (Chr(10))

@CR = Carrige Return (Chr(13))

@CRLF = @CR + @LF

Maybe will help you understand, because I don't see if it would or not. As far as I know it might?

Yes. This does erase the previous file. To work that out, I take you looked at FileOpen in the helpfile? You could try to change the flags, or instead, write to a new file.

Glad to hear you reached your goal <_<

Link to comment
Share on other sites

Is this function included in some public AU3 colletion?

No, i wrote it specialy for you :P

It is not required to check for @CRLF as well? Does @LF cover both?

Basicly @LF is always part of new line in files, but the @CRLF not always can be present, so i think that is better to use @LF to be sure <_<

This basically is erasing the original file, right? :) I'm just wondering how high the risk might be to finally 'kill' the original file...

The risk is always there, but you can always make an temporary backup and then delete it after seccess :) (FileCopy($sFile, $SsFile & ".bak") and FileDelete($sFile & ".bak")). Edited by MsCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

If you ever need code, you can look at the Examples section of this forum, [snip]

The autoit3lib installer is broken, as stated on the download page: It's complaining a to low version number of Autoit3.

The installer succeeds, when going to the registry, HKLM\SW\Autoit v3\autoit, Version, and changing the value to what the installer is searching for.

Who might put that hint in the download page beside autoit3lib installer?

Just for excercise I wrote this:

(propably not too good code, but working for me :-) )

Dim $VersionOK = False
Dim $RefArray, $Limit, $i

$RefArray = StringSplit("3.2.4.9", ".")

$result = RegRead("hklm\software\autoit v3\autoit", "Version") ; no checking done to see if autoit3 is installed at all!


$VerArray = StringSplit($result, "v")
If UBound($VerArray) > 1 Then 
    $VerArray = StringSplit($VerArray[$VerArray[0]], ".")
Else
    MsgBox(0,"Unexpected Version","Can't handle your version number: ")
EndIf


If $RefArray[0] <= $VerArray[0] Then
    $Limit = $RefArray [0]
Else
    $Limit = $VerArray[0]
EndIf

For $i = 1 To $Limit
    If $VerArray[$i] > $RefArray[$i] Then
        $VersionOK = True
        ExitLoop
    Else
        If $RefArray[$i] = $VerArray[$i] Then ; this level of version is the same, continue checking...
            $VersionOK = True
        Else
            $VersionOK = False
            ExitLoop
        EndIf
    EndIf
Next



If $VersionOK Then
    RegWrite("hklm\software\autoit v3\autoit", "Version", "REG_SZ", "v3.2.4.9")
    MsgBox(64, "Version ""fix"" done", "Now you can start the auto3lib.exe installer. Wait clicking OK here, until auto3lib installer has finished!")
    RegWrite("hklm\software\autoit v3\autoit", "Version", "REG_SZ", $result)
    MsgBox(64, "Done", "Your original Version information has been restored: " & $result)
Else
    MsgBox(64, "Too old version", "Your Autoit3 Version is too old: " & $result & @LF & "Has to be v3.2.4.9 or above")
EndIf

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

Why not just use the build-in IniWrite()

Well, one reason (for me) is that IniWrite/Read not support "UTF-8 with Signature" encoding <_< Edited by MsCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

The autoit3lib installer is broken, as stated on the download page: It's complaining a to low version number of Autoit3.

The installer succeeds, when going to the registry, HKLM\SW\Autoit v3\autoit, Version, and changing the value to what the installer is searching for.

Who might put that hint in the download page beside autoit3lib installer?

Just for excercise I wrote this:

(propably not too good code, but working for me :-) )

Works for you? Great, but big_daddy already fixed it.

Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Link to comment
Share on other sites

Works for you? Great, but big_daddy already fixed it.

Nice <_< So it was just woth to install it on several machines here :) (as of writing these lines it wasn't fixed...)

My code of course is pretty clumsy(?), but it's doing a correct version check ;-)

Thanks for the info, Rudi.

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

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...