Solved

simple VBS script to output "Hello World!" to a text file

Posted on 2007-03-28
6
495 Views
Last Modified: 2013-02-16
I'm new to VBS, i need a script that when run will add a line output.txt to say what time it was run at.


the bat equivelant would be "echo run at %date% %time% >> output.txt"


many thanks
0
Comment
Question by:detox1978
  • 3
  • 2
6 Comments
 
LVL 2

Author Comment

by:detox1978
ID: 18808092
upped points as i need this quickly.... :-)
0
 
LVL 35

Accepted Solution

by:
mvidas earned 250 total points
ID: 18808132
Hi detox,

You should just be able to use:

 Dim FSO, TS
 Set FSO = CreateObject("scripting.filesystemobject")
 Set TS = FSO.CreateTextFile("C:\Your file.txt")
 TS.WriteLine "Hello world! " & Now
 TS.Close
 Set TS = Nothing
 Set FSO = Nothing

Similarly, instead of CreateTextFile, you could use OpenTextFile:
 Set TS = FSO.OpenTextFile("C:\your file.txt", 8, True)

The 8 there stands for "ForAppending" which would append the written text to the end of the file. You could also use 2 which is "ForWriting". If you only wanted to read the file, use 1 which is ForReading.
The "True" there is for the Create argument, which will create the text file if it doesn't already exist.

Matt
0
 
LVL 10

Expert Comment

by:geodan7
ID: 18808141
This will do exactly what you want.


Dim objFile, strFile, strFilePath
Dim myDateString
myDateString = Date()

strFilePath = "c:\theDate.txt"
Set objFile = CreateObject("Scripting.FileSystemObject")
Set strFile = objFile.CreateTextFile(strFilePath, True)
strFile.WriteLine(myDateString)
strFile.Close
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Author Comment

by:detox1978
ID: 18808241
thanks,

how do i get it to append the date & time (i.e. for the 2nd time run onwards)
0
 
LVL 2

Author Comment

by:detox1978
ID: 18808302
sorry just re-read.

Dim FSO, TS
Set FSO = CreateObject("scripting.filesystemobject")
Set TS = FSO.OpenTextFile("C:\your file.txt", 8, True)
TS.WriteLine "VBS last run " & Now
TS.Close
Set TS = Nothing
Set FSO = Nothing
0
 
LVL 35

Expert Comment

by:mvidas
ID: 18808380
Correct.. You can use "Date" to get only the date, "Time" to get only the time, or "Now" which is the same as Date & " " & Time. See all 3 by using:
 TS.WriteLine "Time: " & Time
 TS.WriteLine "Date: " & Date
 TS.WriteLine "Now: " & Now

VBS doesn't have the Format function, but using string manipulation you can always get a format you want. If you need help getting the format in a certain way, let me know and I'd be glad to help
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now