ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Shell Command - Net Time (Cannot create text file) (https://www.excelbanter.com/excel-programming/305402-shell-command-net-time-cannot-create-text-file.html)

Alan

Shell Command - Net Time (Cannot create text file)
 

Hi All,

I am trying to create a text file containing the current data / time
and store it in the same folder as my workbook.

I am using the following code under Excel 2000 running under Win2000
Pro on a Win2000 Server domain:


Sub TimeCheck()

ShellCommand = "Net time " & ThisWorkbook.Path & "\_Time.txt"

MsgBox Shell(ShellCommand)

End Sub


The value returned in the MsgBox is obviously the program's task ID
(as expected), but the text file (time.txt) is not created in the
folder with the workbook.

There is no permissions issue - it won't work on a network drive nor
my hard disk where I have full permissions.


Any ideas?

Thanks,

Alan.





Amedee Van Gasse[_3_]

Shell Command - Net Time (Cannot create text file)
 
Alan wrote:


Hi All,

I am trying to create a text file containing the current data / time
and store it in the same folder as my workbook.

I am using the following code under Excel 2000 running under Win2000
Pro on a Win2000 Server domain:


Sub TimeCheck()

ShellCommand = "Net time " & ThisWorkbook.Path & "\_Time.txt"

MsgBox Shell(ShellCommand)

End Sub


The value returned in the MsgBox is obviously the program's task ID
(as expected), but the text file (time.txt) is not created in the
folder with the workbook.

There is no permissions issue - it won't work on a network drive nor
my hard disk where I have full permissions.


Any ideas?

Thanks,

Alan.


I have got the impression you're trying to do something the hard way
when it can be easy. Does it *really* have to be the "net" time?

Sub TimeCheck()

Dim CurrentTime As Date
Const ForWriting = 2
Dim fs, f

Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile(ThisWorkbook.Path & _
"\_Time.txt", ForWriting, True)
CurrentTime = Now
f.Write CurrentTime
f.Close

MsgBox CurrentTime

End Sub

Please note: the True in OpenTextFile is for creating the file if it
does not exist.

--
To top-post is human, to bottom-post and snip is sublime.

Alan

Shell Command - Net Time (Cannot create text file)
 

"Amedee Van Gasse" wrote in message
...
Alan wrote:

I have got the impression you're trying to do something the hard way
when it can be easy. Does it *really* have to be the "net" time?

Sub TimeCheck()

Dim CurrentTime As Date
Const ForWriting = 2
Dim fs, f

Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile(ThisWorkbook.Path & _
"\_Time.txt", ForWriting, True)
CurrentTime = Now
f.Write CurrentTime
f.Close

MsgBox CurrentTime

End Sub

Please note: the True in OpenTextFile is for creating the file if it
does not exist.


Hi,

Once I have the net time, I will update the local client system time
returned by NOW().

Hence that is not an answer here.

Thanks for your idea though - any others?

Alan.



Amedee Van Gasse[_3_]

Shell Command - Net Time (Cannot create text file)
 
Alan wrote:


"Amedee Van Gasse" wrote in message
...
Alan wrote:

I have got the impression you're trying to do something the hard way
when it can be easy. Does it really have to be the "net" time?

Sub TimeCheck()

Dim CurrentTime As Date
Const ForWriting = 2
Dim fs, f

Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile(ThisWorkbook.Path & _
"\_Time.txt", ForWriting, True)
CurrentTime = Now
f.Write CurrentTime
f.Close

MsgBox CurrentTime

End Sub

Please note: the True in OpenTextFile is for creating the file if it
does not exist.


Hi,

Once I have the net time, I will update the local client system time
returned by NOW().

Hence that is not an answer here.

Thanks for your idea though - any others?

Alan.


Sure!
If I knew that in the first place, I would have given you the following
answer.

In the Microsoft Script Center, I found the following script:

----- CUT HERE -----
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer &
"\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_LocalTime")
For Each objItem in colItems
Wscript.Echo "Day: " & objItem.Day
Wscript.Echo "Day Of the Week: " & objItem.DayOfWeek
Wscript.Echo "Hour: " & objItem.Hour
Wscript.Echo "Milliseconds: " & objItem.Milliseconds
Wscript.Echo "Minute: " & objItem.Minute
Wscript.Echo "Month: " & objItem.Month
Wscript.Echo "Quarter: " & objItem.Quarter
Wscript.Echo "Second: " & objItem.Second
Wscript.Echo "Week In the Month: " & objItem.WeekInMonth
Wscript.Echo "Year: " & objItem.Year
Next
----- CUT HERE -----

This is in VBScript and is for Windows XP/2003. 2000, NT4 and 98 don't
support the Win32_LocalTime class used in this script (AFAIK).
You should be able to rewrite this script to VBA, perhaps changing
every Wscript.Echo to Debug.Print or even MsgBox will do the job
(IDNTT).

The trick here is to change strComputer. Here it is "." which means the
local computer. Change this to the name of your time server in your
local network, et voila!

I assume stuff like permissions are also involved, but if you need more
help to get it working, you should really check out
news:microsoft.public.windows.server.scripting

Good luck!


--
Amedee Van Gasse using XanaNews 1.16.3.1
If it has an "X" in the name, it must be Linux?

Alan

Shell Command - Net Time (Cannot create text file)
 
"Amedee Van Gasse" wrote in message
...

Sure!
If I knew that in the first place, I would have given you the
following answer.

In the Microsoft Script Center, I found the following script:

----- CUT HERE -----
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer &
"\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from
Win32_LocalTime") For Each objItem in colItems
Wscript.Echo "Day: " & objItem.Day
Wscript.Echo "Day Of the Week: " & objItem.DayOfWeek
Wscript.Echo "Hour: " & objItem.Hour
Wscript.Echo "Milliseconds: " & objItem.Milliseconds
Wscript.Echo "Minute: " & objItem.Minute
Wscript.Echo "Month: " & objItem.Month
Wscript.Echo "Quarter: " & objItem.Quarter
Wscript.Echo "Second: " & objItem.Second
Wscript.Echo "Week In the Month: " & objItem.WeekInMonth
Wscript.Echo "Year: " & objItem.Year
Next
----- CUT HERE -----

This is in VBScript and is for Windows XP/2003. 2000, NT4 and 98
don't support the Win32_LocalTime class used in this script (AFAIK).
You should be able to rewrite this script to VBA, perhaps changing
every Wscript.Echo to Debug.Print or even MsgBox will do the job
(IDNTT).

The trick here is to change strComputer. Here it is "." which means
the local computer. Change this to the name of your time server in
your local network, et voila!

I assume stuff like permissions are also involved, but if you need
more help to get it working, you should really check out
news:microsoft.public.windows.server.scripting

Good luck!


Hi,

Well - I learnt a lot about server scripts which was very interesting!

I actually ended up solving it in a simpler way by using the command
line parameters of the 'net time' command - see below.

The following executes the command but also sets the client system
clock from the server clock at the same time, so I get where I want to
go in a single step.


Sub TimeCheck()

Shell ("net time \\server /set /yes")

End Sub


Thanks for your help - I really appreciate it.

Alan.






All times are GMT +1. The time now is 01:22 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com