Jump to content

Carriage return


Recommended Posts

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

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

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...