?
Solved

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

Posted on 2007-03-28
6
Medium Priority
?
546 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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

765 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