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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shell call timing

Status
Not open for further replies.

BoFenger

Programmer
Aug 6, 2003
22
DK
Hello
I'am using a shell command to create a text file:

Shell("cmd.exe /C NET TIME \\fileserver > time.txt")

Application.Wait (Now + TimeValue("0:00:01"))
' Open file and read in text
intF = FreeFile
Open filnavn For Input As #intF
Line Input #intF, strX
Close intF

The wait command is inserted to ensure that the text file is created before it is opened. Is there a way I could stop the executing of the code until the textfile is created, instead of using this silly wait command.

hope you can help me
\Bo
 
There is a little method you need to include:

Private Sub RunUntilFinished(ByVal App As String, show As Boolean)

Dim lProcID As Long
Dim hProc As Long

If show = True Then
lProcID = Shell(App, vbNormalNoFocus)
ElseIf show = False Then
lProcID = Shell(App, vbHide)
End If
DoEvents

hProc = OpenProcess(SYNCHRONIZE, 0, lProcID)
If hProc <> 0 Then
WaitForSingleObject hProc, INFINITE
CloseHandle hProc
End If
Exit Sub

End Sub

Also include the following declarations:

Private Declare Function CloseHandle Lib &quot;kernel32&quot; (ByVal hObject As Long) As Long
Private Declare Function OpenProcess Lib &quot;kernel32&quot; (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib &quot;kernel32&quot; (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

Now instead of what you have, you can just type:

RunUntilFinished(&quot;NET TIME \\fileserver > time.txt&quot;, true)
 
Hi,

Instead of using wait you could use DIR$ to see if the file exists before continuing:

strResults = &quot;&quot;
Do While strResults = &quot;&quot;
strResults = Dir$(&quot;C:\time.txt&quot;)
Loop

Alternatively, instead of shelling a DOS command you could open a file for output:

Open &quot;c:\time.txt&quot; For Output As #2
Print #2, Time
Close #2

Best of luck,
Tom
 
Hi TomKane
It almost worked, but I had to tjeck the filesize as well, otherwise I was reading from an empty file:

s = 0
Do While s = 0
s = CreateObject(&quot;Scripting.FileSystemObject&quot;).Getfile(&quot;c:\time.txt&quot;).Size
Loop

thank you for your tip

\Bo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top