writeline junk in first line

M

mcolson

I am using opentextfilewriter to create a batch file and have been
getting some junk on the first line of the file (first line only). It
is not visible when viewing the file. However if I use a Hex Text
Editor, I can see a couple of garbage characters on the first line.
The computer also complains when I try and run the program, saying it
doesn't recognize I can however copy and paste the text into another
batch file and it will work perfectly. What may be causing this
garbage to be created. Here is my code.

Dim file As System.IO.StreamWriter

file = My.Computer.FileSystem.OpenTextFileWriter(strBatchFilePath,
False)
file.WriteLine("@ Echo Off")
file.WriteLine("for /f ""tokens=1-5 delims=/ "" %%d in (""%date%"") do
SET YEAR=%%g")

The garbage I'm seeing is always on the first line. If I append to an
existing file, I do not see this problem. It happens when I choose to
overwrite or create a new file.

I am using Microsoft Visual Basic Express Edition.

Here is the error I get when running the batch file.

C:\>¡É¨[©´@ Echo Off
'¡É¨[©´@' is not recognized as an internal or external command,
operable program or batch file.

If I write a blank line first I get:

C:\>¡É¨[©´
'¡É¨[©´' is not recognized as an internal or external command,
operable program or batch file.

Thanks in advance for any help.
 
M

mcolson

I am using opentextfilewriter to create a batch file and have been
getting some junk on the first line of the file (first line only). It
is not visible when viewing the file. However if I use a Hex Text
Editor, I can see a couple of garbage characters on the first line.
The computer also complains when I try and run the program, saying it
doesn't recognize I can however copy and paste the text into another
batch file and it will work perfectly. What may be causing this
garbage to be created. Here is my code.

Dim file As System.IO.StreamWriter

file = My.Computer.FileSystem.OpenTextFileWriter(strBatchFilePath,
False)
file.WriteLine("@ Echo Off")
file.WriteLine("for /f ""tokens=1-5 delims=/ "" %%d in (""%date%"") do
SET YEAR=%%g")

The garbage I'm seeing is always on the first line. If I append to an
existing file, I do not see this problem. It happens when I choose to
overwrite or create a new file.

I am using Microsoft Visual Basic Express Edition.

Here is the error I get when running the batch file.

C:\>¡É¨[©´@ Echo Off
'¡É¨[©´@' is not recognized as an internal or external command,
operable program or batch file.

If I write a blank line first I get:

C:\>¡É¨[©´
'¡É¨[©´' is not recognized as an internal or external command,
operable program or batch file.

Thanks in advance for any help.

Got it. Default encoding is Encoding to be used in writing to the
file is UTF8. I had to make it ASCII.

file = My.Computer.FileSystem.OpenTextFileWriter(strBatchFilePath,
False, System.Text.Encoding.ASCII)
 
G

Göran Andersson

mcolson said:
I am using opentextfilewriter to create a batch file and have been
getting some junk on the first line of the file (first line only). It
is not visible when viewing the file. However if I use a Hex Text
Editor, I can see a couple of garbage characters on the first line.
The computer also complains when I try and run the program, saying it
doesn't recognize I can however copy and paste the text into another
batch file and it will work perfectly. What may be causing this
garbage to be created. Here is my code.

Dim file As System.IO.StreamWriter

file = My.Computer.FileSystem.OpenTextFileWriter(strBatchFilePath,
False)
file.WriteLine("@ Echo Off")
file.WriteLine("for /f ""tokens=1-5 delims=/ "" %%d in (""%date%"") do
SET YEAR=%%g")

The garbage I'm seeing is always on the first line. If I append to an
existing file, I do not see this problem. It happens when I choose to
overwrite or create a new file.

I am using Microsoft Visual Basic Express Edition.

Here is the error I get when running the batch file.

C:\>∩╗â”@ Echo Off
'∩╗â”@' is not recognized as an internal or external command,
operable program or batch file.

If I write a blank line first I get:

C:\>∩╗â”
'∩╗â”' is not recognized as an internal or external command,
operable program or batch file.

Thanks in advance for any help.

What you see is the BOM, the Byte Order Mark. This is the first
characters in a text file that is encoded using one of the Unicode
encodings.

To write a batch file you should specify that the file should be encoded
using the Encoding.Default encoding.
 
J

Jack Jackson

I am using opentextfilewriter to create a batch file and have been
getting some junk on the first line of the file (first line only). It
is not visible when viewing the file. However if I use a Hex Text
Editor, I can see a couple of garbage characters on the first line.
The computer also complains when I try and run the program, saying it
doesn't recognize I can however copy and paste the text into another
batch file and it will work perfectly. What may be causing this
garbage to be created. Here is my code.

Dim file As System.IO.StreamWriter

file = My.Computer.FileSystem.OpenTextFileWriter(strBatchFilePath,
False)
file.WriteLine("@ Echo Off")
file.WriteLine("for /f ""tokens=1-5 delims=/ "" %%d in (""%date%"") do
SET YEAR=%%g")

The garbage I'm seeing is always on the first line. If I append to an
existing file, I do not see this problem. It happens when I choose to
overwrite or create a new file.

I am using Microsoft Visual Basic Express Edition.

Here is the error I get when running the batch file.

C:\>???[??@ Echo Off
'???[??@' is not recognized as an internal or external command,
operable program or batch file.

If I write a blank line first I get:

C:\>???[??
'???[??' is not recognized as an internal or external command,
operable program or batch file.

Thanks in advance for any help.

The default encoding for OpenTextFileWriter is UTF-8, which is Unicode
(2 bytes per character). You probably want to use the constructor
that has three parameters and supply ASCIIEncoding for the third
parameter.

The "garbage" you see at the beginning of the file is a special
Unicode character that tells applications such as Notepad that the
file contains two bytes per character so they can properly handle the
file.
 
G

Göran Andersson

Jack said:
The default encoding for OpenTextFileWriter is UTF-8, which is Unicode
(2 bytes per character).

That's UTF-16. UTF-8 stores the most common characters in one byte, and
other characters in several bytes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top