Jump to content

How to inital a file before FileWrite?


Recommended Posts

Referring to following coding, $oMeta.GetAttribute('content') should insert a single line within text file.  I would like to know on how to inital a file before FileWrite, so there is only a single line within text file.  Sometimes, it gets a second line with "".

$sURL = "www.sample.com"
Local $oIE = _IECreate($sURL)
$oMeta = _IEGetObjByName($oIE, 'og:url')
;ConsoleWrite($oMeta.GetAttribute('content') & @CRLF)
;MsgBox(0, "URL", StringReplace( $oMeta.GetAttribute('content') & @CRLF, "html/","html"))
Local $hFileOpen = FileOpen("D:\Import.txt", 2)
FileWrite($hFileOpen, StringReplace( $oMeta.GetAttribute('content') & @CRLF, "html/","html"))
FileClose($hFileOpen)

Does anyone have any suggestions on how to initial D:\Import.txt file and only insert a single line into file as well?
Thanks in advance for any suggestions

 

Link to comment
Share on other sites

4 hours ago, JohnOne said:

Perhaps replace @CR and @LF individually, before write.

Or Regex replace trailing whitespace.

I would like to know on whether it is necessary to add @CRLF or not, if I only export a single string into text file.

Do you have any suggestions?
Thank you very much for any suggestions (^v^)

 

Link to comment
Share on other sites

Are you talking about creating the file by saying initial? Because FileWrite will create the file if it doesn't exist already. May need to clarify it a bit more so I understand. :P

Edited by xCROv
Link to comment
Share on other sites

FileWrite is suitable to my situation, which would create a file on existed and non-existed situation,

FileWrite($hFileOpen, StringReplace( $oMeta.GetAttribute('content'), "html/","html"))

When I insert text into text file, I prefer only include the content without any special char at all.  Such as if text contains "ABC" then only insert "ABC" into specific text file, that is all.  Considering FileWrite need to export text into existing file, so what to do in order to erase all texts within text file and insert "ABC" into it, so the specific text file only contain "ABC" under this situation.

Do you have any suggestions?
Thanks, to everyone very much for any suggestions (^v^)

 

Edited by oemript
Link to comment
Share on other sites

FileDelete the file before FileWrite to it.

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 here
RegExp tutorial: enough to get started
PCRE 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)

Link to comment
Share on other sites

Hello,

$hFileOpen = FileOpen("D:\Import.txt", 2)

... will open an empty file for write. This means, in case the file already exists, its content will be erased. If it didn't exist already, a new file will be created.

FileWrite($hFileOpen,$YourString)

... will write your string to that file without adding a @CRLF at the end of the line that string was written to.

 

 

In case you see a trailing empty line in your file, the string $YourString has a "trailing LINEFEED" at it's end. To cleanup that one use either StringStripWS() or StringRegExReplace() before writing the string to your file.

 

You can use ...

ConsoleWrite("""" & $YourString & """" & @CRLF)

... to see in the output pane of SciTE the result of writing your string to your file

 

Regards, Rudi.

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

If I prefer big-5 encoding instead of utf-8, what kind of coding should be inserted before FileWrite?

Big-5.png

Do you have any suggestions?

Thanks, to everyone for any suggestions (^v^)

Edited by oemript
Link to comment
Share on other sites

  • Developers
3 hours ago, oemript said:

what kind of coding should be inserted before FileWrite?

Have you tried with your originally posted code setting the appropriate value for parameter 2, thus including  

$FO_UTF16_BE_NOBOM (2048) = Use Unicode UTF16 Big Endian (without BOM) reading and writing mode.

Jos

 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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...