Sign in to follow this  
Followers 0
dirty

search and replace strings in ini file

6 posts in this topic

#1 ·  Posted (edited)

Ill get straight to the point :graduated:

i have ini with

[Project]
Name=Midas Rex
Count=1
Number=N/A
Category=Neuro
Location=Who knows
ImagePath=F:\Autoit projects\CS Surgical Inventory managment\St. Anthony Sets\Instrument Pictures\Neuro\Midas Rex.JPG
MFDPath=F:\Autoit projects\CS Surgical Inventory managment\St. Anthony Sets\Instrument Pictures\Neuro\doc.pdf
CountSheet=F:\Autoit projects\CS Surgical Inventory managment\St. Anthony Sets\\Neuro\Midas Rex.xls
Note=Aditional Notes: No notes

what would be the simplest way to find and replace strings with paths to file ?

I tried

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <File.au3>
#Include <Array.au3>
 
$Source = ""
$SwapWith = ""
Global $dir = @DesktopDir
$GUI = GUICreate ("PathSwap",300,200)
$StringInput= GUICtrlCreateInput ($Source,10,20,200,20)
$SeplaceInput= GUICtrlCreateInput ($SwapWith ,10,40,200,20)
$Go= GUICtrlCreateButton ("Go !",10,60,200,20)
GUISetState (@SW_SHOW)
While 1
    $msg = GUIGetMsg()
        If $msg = $GUI_EVENT_CLOSE Then
            Exit
        EndIf
        If $msg = $Go Then
            _searchName($dir)
        EndIf
WEnd
Func _searchName($dir)
    Local $ArrTargetItems, $TargetItem
    If (StringRight($dir, 1) = "\") Then $dir = StringTrimRight($dir, 1)
        $ArrTargetItems = _FileListToArray($dir, "*", 0)
        If IsArray($ArrTargetItems) Then
            For $n = 1 To $ArrTargetItems[0]
                $TargetItem = $dir & '\' & $ArrTargetItems[$n]
                If StringInStr(FileGetAttrib($TargetItem), "D") Then ;This is a folder
                    _searchName($TargetItem) ;Call recursively
                Else ;This is a file
                    If StringRight ($TargetItem,4) = ".ini" Then
                        $String = GUICtrlRead ($StringInput)
                        $ReadImagePath = IniRead ($TargetItem,"Project","ImagePath","")
                        $StringPos = StringInStr ($ReadImagePath,$String)
                        If $StringPos >0 Then
                            $RemoveUnwanted = StringTrimLeft ($ReadImagePath,$StringPos)
                            MsgBox(0,'',$StringPos)
                        EndIf
                    EndIf
                EndIf
            Next
        EndIf
EndFunc

i spent a day trying to figure out how to replace paths for all linked filenames,keeping filenames and replacing paths only in all found ini files.

Can you guys give me instructions or a step by step procedure on how that needs to be done ? for example:

#1Find file

#2 Get string by iniread

#3 Count how many characters in given string given by ini

#4 isolate and remove string that is provided in GUI, in this case a different path

#5 replace path from ini with provided in GUI keeping the filename unchanged.

i have a clue how to do that bu its too much confusion and my brain is not working :)

Need someone who had better sleep then me ;)

Thanks

PathSwap.au3

Edited by dirty

Share this post


Link to post
Share on other sites



dirty,

Rather than debug your recursive code I used my RecFileListToArray[/] UDF to get all the *.ini files on the path (look in my sig to download it). :graduated:

I hope this script does what you want - it certainly does when I test it: :)

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <WinAPI.au3>
#include <Array.au3>
#include <RecFileListToArray.au3>

; Set this to what you want as the root path
$sDir = @ScriptDir

; Create GUI
$hGUI = GUICreate("PathSwap", 300, 200)

$hStringInput = GUICtrlCreateInput("", 10, 20, 200, 20)
GUICtrlSendMsg(-1, $EM_SETCUEBANNER, True, "Current Path")
$hReplaceInput = GUICtrlCreateInput("", 10, 50, 200, 20)
GUICtrlSendMsg(-1, $EM_SETCUEBANNER, True, "Replacement Path")
$hGo = GUICtrlCreateButton("Go !", 10, 80, 200, 20)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hGo
            _searchName($sDir)
    EndSwitch

WEnd


Func _searchName($sDir)

    ; List all the ini files on this path
    $aFiles = _RecFileListToArray($sDir, "*.ini", 1, 1, 0, 2)

    ; Now taking each one in turn
    For $i = 1 To $aFiles[0]

        ; Read the file contents
        $sText = FileRead($aFiles[$i])
        ; Display - just for interest
        MsgBox(0, $aFiles[$i], $sText)
        ; Replace the current path with the new one
        $sText = StringReplace($sText, GUICtrlRead($hStringInput), GUICtrlRead($hReplaceInput))
        ; If there were replacements
        If @extended Then
            ; Show the new content - just for interest
            MsgBox(0, $aFiles[$i], $sText)
            ; Open teh file and overwrite with the new content
            $hFile = FileOpen($aFiles[$i], 2)
            FileWrite($hFile, $sText)
            FileClose($hFile)
        Else
            ; Say nothing was changed - just for interest
            MsgBox(0, $aFiles[$i], "Not Found")
        EndIf

    Next

EndFunc

If you have any questions, please ask. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

C:\Users\Administrator\Desktop\RFLTA\New AutoIt v3 Script (2).au3 (39) : ==> Subscript used with non-Array variable.:

For $i = 1 To $aFiles[0]

For $i = 1 To $aFiles^ ERROR

i used code you gave me above but only when dir was @scriptdir. i fixed that and it worked. still dont understand why dir would cause such error.

Thanks much.

So does this UDF uses winapi's ?

Edited by dirty

Share this post


Link to post
Share on other sites

dirty,

It means you did not find any *.ini files on the path you set. Did you change this line to reflect the path you want to use? :graduated:

$sDir = @ScriptDir

I did mention:

; Set this to what you want as the root path

;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

dirty,

Just seen your edit - nice to see my remote debugging skills are still up to scratch! :)

still dont understand why dir would cause such error

Because if there are no files found the UDF returns an error and not an array. So when you try to access the array with $aFiles[0] there is no such element and you get an error. I could have added errorchecking code but for such a minor script snippet I did not. :graduated:

So does this UDF uses winapi's ?

No, it uses an iterative search algorithm. Take a look at the Recursion tutorial in the Wiki to see why I use that type of algorithm and not full recursion as you tried to do. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

yes thanks i got this part, and hey i really appreciate you describe each stem of your function, its very helpful for noobs like me :graduated:

Already using your UDF, its awesome

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