File object add ReadText Method

Example: Fsutil.openfile(c:xxx).ReadText ([lenght], [encode])

You should be able to do this with the Windows Script Host's ReadAll() (documentation) method of the Scripting.FileSystemObject

Instead of using DOpus to open the file, within your DOpus script, you open a new Scripting.FileSystemObject object and pass the path of the file to it. Then you can read it as a TextStream object. Here is an example from Microsoft's site, which first creates/writes a text file and then reads it (that page also has a VBScript example, it's from the link to the method I posted above).

function ReadAllTextFile()
{
    var ForReading = 1, ForWriting = 2;
    var fso = new ActiveXObject("Scripting.FileSystemObject");

    // Open the file for output.
    var filename = "c:\\testfile.txt";

    var f = fso.OpenTextFile(filename, ForWriting, true);

    // Write to the file.
    f.Write("Header");
    f.Write("1234567890987654321");
    f.Close();

    // Open the file for input.
    f = fso.OpenTextFile(filename, ForReading);

    // Read from the file.
    if (f.AtEndOfStream)
        return ("");
    else
        return (f.ReadAll());
}

I can't help you more, right now, since I am still getting familiar with the DOpus scripting API, but it should be totally easy.

That's the nice thing with a global and generic scripting host: You can use modules and libraries from outside the hosting application.

Now, where you put the text, which has been read in, that's another topic...

1 Like

Have you ever tried reading a file in a Zip compressed file?

EPUBFile = "C:\Users\qiuqi\Downloads\148451.epub"

Dim EPUBDir, XMLDoc, XMLRoot, Elem
Set XMLDoc = CreateObject("Microsoft.XMLDOM")

Set XMLFile = DOpus.FSUtil.OpenFile(EPUBFile & "\META-INF\container.xml")
Set XMLText = DOpus.Create.StringTools
Set XMLBlob = XMLFile.Read()
XMLDoc.Load("C:\Users\qiuqi\Downloads\148451.epub\META-INF\container.xml")
dopus.output "Load form file:" & xmldoc.xml  & vbcrlf & "================" 'null
dopus.output "Decode Blob: " & XMLText.Decode(XMLBlob,"utf-8") & vbcrlf & "================"
XMLDoc.LoadXML(XMLText.Decode(XMLBlob,"utf-8"))
dopus.output "load form string: " & xmldoc.xml & vbcrlf & "================"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Users\qiuqi\Downloads\148451.epub\META-INF\container.xml", 1)
dopus.output objFile.readll

output

Load form file:
================
Decode Blob: <?xml version="1.0"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
   <rootfiles>
      <rootfile full-path="content.opf" media-type="application/oebps-package+xml"/>
   </rootfiles>
</container>
    
================
load form string: <?xml version="1.0"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
	<rootfiles>
		<rootfile full-path="content.opf" media-type="application/oebps-package+xml"/>
	</rootfiles>
</container>

================
发生错误于 38,位置 1
路径未找到 (0x800a004c)

1 Like

No. :slight_smile: At least not via scripting. I usually unarc to %TEMP%, do the work and then rearc.

Can't you use the existing Read method and then convert it to a string using the StringTools object?

Yes, but some files Stringtool files cannot be decoded.

EPUBFile = "C:\Users\qiuqi\Downloads\148451.epub"

Dim EPUBDir, XMLDoc, XMLRoot, Elem
Set XMLDoc = CreateObject("Microsoft.XMLDOM")

Set XMLFile = DOpus.FSUtil.OpenFile(EPUBFile & "\META-INF\container.xml")
Set XMLText = DOpus.Create.StringTools
Set XMLBlob = XMLFile.Read()

XMLDoc.Load("C:\Users\qiuqi\Downloads\148451.epub\META-INF\container.xml")
dopus.output "Load form file:" & vbcrlf & xmldoc.xml  & vbcrlf & "================" 'null
dopus.output "Decode Blob: " & vbcrlf &  XMLText.Decode(XMLBlob,"utf-8") & vbcrlf & "================"
XMLDoc.LoadXML(XMLText.Decode(XMLBlob,"utf-8"))
dopus.output "form xml object: " & VBCRLF & xmldoc.xml & vbcrlf & "================"

Set XMLFile = DOpus.FSUtil.OpenFile(EPUBFile & "\content.opf")
Set XMLText = DOpus.Create.StringTools
Set XMLBlob = XMLFile.Read()

XMLDoc.Load("C:\Users\qiuqi\Downloads\148451.epub\content.opf")
dopus.output "Load form file:" & vbcrlf & xmldoc.xml  & vbcrlf & "================" 'null
dopus.output "Decode Blob: " & vbcrlf &  XMLText.Decode(XMLBlob,"utf-8") & vbcrlf & "================"
XMLDoc.LoadXML(XMLText.Decode(XMLBlob,"utf-8"))
dopus.output "form xml object: " & VBCRLF & xmldoc.xml & vbcrlf & "================"

rename to 148451.epub
148451.epub.zip (6.0 MB)

StringTools cannot decode unicode (BE & LE) text

Scripting.FileSystemObject.OpenTextFile cannot decode UTF-8

ADODB.Stream is inefficient

Therefore, a unified interface is needed to access text files. I very much hope that DOpus has related functions for processing text files.

Fsutil.opentext(c:xxx) return a TextFile object, which has some powerful text processing functions

example:

readline([LineIndex]): read one line, -1 read all line

delline([LineIndex]): delete one line, -1 delete all line

save([filename], [encode]): save the file

......

We'll make it so StringTools can decode UTF-16 in the next version:

1 Like