Solved

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

Posted on 2007-03-28
6
524 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…

856 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