Solved

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

Posted on 2007-03-28
6
541 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
[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
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
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 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

705 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