Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change the OS Date/Time 2

Status
Not open for further replies.

stevepuri

Programmer
Mar 30, 2001
24
0
0
US
Does anyone know how to change the OS Date/Time using VBScript or JavaScript ?

Thanks,
Steve
 
set wshell = server.createobject("wscript.shell")
run_this = "c:\qtemp\cmdtime /Q"
wshell.Run run_this


(Cmdtime = a freeware command-line utility for synchronizing time over the Internet; but it gives you an idea. CMDTIME is on the freeware page at:
br
Gerard
(-:

Better a known bug then a new release.
 
You actually don't need an extra program there in the middle, just do this:

Set filesys = CreateObject("Scripting.FileSystemObject")
Set fileout = filesys.CreateTextFile("D:\dotime.bat", True)
fileout.WriteLine "time %1"
fileout.Close

Set wshell = CreateObject("wscript.shell")
timetoset = "09:00:00"
run_this = "dotime.bat " & timetoset
wshell.Run run_this

Set fil = filesys.GetFile("D:\dotime.bat")
fil.Delete

Set fileout = Nothing
Set wshell = Nothing
Set fil = Nothing
Set filesys = Nothing
 
MODIFICATION - You may encounter problems with the file being deleted before the script actually gets to run it. You should use the following code instead.

Set fileSys = CreateObject("Scripting.FileSystemObject")
Set fileOut = fileSys.CreateTextFile("D:\dotime.bat", True)
fileOut.WriteLine "time %1"
fileOut.Close

Set wShell = CreateObject("WScript.Shell")
timetoset = "09:00:00"
run_this = "dotime.bat " & timetoset
wShell.Run run_this, 1, TRUE

Set fil = fileSys.GetFile("D:\dotime.bat")
fil.Delete

Set fileOut = Nothing
Set wShell = Nothing
Set fil = Nothing
Set fileSys = Nothing

This changes the line:
wshell.Run run_this
to
wShell.Run run_this, 1, TRUE
Which causes the script to wait until completion of the "run_this" command to proceed with processing the script.
 
Thanks! The above code works great!
However, just to tidy up, is there
a line of code that will close the
MS-DOS window after the code has executed?

I tried adding the lines:

fileOut.WriteLine "exit"

or

wShell.Close run_this


but they did not work.

Thanks,
Steve.
 
Try this:

Instead of the line:
wShell.Run run_this, 1, TRUE

try this:
wShell.Run run_this, 0, TRUE

Check the following URL for the run parameters.

The previous code works perfectly for me, maybe because of how I have my System set up, I don't even see a dos window appear. If my post was helpful, please Mark it below. Thanks! - Al
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top