-1

The next title will replace the previous title on the same line. As a result, only the last one of the titles will finally remain in the text file.

My following VBScript will find a number of update titles (="Title" in the script). Each "Title" will be shown in the output file "c:\Testing\testing.txt". One title will be shown on the first line at a time. The command "Next" will bring up the next title, which will be shown on the same line when the previous title disappears. Simply put, the next title will replace the previous title on the first line. As a result, only the last one of the titles will finally remain in the file. Is it possible to add an empty line between two titles, so that all titles will be shown in the file?

Set Job = CreateObject("Microsoft.Update.Session")
Set Tool = Job.CreateupdateSearcher() 
Set Result = Tool.Search("IsInstalled=0 and IsHidden=0")
For Number = 0 To Result.Updates.Count-1
    Set Title = Result.Updates.Item(Number)

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    outFile = "c:\Testing\testing.txt"
    Set objFile = objFSO.CreateTextFile(outFile, True)
    objFile.Write Title & vbCrLf 
    objFile.Close
Next

I want the titles shown on different lines rather than on the same line.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Matthew Wai
  • 962
  • 7
  • 14
  • 1
    Possible duplicate of [How to create text file and write to it in vbscript](https://stackoverflow.com/questions/34045891/how-to-create-text-file-and-write-to-it-in-vbscript) – user692942 Feb 04 '19 at 07:14

2 Answers2

0

I just found that both OpenTextFile and 8, True are not necessary in the following script, which works at my end.

Set Job = CreateObject("Microsoft.Update.Session")
Set Tool = Job.CreateupdateSearcher() 
Set Result = Tool.Search("IsInstalled=0 and IsHidden=0")
Set X = CreateObject("Scripting.FileSystemObject")
Set Z = X.CreateTextFile("Updates_found.txt")

For Number = 0 To Result.Updates.Count-1
Set Title = Result.Updates.Item(Number)
Z.Writeline Title
Next
Matthew Wai
  • 962
  • 7
  • 14
-1
Set objFSO  = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("c:\Testing\testing.txt", True)  'True = overwrite existing file
objFile.Close
Set objFile = objFSO.OpenTextFile("c:\Testing\testing.txt", 8)       '8 = For Appending
objFile.WriteLine ("Title")
objFile.WriteLine ("Title2")
objFile.Close

Hope this helps!

  • @ flipdascript, an error arose when I ran it. The error is on this line `Set objFile = objFSO.OpenTextFile(""c:\Testing\testing.txt", 8)`. Windows Script Host said "**Line: 9. Char: 37. Error: Expected ')'. Code: 800A03EE. Source: Microsoft VBScript compilation error.**" – Matthew Wai Feb 03 '19 at 16:05
  • @MatthewWai In that line you are escaping`"` with another `"`. Just remove one `"` to avoid that error – Gurmanjot Singh Feb 03 '19 at 16:10
  • @ Gurman, Done as you said, but another error arose on the same line. Windows Script Host said "**Line: 9. Char: 1. Error: Permission denied. Code: 800A0046. Source: Microsoft VBScript runtime error**". – Matthew Wai Feb 03 '19 at 16:16
  • 1
    @MatthewWai That's because the 2nd statement already opened the file, so the 3rd statement can't open it again. Remove the `CreateTextFile` line from the code (as it would still truncate the file) and change the `OpenTextFile` statement to `Set objFile = objFSO.OpenTextFile("c:\Testing\testing.txt", 8, True)`. That will create the file if it's missing and open it for appending. – Ansgar Wiechers Feb 03 '19 at 16:24
  • Thanks to all of you! It works now. One more question: how can I change the output folder **c:\Testing\** to the folder containing the VBScript file? – Matthew Wai Feb 03 '19 at 16:43
  • Do you mean replacing "c:\Testing" with "ScriptFullName"? – Matthew Wai Feb 03 '19 at 16:51
  • 2
    *\*sigh\** It never ceases to amaze me how answers with obviously broken code can still attract upvotes ... – Ansgar Wiechers Feb 03 '19 at 16:51
  • 1
    @MatthewWai Use the `BuildPath` method for constructing a full path from the value of `WScript.ScriptFullName' and the filename. – Ansgar Wiechers Feb 03 '19 at 16:53
  • Sorry, I don't know what BuildPath is. Can you simply change the script in the answer, so that "C:\Testing" will be changed to the parent folder? – Matthew Wai Feb 03 '19 at 16:54
  • 1
    I could. But since you're here to learn, I'll refer you to the [documentation](https://learn.microsoft.com/en-us/previous-versions//z0z2z1zt%28v%3dvs.85%29) instead. – Ansgar Wiechers Feb 03 '19 at 17:01
  • 1
    Then I suggest you hire someone to do the work for you. Here on SO we're willing to help, but we still do expect *you* to do the legwork. – Ansgar Wiechers Feb 03 '19 at 17:09
  • Thank you for your guidance and encouragement. I found the answer at [link](https://stackoverflow.com/a/16145760/7397743) The "CurrentDirectory" method was used there. – Matthew Wai Feb 03 '19 at 17:46
  • @MatthewWai `CurrentDirectory` is the current working directory, which is not necessarily the same directory as the location of the script. – Ansgar Wiechers Feb 03 '19 at 19:07
  • Sorry, I forgot to say that the script will only be run in its parent folder, so `CurrentDirectory` will do the job. – Matthew Wai Feb 04 '19 at 15:26
  • Sorry for that one extra " and thanks for correcting it – flipdascript Feb 05 '19 at 11:25
  • I gave your answer an upvote, which was replaced with a downvote. I wonder why. – Matthew Wai Feb 05 '19 at 12:55
  • 1
    @MatthewWai One of the downvotes on the answer is from me, because the code in it is still broken. And you shouldn't upvote (or accept) answers that are factually incorrect. – Ansgar Wiechers Feb 06 '19 at 14:50
  • code corrected. Sorry for that...I think I'll never post anything again while in a hurry ;) – flipdascript Feb 08 '19 at 09:06