1

I guess my attempt failed for two reasons:

  1. My programming skills are limited to a few months of on the job coding-retraining before I dropped out, none of that is in VBscript or even in an OOP language and;

  2. I'm a dummy :) }

=======================================================

My original goal was to open a text file (wincmd.ini), do a straight (=unconditional) search and replace on twice (lines 11 and 12), save the same file and close it. This should happen at Windows-startup. I tried to use DOS batch files, but no joy. This VBscript seems to work, but I'm still getting the following error for this code. Please could you advise where it is wrong? Line 9 is "strText = objFile.ReadAll".

error: Line 9 | Character 1 | Error: Input past end of file | Code: 800A003E | Source: Microsoft VBScript runtime error.

Const ForReading = 1
Const ForWriting = 2
Const FileIn = "C:\totalcmd852\wincmd.ini"
Const FileOut = "C:\totalcmd852\wincmd.ini"  '<== Change to prevent overwrite of original file

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(FileIn, ForReading)

strText = objFile.ReadAll 
objFile.Close
strNewText = Replace(strText, "D:\totalcmd852\aWin10.bar", "C:\totalcmd852\aWin7.bar")
strNewText = Replace(strText, "D:\", "C:\")

Set objFile = objFSO.OpenTextFile(FileOut, ForWriting)
objFile.WriteLine strNewText
objFile.Close

What I tried: I looked at other solutions here and elsewhere, so I did figure out this is clearly, the infamous ReadAll error. It's trying to read something which isn't there. But ...... the problem is, the wincmd.ini-file is TOO, there. Please refer me to other solutions, and feel free to tell me what obvious thing I missed, but really: NONE of them seemed applicable to me. Then again, I've got very, very, very limited coding experience.

EDIT: The suggested answer did not fit, because: For some reason, each time I boot up, the file question is reduced to zero length. This produced the error.

user692942
  • 16,398
  • 7
  • 76
  • 175
  • Don't use `ReadAll` it's fraught with issues not to mention efficient on larger files. Use `OpenAsTextStream()` to use the `TextStream` object to stream the read, also can use it for the write. – user692942 Apr 21 '20 at 10:02
  • 1
    it might, but I am really suffering from a lack of explanatory comments with each line of code. Meaning, I do guess what each line means, but I am not sure. But thanks, you've given me something which seems to make sense. – Hecate Newb Apr 21 '20 at 10:06
  • Should I change the line **strText = objFile.ReadAll** to **strText = objFile.OpenAsTextStream()**?? And how do I use the TextStream object for writing? Thanks! – Hecate Newb Apr 21 '20 at 10:11
  • Okay, no. I tried to replace that, and it got me the error 800A01B6 ("object doesn't support this property or menthod"). Yep, me stupid. I will return to your above link....... sigh – Hecate Newb Apr 21 '20 at 10:16
  • There are examples of how to use it in the duplicate question, you will need to create an object reference to the `TextStream` and then loop through it reading the file in chunks. – user692942 Apr 21 '20 at 10:21
  • 1
    The `ReadAll` method fails for an open file of zero length throwing the _Input past end of file_ error. Try `Set f = objFSO.GetFile(FileIn)` and then check `f.Size` property. – JosefZ Apr 21 '20 at 10:59
  • @Lankymais this is a tiny .ini file, 40k, so .... chunks??? – Hecate Newb Apr 21 '20 at 12:08
  • 1
    @JosefZ Okay, so it appears that the wincmd.ini file **indeed** HAD zero length!?!?!??!? I am bewildered as to how this came to be. I replaced it and the VBscript error did not occur. I'm baffled as to what happened. – Hecate Newb Apr 21 '20 at 12:11
  • Good spot @JosefZ. The stream approach is for bigger files, but still worth learning about not a fan of `ReadAll`. – user692942 Apr 21 '20 at 15:22
  • Does this answer your question? [VBscript error input past end of file](https://stackoverflow.com/a/26032631) – user692942 Apr 21 '20 at 15:23
  • Does this answer your question? [Read and write into a file using VBScript](https://stackoverflow.com/questions/1142678/read-and-write-into-a-file-using-vbscript) – M-- Apr 22 '20 at 19:17

0 Answers0