Jump to content
Sign in to follow this  
serr57

Carriage return

Recommended Posts

serr57

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

Share this post


Link to post
Share on other sites
Melba23

serr57,

There is a whole lot wrong with that code: :party:

- 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: :mellow:

$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? :P

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
serr57

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

Share this post


Link to post
Share on other sites
Melba23

serr57,

You were modifying the same original string twice and then overwriting the 2 results - you need to keep modifying the same string: :party:

$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. :mellow: 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? :P

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

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.