[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2007-03-28
6
Medium Priority
?
552 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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 script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

650 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