Why does VBS not read this text file correctly? - vbscript

I have the following code to read a text file:
Option Explicit
Dim InputFile
Dim FSO, oFile
Dim strData
InputFile = "C:\Program Files (x86)\AVG\CloudCare\ClientVersion.txt"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFile = FSO.OpenTextFile(InputFile)
strData = oFile.ReadAll
oFile.Close
msgbox strData
The contents of ClientVersion.txt is:
CLIENT_VERSION_STRING _T("3.5.2") //
When I run the VBS code, I get back this:
If I create a new text file with the same content in the same location, it works fine. Is there a reason why VBS is unable to read this simple text file? I couldn't see any issues with permissions on the file.

ÿþ is the byte order mark of a UTF-16 Little Endian encoded file. UTF-16 (unlike ASCII/ANSI) uses two bytes for a character instead of just one. However, the OpenTextFile method reads files as ASCII files by default, so each 2-byte character gets interpreted as two separate characters.
From the documentation:
Syntax
object.OpenTextFile(filename[, iomode[, create[, format]]])
Arguments
[…]
format
Optional. One of three Tristate values used to indicate the format of the opened file (TristateTrue = -1 to open the file as Unicode, TristateFalse = 0 to open the file as ASCII, TristateUseDefault = -2 to open the file as the system default). If omitted, the file is opened as ASCII.
Specify the proper encoding when reading the file and the problem will disappear:
Set oFile = FSO.OpenTextFile(InputFile, 1, False, -1)

Related

How can I save non-Latin characters in a text file via VBScript? [duplicate]

This question already has answers here:
Write Chinese chars to a text file using vbscript
(3 answers)
Closed 4 years ago.
Chinese characters cannot be saved in a text file via VBScript.
The VBScript is in a folder whose name is in Chinese: 视窗. The script will create a text file, in which the current working directory will be shown. The Chinese characters cannot be saved in the file. Windows Script Host says "Error: Invalid procedure call or argument". The error will not arise if the folder name is in English.
Path = CreateObject("WScript.Shell").CurrentDirectory
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(Path & "\Testing.txt", 8, True)
objFile.WriteLine Path
objFile.close
Is it possible for VBScript to save a file path that contains Chinese characters?
The method openTextFile has another optional parameter - format. Its value is 0 by default which opens the file in ASCII format. To save the chinese characters in the file, you can open the file in UNICODE format by specifying the value of the parameter format = -1. Here is the Reference.
objFso.openTextFile(path,8, true, -1) '-1 = TriStateTrue = Opens the file as Unicode
path = split(wscript.scriptFullName, wscript.scriptname)(0) & "Testing.txt"
set objFso = createObject("scripting.filesystemobject")
set objFile = objFso.openTextFile(path,8, true, -1)
objFile.write path
objFile.Close
set objFile = Nothing
set objFso = Nothing

Physically opening a text file

I have a text file C:\user\test.txt with the text "This is a test", and I want to physically open the file using VBScript (as if i were to click Accesories → Notepad). I want to see the file open on my screen so I can visually read the text.
Now, for some reason I can't. This is what I'm trying (I tried with and without the "textfile.close" line, and yes, the file exists in that path):
dim FS1, textfile
Const ForReading = 1
set FS1 = CreateObject("Scripting.FileSystemObject")
set textfile = FS1.OpenTextFile("C:\user\test.txt", ForReading, True)
textfile.close
What am I missing? I have no issue creating, writing, or appending... but I just can't open it!
If you want to open the file in Notepad:
With CreateObject("WScript.Shell")
.Run "notepad.exe c:\user\test.txt"
End With
You can also just run the file and it will open in your default txt editor (whatever Windows file association you have established for extension txt).
With CreateObject("WScript.Shell")
.Run "c:\user\test.txt"
End With
Are you reading the file?
You should be doing something like
' Open the file for input.
Set MyFile = fso.OpenTextFile(FileName, ForReading)
' Read from the file and display the results.
Do While MyFile.AtEndOfStream <> True
TextLine = MyFile.ReadLine
Document.Write TextLine & "<br />"
Loop
MyFile.Close
https://msdn.microsoft.com/en-us/library/314cz14s(v=vs.84).aspx

