Solved

Create text file from VBSript

Posted on 2009-05-14
7
840 Views
Last Modified: 2012-05-07
I have been tasked with writing a VBScript that is being used to determine if our corporate antivirus definitions are over 7 days old.  This will be  used by our management platform to automatically generate an alert.  I have been able to get it where it generates the output into a pop up window, but I need it to create a text file called vipreage.txt on the C: drive.  I have been trying  to find an answer to this but scripting has never been a strong suit of mine.  Any help would be greatly appreciated.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("c:\Program Files\Sunbelt Software\SBEAgent\Definitions\defver.txt")
wscript.echo DateDiff("d", objFile.DateLastModified, Now())

Open in new window

0
Comment
Question by:sdipietro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 5

Expert Comment

by:satheeshm
ID: 24388333
Hi,

You are near to solution. you can create text file using file system object. Check the below code sample.

dim fs,fname
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fname=fs.CreateTextFile("c:\test.txt",true)
fname.WriteLine("Hello World!")
fname.Close
set fname=nothing
set fs=nothing
0
 

Author Comment

by:sdipietro
ID: 24388361
satheeshm,

Would that just be added below the existing text?
0
 
LVL 9

Assisted Solution

by:gregcmcse
gregcmcse earned 62 total points
ID: 24388478
Replace the "wscript.echo" line with these lines:
set myFile = objFSO.CreateTextFile("c:\vipreage.txt",True)
myFile.WriteLine DateDiff("d", objFile.DateLastModified, Now())
myFile.Close
set myFile = Nothing
 
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Accepted Solution

by:
satheeshm earned 63 total points
ID: 24388517
no.. it creates a new file. you want create new file or you want to append to an existing text file.

to create a new file use the below code. I added code to your existing code.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("c:\Program Files\Sunbelt Software\SBEAgent\Definitions\defver.txt")
wscript.echo DateDiff("d", objFile.DateLastModified, Now())
dim fname
set fname=fs.CreateTextFile("c:\vipreage.txt",true)
fname.WriteLine("Hello World!") \\ Change this to what ever you want to write into the file
fname.Close
set fname=nothing
set objFSO=nothing
0
 
LVL 5

Expert Comment

by:satheeshm
ID: 24388527
okay.. if you dont want alert remove the echo from that code
0
 
LVL 9

Expert Comment

by:gregcmcse
ID: 24388668
If you want to append to your output file rather than overwrite it everytime, change it to this:
ForAppending = 8
set myFile = objFSO.OpenTextFile("c:\vipreage.txt", ForAppending,True)
myFile.WriteLine DateDiff("d", objFile.DateLastModified, Now())
myFile.Close
set myFile = Nothing
0
 

Author Comment

by:sdipietro
ID: 24420954
Thanks Guys!  I was able to get the script working by using what you both gave me.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question