Jump to content
oemript

How to inital a file before FileWrite?

Recommended Posts

oemript

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

 

Share this post


Link to post
Share on other sites
oemript
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^)

 

Share this post


Link to post
Share on other sites
xCROv
Posted (edited)

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

Share this post


Link to post
Share on other sites
oemript
Posted (edited)

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

Share this post


Link to post
Share on other sites
jchd

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)

Share this post


Link to post
Share on other sites
rudi
Posted (edited)

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
  • Thanks 1

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

Share this post


Link to post
Share on other sites
oemript
Posted (edited)

I would like to know on how to assign different encoding for export text file.

Do you have any suggestions?

Thanks, to everyone very much for suggestions (^v^)

Edited by oemript

Share this post


Link to post
Share on other sites
rudi

Hi.

Please give an example of the STRING you have to process, and how you expect the CSV line in your export file to look like.

 

Regards, Rudi.


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

Share this post


Link to post
Share on other sites
oemript
Posted (edited)

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

Share this post


Link to post
Share on other sites
Jos
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

 


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
oemript

I would look into it

Thanks, to everyone very much for suggestions (^v^)

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

×