Inserting Lines into a Non-Text File

I am trying to insert lines into the middle of a non-text file (the extension of the file is "dxf"). I am using vbscript to do this.
Everywhere I have look, I come across the FileSystemObject.OpenTextFile. However, when I try to use this on a dxf file, it is causing an error: Exception 80070057 (I believe this is an invalid file) .
Here is my code:
Dim file
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
If fileexists(dxfFile$) Then
Set file = fso.OpenTextFile(dxfPath, ForAppending, True)
file.WriteLine("<PORTLIST TESTING>ASDFLKJ")
file.Close
End If
dxfFile$ is not a valid VBscript variable name; use dxfFile, file or dfxPath (consistently)
FileExists is a method of the FileSystemObject; you need to call fso.FileExists
Neither dxfFile, nor dfxPath, nor ForAppending are defined
Calling .OpenTextFile with an undefined/empty first/filespec parameter throws an error 5 - Invalid procedure call or argument
You can't insert lines by appending them; modifying files 'in the middle' is especially clumsy in VBScript; loading the whole file into memory, editing, writing it back may work for you
.DFX file come in ASCII or binary format; if the latter, you can't use the FileSystemObject (see ADODB.Stream)

Using VB Script to read contents of .msg file

I have a folder with thousands of Outlook .msg files.
I'd like to know if it's possible to write a VB Script that can read the sender and receiver from each file, and move the .msg file to a folder based on this info?
Thanks
You shouldn't ask yes/no questions unless you expect the answer to be either "yes" or "no".
Set ol = CreateObject("Outlook.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
For Each f In fso.GetFolder("C:\some\folder").Files
If LCase(fso.GetExtensionName(f)) = "msg" Then
Set msg = ol.CreateItemFromTemplate(f.Path)
WScript.Echo msg.Sender.Name
For Each rcpt In msg.Recipients
WScript.Echo rcpt.Name
Next
End If
Next
For reading the contents of a .msg file, I have used the following approach.
use CreateItemFromTemplate for the msg file using the outlook object
Use this outlook object to save the data into a .txt file
Once the .txt file is created read it and use the data as required
Script:
Dim OL : Set OL=CreateObject("Outlook.Application")
Dim Msg ':Set Msg= CreateObject("Outlook.MailItem")
Set Msg = OL.CreateItemFromTemplate("C:\test.msg")
'MsgBox Msg.Subject
Msg.saveAs "C:\test.txt", olDoc
'The above statement will save the contents of .msg file into the designate .txt file
Set OL = Nothing
Set Msg = Nothing
Once the .txt file is created use it as required for your computations.

How to parse one text file to multiple files based on data contained within

I have an enormous text file that I'd like to parse into other files - I think the only thing I have on this box (company computer) to use is VBS, so here's my question:
I have text file with a bunch of network configurations that looks like this:
"Active","HostName","IPAddress"
!
.......
end
This repeats itself throughout the file, but obviously for each configuration different data will occur within the "..." It always says "Active" for each configuration as well.
I want to create save files of the type HostName.cfg for each configuration and save all of the text between and including the ! and "end" . The line with the three quoted attributes doesn't need to be copied.
I'm still learning VBS so I'd appreciate any help in the matter. Thanks!
Here are some useful file reading functions and statements:
Freefile
Returns an integer which you should assign to a variable and then use in an Open statement.
Open <string> For Input As <integer>
Opens the specified file using the specified file handle. Don't use the same file handle twice without closing it in between.
Line Input #<integer>, <variable>
Reads one line of the file into a string.
Input #<integer>, <variable>, <variable>, <variable>
Reads one line of the file delimited by commas into several variables.
I once had to read a text file while omitting starting and ending lines. Though I didn't need to write it anywhere, FSO is simple enough that you will be able to figure it out. Here's some code for reading a file and link for writing to file via FSO: link
'Create a FSO object and open the file by providing the file path
Dim fso, f, filePath, line
filePath = "test.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filePath)
'Skip through the first two lines
f.readline
f.readline
'Read till the end of file, if the line is not "end", split it based on ","
while not f.AtEndOfStream
line = f.readline()
if line <> "end" then
arr = split(line, ",")
'Write the arr to file
end if
wend
f.Close

Resources