qwert Posted March 15, 2019 Posted March 15, 2019 I recall seeing an example once, but I can't locate it. I need to parse and RTF file for some key phrases and then replace them and/or grab that content. But I'm working from a script that doesn't use a GUI. Can someone suggest a method or point me to an example? Thanks in advance for any help.
orbs Posted March 15, 2019 Posted March 15, 2019 RTF is a plain text file, you can process it like any other text file. however if you suspect your key phrases might be used in the RTF settings (which are also stored as plain text), you can create a GUI (yet leave it hidden) with a RichEdit control, then use _GUICtrlRichEdit_StreamFromFile() and _GUICtrlRichEdit_GetText(). you can also investigate these function in the UDF, to see if you can eliminate the reference to the RichEdit control. Signature - my forum contributions: Spoiler UDF: LFN - support for long file names (over 260 characters) InputImpose - impose valid characters in an input control TimeConvert - convert UTC to/from local time and/or reformat the string representation AMF - accept multiple files from Windows Explorer context menu DateDuration - literal description of the difference between given dates WinPose - simultaneous fluent move and resize Apps: Touch - set the "modified" timestamp of a file to current time Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes SPDiff - Single-Pane Text Diff Magic Math - a math puzzle Demos: Title Bar Menu - click the window title to pop-up a menu
water Posted March 15, 2019 Posted March 15, 2019 RTF can be processed by MS Word. Then use the Word UDF to do all the needed changes and save the file as RTF. My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
qwert Posted March 15, 2019 Author Posted March 15, 2019 Thanks for both responses. Quote RTF is a plain text file, you can process it like any other text file Initially, I made that assumption, but I got unintelligible results when I read in the file. The documentation implies that AutoIt will determine file encoding: Quote the content of the file is examined and a guess is made whether the file is UTF8, UTF16 or ANSI But now that you've mentioned it, I inspected the file ... found it was ANSI ... and added $FO_ANSI to my FileOpen call. I can now read the plain text. @water, that's good to know. I'll keep it in mind for any file processing that's more complicated than what I'm attempting. But, for now, I'm looking for the very simplest method. It looks like that is going to be Read ANSI ... Replace Text ... Write ANSI. My concern is that I'll encounter an RTF that isn't ANSI. It would be better if AutoIt could handle that part.
Skysnake Posted March 27, 2019 Posted March 27, 2019 (edited) I am not getting anywhere... myRTF.rtf ; Create a constant variable in Local scope of the filepath that will be read/written to. Local Const $sFilePath = @ScriptDir & "\" & "myRTF.rtf" ; Open the file for reading and store the handle to a variable. Local $hFileOpen = FileOpen($sFilePath, $FO_ANSI ) ; + $FO_OVERWRITE If $hFileOpen = -1 Then MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.") Return False EndIf ; Read the contents of the file using the handle returned by FileOpen. Local $sFileRead = FileRead($hFileOpen) ConsoleWrite("$sFileRead " & $sFileRead & @CRLF) ;; \hich\af31506\dbch\af31505\loch\f31506 old text replacethis in the document of my choice.} Local $sString = StringReplace($sFileRead, "replacethis", "The name is Bond, James Bond", 0, 0) ConsoleWrite("@extended " & @extended & @CRLF) ConsoleWrite("$sString " & $sString & @CRLF) ; Write data to the file using the handle returned by FileOpen. FileWrite($hFileOpen, $sString) Local $ERROR = @error Local $EXTENDED = @extended ConsoleWrite($ERROR) ConsoleWrite($EXTENDED) ; Close the handle returned by FileOpen. FileClose($hFileOpen) I get a consolewrite that shows the text has been replaced, but I cannot get the FileWrite to write the new content. Extended 1 as expected \f31506\fs22\lang1033\langfe1033\cgrid\langnp1033\langfenp1033 {\rtlch\fcs1 \af31507 \ltrch\fcs0 \insrsid1787831 This is the old text The name is Bond, James Bond with something better}{\rtlch\fcs1 \af31507 \ltrch\fcs0 \insrsid3810039 I consistently get Failure: 0 if file not opened in writemode, file is read only, or file cannot otherwise be written to. But it is opened as ANSI ? I miss something? Edited March 27, 2019 by Skysnake Skysnake Why is the snake in the sky?
jchd Posted March 27, 2019 Posted March 27, 2019 Writing to a file open in read mode? This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)
Skysnake Posted March 27, 2019 Posted March 27, 2019 1 hour ago, jchd said: Writing to a file open in read mode? Looks like But the documentation says ANSI flag is read + write. If I add 2 + ANSI, it errors out. Skysnake Why is the snake in the sky?
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