dirty Posted September 17, 2011 Share Posted September 17, 2011 (edited) Ill get straight to the point 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 expandcollapse popup#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 ThanksPathSwap.au3 Edited September 17, 2011 by dirty Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted September 17, 2011 Moderators Share Posted September 17, 2011 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). I hope this script does what you want - it certainly does when I test it: expandcollapse popup#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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
dirty Posted September 17, 2011 Author Share Posted September 17, 2011 (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 September 17, 2011 by dirty Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted September 17, 2011 Moderators Share Posted September 17, 2011 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? $sDir = @ScriptDirI did mention:; Set this to what you want as the root pathM23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted September 17, 2011 Moderators Share Posted September 17, 2011 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 errorBecause 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. 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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
dirty Posted September 17, 2011 Author Share Posted September 17, 2011 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 Already using your UDF, its awesome Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now