serr57 Posted June 28, 2010 Share Posted June 28, 2010 Hi, every body, I would like to add a carriage return when a string is found. I have tried StringAddCR function, but nothing change. $file = "C:\TEMP\MMarshal\MMarshalConfig.xml" $open = fileopen($file,0) $read = fileread($open) FileClose($open) $CR = StringAddCR ("/>") FileWrite($file, $CR) Could you help me Many thanks Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted June 28, 2010 Moderators Share Posted June 28, 2010 serr57,There is a whole lot wrong with that code: - 1. $open = fileopen($file,0) - you are only opening the file for reading, so you cannot write to it.- 2. FileClose($open) - You are closing the file before trying to write to it, so the write would fail even if you had opened the file correctly.- 3. $CR = StringAddCR ("/>") - This line will do nothing at all. Look at the Help file for StringAddCR:StringAddCR - Takes a string and prefixes all linefeed characters ( Chr(10) ) with a carriage return character ( Chr(13) ).You have no linefeed characters, so you get no added carriage returns.; ------So.....what exactly do you want to do? Start a new line after each "/>" in the file?If so then this should work for you: $sFile = "Your_file_name" ; Open file for read $hFile = FileOpen($sFile, 0) ; Read file $sContent = FileRead($hFile) ; Close file FileClose($hFile) ; Add the new line characters $sNewContent = StringReplace($sContent, "/>", "/>" & @CRLF) ; Reopen the file for overwrite $hFile = FileOpen($sFile, 2) ; Write file FileWrite($hFile, $sNewContent) ; Close file FileClose($hFile)Before: />/>/> After: /> /> />Does that do what you want? 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...
serr57 Posted June 28, 2010 Author Share Posted June 28, 2010 Hi, Many thanks. The second thing is to modify with 2 strings. FileCopy ("C:\TEMP\MMarshal\MMarshalConfig.xml", "C:\TEMP\MMarshal\MMarshalConfigOr.xml") sleep(5000) $file = "C:\TEMP\MMarshal\MMarshalConfig.xml" $open = fileopen($file,0) $read = fileread($open) FileClose($open) $CR1 = StringReplace($read, "/>", "/>" & @CRLF) $CR2 = StringReplace($read, "</Key>", "</Key>" & @CRLF) $open = fileopen($file,2) FileWrite($file, $CR1) FileWrite($file, $CR2) FileClose($file) But only one is written Don't know why. I am not an expert with autoit. Before I use it only for application deployment thanks Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted June 28, 2010 Moderators Share Posted June 28, 2010 serr57,You were modifying the same original string twice and then overwriting the 2 results - you need to keep modifying the same string: $file = "Your_file_path" $open = fileopen($file,0) $read = fileread($open) FileClose($open) ; Do the first replace $CR1 = StringReplace($read, "/>", "/>" & @CRLF) ; Do the second replace - ON THE STRING WHICH HAS ALREADY BEEN AMENDED $CR2 = StringReplace($CR1, "</Key>", "</Key>" & @CRLF) $open = fileopen($file,2) ; Only write the final string FileWrite($open, $CR2) FileClose($open)You should also use either filenames or file handles - you were mixing them in your script when writing the file. From the Help file:"Note: Do not mix filehandles and filenames, i.e., don't FileOpen a file and then use a filename in this function. Either use filehandles or filenames in your routines, not both."All clear? 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...
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