pikameuh
pikameuh

Reputation: 159

VBScript create and open a new file

I try to make a script in VBScript for PowerAMC. And I've got an error.

I checked all elements to make a file with the content (XSD file):

private Sub writeInFile(pathFolder, pathFile, val)
  Output "WriteInFile["&pathFolder&pathFile&"]"
  Dim fso, MyFile
  Set fso = CreateObject("Scripting.FileSystemObject")

  Set MyFile = fso.CreateTextFile(pathFolder&pathFile, true)
  If (fso.FileExists(pathFolder&pathFile)) Then
    MyFile.WriteLine(val)
  Else
    ouput "File can't be create"
  End If

  MyFile.Close
  end Sub

And the file exists with good content, but if I try to read it with:

public Function readFile(path)
  'Declare variables
  Dim objFSO, objReadFile, contents

  'Set Objects
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objReadFile = objFSO.OpenTextFile(path, 1, false)

  'Read file contents
  contents = objReadFile.ReadAll

  'Close file
  objReadFile.close

  'Cleanup objects
  Set objFSO = Nothing
  Set objReadFile = Nothing

  readFile = contents
   End Function

I get that : "ÿþ<" for only content, but if I try to read a file that is not created by the previous function, it runs perfectly.

Upvotes: 1

Views: 500

Answers (1)

Hackoo
Hackoo

Reputation: 18827

I think the problem comes from Unicode format, take a look at this => FileSystemObject - Reading Unicode Files

Upvotes: 1

Related Questions