-1

I need to read a text file and copy in other file, but some line contains ASCII non-printable code as NUL, ETX, SOH, etc.

I use ReadLine in this code to copy file and all lines are copyed, but not ASCII line. There is a way to copy also that line?

Set file = fso.GetFile(FileNameIn)

Set OutFile = fso.CreateTextFile(FileNameOut, True)
Set ts = file.OpenAsTextStream(1, -2)

Do Until ts.AtEndOfStream
    strLine = ts.ReadLine
    OutFile.WriteLine(strLine)
Loop
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Vardar
  • 1
  • 3
  • Sounds to me like you have a binary file, not an ASCII file. If that's the case see [this question](http://stackoverflow.com/q/6060529/1630171) about handling binary files in VBScript. – Ansgar Wiechers Oct 11 '16 at 13:57
  • BTW, dumb question: why don't you simply [copy](https://msdn.microsoft.com/en-us/library/e1wf9e7w%28v=vs.84%29.aspx) the file, since you apparently don't want to modify it anyway? – Ansgar Wiechers Oct 11 '16 at 15:57
  • Because i WANT to modify it. I must replce some string with other, and replacement work perfectly. But when i get the BITMAP string, i get error 5 – Vardar Oct 11 '16 at 17:10
  • 1
    In that case see the "Read and write binary file in VBscript" question. Beware that patching binary files by string replacement may not work as you expect. – Ansgar Wiechers Oct 11 '16 at 20:52

1 Answers1

0

Sounds like you need to specify the ASCII format parameter when opening the file (not using system defaults -2).

 Set ts = file.OpenAsTextStream(1,0)

check out for full reference:

MSDN: OpenTextFile Method

MSDN: CreateTextFile Method

Omitting unicode parameter in output file should be OK - its ASCII assumed.

  • Not work.... my ascii string is a bitmap code. When i try to read and write that line Vbscript return error 5. – Vardar Oct 11 '16 at 14:17
  • looks like @Lankymart post is correct - opening file with all parameters specified -- OpenTextFile(FileNameIn, 1, False, 0) – Andre Truter Oct 11 '16 at 14:29
  • Nope... i try to OpenTextFile(FileNameIn, 1, False, 0) for reading and OpenTextFile(FileNameOut, 2, False, 0) for writing, but always i had an error 5 - Invalid procedure call or argument when try to write this line: BITMAP 187,62,10,544,1,}÷ß}÷ß}÷ß}ïÿÿÿÿÿÿÿÿÿÇÿÿÿÿÿÿÿÿÿÃÿÿÿÿÿÿÿÿÿáÿÿÿÿÿÿÿÿÿàÿÿÿÿÿÿÿÿÿ`÷ß}÷ß}÷ß}àÿÿÿÿÿÿÿÿÀ?ÿÿÿÿÿÿÿÿÀÿÿÿÿÿÿÿÿàÿÿÿÿÿÿÿÿàÿÿÿÿÿÿÿÿ`ß}÷ß}÷ß}àÿÿÿÿÿÿÿÿÀÿÿÿÿÿÿÿÿÀ ÿÿÿÿÿÿÿÿà ÿÿÿÿÿÿÿà ?ÿÿÿÿÿÿÿ` }÷ß}÷ß}à ÿÿÿÿÿÿÿÀ ÿÿÿÿÿÿÿÀ 'ÿÿÿÿÿÿÿà ÿÿÿÿÿÿÿà ÿÿÿÿÿÿÿ` ý÷ß}÷ß}à ÿÿÿÿÿÿÿà ÿÿÿÿÿÿÿð ÿÿÿÿÿÿø ?ÿÿÿÿÿÿø ÿÿÿÿÿÿ| – Vardar Oct 11 '16 at 14:45
  • @Vardar looks like your trying to open BITMAP image file which is binary not text follow [@ansgar](http://stackoverflow.com/users/1630171/ansgar-wiechers) advice in [his comment](http://stackoverflow.com/questions/39978757/read-string-with-ascii#comment67236713_39978757) to see how to read binary files with `ADODB.Stream`. – user692942 Oct 11 '16 at 15:39
  • No, BITMAP string is incorpored in a txt file. I want to read the file and save it in another file. But when i reach the BITMAP string i get error 5 – Vardar Oct 11 '16 at 17:14
  • @Vardar all of which is missing from the initial question, so don't be surprised when people try to second guess you. You give no details of the file structure or any errors in the question. Update the question and collate the information you have provided in the comments into a meaningful problem statement. – user692942 Oct 11 '16 at 23:52