hassankhokhar11 Posted November 8, 2010 Share Posted November 8, 2010 (edited) hi Edited November 9, 2010 by hassankhokhar11 Link to comment Share on other sites More sharing options...
JoHanatCent Posted November 8, 2010 Share Posted November 8, 2010 (edited) Try this line: $Search = FileFindFirstFile($SourceFolder & "*.pcf") replacing: $Search = FileFindFirstFile("*.pcf") Edit ========>> I saw that it did not want to work in the "Examples Forum" Please make sure that the first line: Dim $FolderName = "C:\Documents and Settings\hnaveed\Desktop\Profiles" Is spelled correctly and valid. Edited November 8, 2010 by JoHanatCent Link to comment Share on other sites More sharing options...
hassankhokhar11 Posted November 8, 2010 Author Share Posted November 8, 2010 Try this line:$Search = FileFindFirstFile($SourceFolder & "*.pcf")replacing:$Search = FileFindFirstFile("*.pcf")Edit ========>>I saw that it did not want to work in the "Examples Forum"Please make sure that the first line:Dim $FolderName = "C:\Documents and Settings\hnaveed\Desktop\Profiles"Is spelled correctly and valid.yes it is spelled correctly and valid but it still doesent works Link to comment Share on other sites More sharing options...
UEZ Posted November 8, 2010 Share Posted November 8, 2010 (edited) What I read from your code is you search for files and replace the content (string) with another string. Try this: Look at post #19I hope it helps you! Br, UEZ Edited November 30, 2010 by UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
hassankhokhar11 Posted November 9, 2010 Author Share Posted November 9, 2010 can i do it without using array??? Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted November 9, 2010 Moderators Share Posted November 9, 2010 hassankhokhar11, Please do not remove the code that was causing you a problem from the first post of a thread. It makes the rest of the thread meaningless. 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...
UEZ Posted November 9, 2010 Share Posted November 9, 2010 (edited) can i do it without using array??? Yes you can! Did the code I posted work for you? Br, UEZ Edited November 9, 2010 by UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
hassankhokhar11 Posted November 9, 2010 Author Share Posted November 9, 2010 Yes you can! Did the code I posted work for you? Br, UEZ yes it is but i want it to do it without arraysi will be thankful to u if u can correct my code and plus i want my code to write that string in a file were it do not find that string like if that string is missing from the file it should write it aswell...my code.au3 Link to comment Share on other sites More sharing options...
UEZ Posted November 9, 2010 Share Posted November 9, 2010 What is the problem with arrays? I cannot follow! Br, UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
hassankhokhar11 Posted November 9, 2010 Author Share Posted November 9, 2010 What is the problem with arrays? I cannot follow!Br,UEZim not allowed to use arrays but if u can tell how should i make a function of array which reads the file and if it dosent find that string in it it should write on it?? Link to comment Share on other sites More sharing options...
GEOSoft Posted November 9, 2010 Share Posted November 9, 2010 If the string is not found, do you just want it appended to the end of a file or do you want it written to a particular location in the file? George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
GEOSoft Posted November 9, 2010 Share Posted November 9, 2010 @UEZ Nice code but you may want to correct the function header for SearchAndReplace(). You left $replace out of the parameters section. George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
UEZ Posted November 9, 2010 Share Posted November 9, 2010 @UEZNice code but you may want to correct the function header for SearchAndReplace(). You left $replace out of the parameters section.Indeed, thanks for the hint.Corrected.Br,UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
hassankhokhar11 Posted November 9, 2010 Author Share Posted November 9, 2010 Indeed, thanks for the hint.Corrected.Br,UEZcan i get the corrected version?? Link to comment Share on other sites More sharing options...
hassankhokhar11 Posted November 9, 2010 Author Share Posted November 9, 2010 If the string is not found, do you just want it appended to the end of a file or do you want it written to a particular location in the file?it should be in the last line of the file...thanks if u can help plz Link to comment Share on other sites More sharing options...
UEZ Posted November 9, 2010 Share Posted November 9, 2010 can i get the corrected version??Look at post #4. I forgot the $replace description in function header, so no impact to the function itself!You can modify the code I wrote by adding the string to the last line easily!Try it, if you need help just post it.Br,UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
GEOSoft Posted November 9, 2010 Share Posted November 9, 2010 Just off the top of my head. $sFile = "C:\some\path\somefile.txt" $sStr = _Replace($sFile, "some string", "some other string");; or _Replace($sFile, "some string", "some other string", True) if you want it to be case sensitive. If @Extended Then $hFile = FileOpen($sFile, 2) FileWrite($hFile, $sStr) FileClose($hFile) EndIf Func _Replace($sString, $sFind, $sReplace, $bCaseSense = False) If FileExists($sString) Then $sString = FileRead($sString) Local $sSre = $sFind $sString = StringStripWS($sString, 2);; gets rid of trailing whitespace. comment it out if you don't need it If $bCaseSense Then $sSre = "(?i)" & $sSre) $sString = StringRegExpReplace($sString, $sSRE, $sReplace);; Comment this line out if you instead want the next line ;;$sString = StringRegExpReplace($sString, "(?m:^).*" & $sSre & ".*", $sReplace);; Use this one if you want to replace an entire line if it contains $sFind If NOT @Extended Then $sString &= @CRLF & $sReplace Return SetError(0, 1, $sString) EndFunc George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
hassankhokhar11 Posted November 10, 2010 Author Share Posted November 10, 2010 Look at post #4. I forgot the $replace description in function header, so no impact to the function itself!You can modify the code I wrote by adding the string to the last line easily!Try it, if you need help just post it.Br,UEZive tried but its not working i might not be doing it the right way can u plz send the code so i can add that to ur old code...code for if the string is not found so it should write the string in that file.... Link to comment Share on other sites More sharing options...
UEZ Posted November 10, 2010 Share Posted November 10, 2010 (edited) Try this: expandcollapse popup#include <Array.au3> Opt("MustDeclareVars", 1) Dim $aFiles[1] Local $path = "C:\Documents and Settings\hnaveed\Desktop\Profiles" Local $file = ".pcf" $aFiles = File_Seeker($path, $file, $aFiles, 1) ;~ _ArrayDisplay($aFiles) Local $n, $i, $hFile Local $search = "SDIUseHardwareToken=1" Local $replace = "SDIUseHardwareToken=0" For $i = 1 To UBound($aFiles) - 1 $n = SearchAndReplace($aFiles[$i], $search, $replace) If $n = 0 Then $hFile = FileOpen($aFiles[$i], 1) FileWriteLine($hFile, $replace) FileClose($hFile) ConsoleWrite("Added line " & $replace & " to " & $aFiles[$i] & @CRLF) Else ConsoleWrite("Replaced " & $n & " times in file " & $aFiles[$i] & " " & $search & " with " & $replace & @CRLF) EndIf Next Exit ;================================================================================================== ; Function Name: SearchAndReplace($file, $search, $replace, $CaseSensitive = False) ; Description: searchs for a give string and replace it with another string. ; Overwrites file afterwards if replacement > 0! ; Parameter(s): $file filename to search for string ; $search search for string ; $replace replace it with this string ; $CaseSensitive enable / disable case sensitivity ; Return Value(s): amount of replacements ; Version: v0.50 Build 2010-11-08 Beta ; Author(s): UEZ ;================================================================================================== Func SearchAndReplace($file, $search, $replace, $CaseSensitive = False) If Not FileExists($file) Then Return SetError(1, 0, "File not found!") Local $sReplace, $no Local $string = FileRead($file) If @error Then Return SetError(2, 0, "Could not read file!") If $CaseSensitive Then $sReplace = StringRegExpReplace($string, $search, $replace) Else $sReplace = StringRegExpReplace($string, "(?i)" & $search, $replace) EndIf Local $no = @extended If $no > 0 Then Local $hFile = FileOpen($file, 2) If @error Then Return SetError(3, 0, "Could not create file " & $file & "!") FileWrite($hFile, $sReplace) If @error Then Return SetError(4, 0, "Unable to write file!") FileClose($hFile) EndIf Return SetError(0, 0, $no) EndFunc ;================================================================================================== ; Function Name: File_Seeker($path, $file, $array) ; Description: searchs for a file recursively on given drive ; Parameter(s): $path path where to search ; $file filename to search for ; $array to save the search results ; $ext search for file extension, e.g. .mp3, .txt, .log ; $exact to search for exact file name (not case sensitive) ; $rec to search recursively for files and folders ; $folder include also folder names to search pattern ; Return Value(s): an array with the full path of the files, $array[0] = amount of found files ; Version: v0.82 Build 2010-02-13 Beta ; Author(s): UEZ ;================================================================================================== Func File_Seeker($path, $file, ByRef $aResults, $ext = 0, $exact = 0, $rec = 1, $folder = 0) Local $extended If $file <> "" And IsArray($aResults) And BitAND($ext, $exact) <> 1 Then If StringRegExp(StringLeft($path, 1), "(?i)[a-z]") And StringMid($path, 2, 1) = ":" Then If StringRight($path, 1) <> "\" Then $path &= "\" Local $seek, $suffix = "" Local $search = FileFindFirstFile($path & "*") If $search = -1 Then Return SetError(1, 0, 0) EndIf While 1 $seek = FileFindNextFile($search) $extended = @extended If @error Then ExitLoop If $extended And $rec Then File_Seeker($path & $seek & "\", $file, $aResults, $ext, $exact, $rec, $folder) If $exact And Not $ext Then If $seek = $file Then _ArrayAdd($aResults, $path & $seek) Else If $ext Then If StringRegExp($seek, "(?i:\" & $file & "+$\b)") And StringLeft($file, 1) = "." Then If ($extended And $folder) Or Not $extended Then _ArrayAdd($aResults, $path & $seek) EndIf Else If $file = "*" Then If ($extended And $folder) Or Not $extended Then _ArrayAdd($aResults, $path & $seek) Else If StringInStr($file, ".") Then If StringRegExp($seek, "(?i:\" & $file & ")+") Then If ($extended And $folder) Or Not $extended Then _ArrayAdd($aResults, $path & $seek) EndIf Else If StringRegExp($seek, "(?i:" & $file & ")+") Then If ($extended And $folder) Or Not $extended Then _ArrayAdd($aResults, $path & $seek) EndIf EndIf EndIf EndIf EndIf WEnd FileClose($search) $aResults[0] = UBound($aResults) - 1 Return SetError(0, 0, $aResults) Else Return SetError(1, 0, 0) EndIf Else Return SetError(2, 0, 0) EndIf EndFuncI did not test it! Br, UEZ Edited November 10, 2010 by UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
hassankhokhar11 Posted November 10, 2010 Author Share Posted November 10, 2010 Try this: expandcollapse popup#include <Array.au3> Opt("MustDeclareVars", 1) Dim $aFiles[1] Local $path = "C:\Documents and Settings\hnaveed\Desktop\Profiles" Local $file = ".pcf" $aFiles = File_Seeker($path, $file, $aFiles, 1) ;~ _ArrayDisplay($aFiles) Local $n, $i Local $search = "SDIUseHardwareToken=1" Local $replace = "SDIUseHardwareToken=0" For $i = 1 To UBound($aFiles) - 1 $n = SearchAndReplace($aFiles[$i], $search, $replace) If $n = 0 Then $hFile = FileOpen($aFiles[$i], 1) FileWriteLine($hFile, $replace) FileClose($hFile) ConsoleWrite("Added line " & $replace & " to " & $aFiles[$i] @CRLF) Else ConsoleWrite("Replaced " & $n & " times in file " & $aFiles[$i] & " " & $search & " with " & $replace & @CRLF) EndIf Next Exit ;================================================================================================== ; Function Name: SearchAndReplace($file, $search, $replace, $CaseSensitive = False) ; Description: searchs for a give string and replace it with another string. ; Overwrites file afterwards if replacement > 0! ; Parameter(s): $file filename to search for string ; $search search for string ; $replace replace it with this string ; $CaseSensitive enable / disable case sensitivity ; Return Value(s): amount of replacements ; Version: v0.50 Build 2010-11-08 Beta ; Author(s): UEZ ;================================================================================================== Func SearchAndReplace($file, $search, $replace, $CaseSensitive = False) If Not FileExists($file) Then Return SetError(1, 0, "File not found!") Local $sReplace, $no Local $string = FileRead($file) If @error Then Return SetError(2, 0, "Could not read file!") If $CaseSensitive Then $sReplace = StringRegExpReplace($string, $search, $replace) Else $sReplace = StringRegExpReplace($string, "(?i)" & $search, $replace) EndIf Local $no = @extended If $no > 0 Then Local $hFile = FileOpen($file, 2) If @error Then Return SetError(3, 0, "Could not create file " & $file & "!") FileWrite($hFile, $sReplace) If @error Then Return SetError(4, 0, "Unable to write file!") FileClose($hFile) EndIf Return SetError(0, 0, $no) EndFunc ;================================================================================================== ; Function Name: File_Seeker($path, $file, $array) ; Description: searchs for a file recursively on given drive ; Parameter(s): $path path where to search ; $file filename to search for ; $array to save the search results ; $ext search for file extension, e.g. .mp3, .txt, .log ; $exact to search for exact file name (not case sensitive) ; $rec to search recursively for files and folders ; $folder include also folder names to search pattern ; Return Value(s): an array with the full path of the files, $array[0] = amount of found files ; Version: v0.82 Build 2010-02-13 Beta ; Author(s): UEZ ;================================================================================================== Func File_Seeker($path, $file, ByRef $aResults, $ext = 0, $exact = 0, $rec = 1, $folder = 0) Local $extended If $file <> "" And IsArray($aResults) And BitAND($ext, $exact) <> 1 Then If StringRegExp(StringLeft($path, 1), "(?i)[a-z]") And StringMid($path, 2, 1) = ":" Then If StringRight($path, 1) <> "\" Then $path &= "\" Local $seek, $suffix = "" Local $search = FileFindFirstFile($path & "*") If $search = -1 Then Return SetError(1, 0, 0) EndIf While 1 $seek = FileFindNextFile($search) $extended = @extended If @error Then ExitLoop If $extended And $rec Then File_Seeker($path & $seek & "\", $file, $aResults, $ext, $exact, $rec, $folder) If $exact And Not $ext Then If $seek = $file Then _ArrayAdd($aResults, $path & $seek) Else If $ext Then If StringRegExp($seek, "(?i:\" & $file & "+$\b)") And StringLeft($file, 1) = "." Then If ($extended And $folder) Or Not $extended Then _ArrayAdd($aResults, $path & $seek) EndIf Else If $file = "*" Then If ($extended And $folder) Or Not $extended Then _ArrayAdd($aResults, $path & $seek) Else If StringInStr($file, ".") Then If StringRegExp($seek, "(?i:\" & $file & ")+") Then If ($extended And $folder) Or Not $extended Then _ArrayAdd($aResults, $path & $seek) EndIf Else If StringRegExp($seek, "(?i:" & $file & ")+") Then If ($extended And $folder) Or Not $extended Then _ArrayAdd($aResults, $path & $seek) EndIf EndIf EndIf EndIf EndIf WEnd FileClose($search) $aResults[0] = UBound($aResults) - 1 Return SetError(0, 0, $aResults) Else Return SetError(1, 0, 0) EndIf Else Return SetError(2, 0, 0) EndIf EndFunc I did not test it! Br, UEZ its saying variable $hFile is used without declaration 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