Help with Shell

User avatar
Don Wells
5StarLounger
Posts: 689
Joined: 27 Jan 2010, 16:45
Location: Ottawa, Ontario, Canada

Help with Shell

Post by Don Wells »

The following Command line works as expected
    systeminfo |find "Time Zone" > c:\trash\data.txt

I have attempted to achieve the same results with the following VBA code which I derived from here in the post by 'Billy Yao [MSFT]' dated 28th Jan 2004:

Code: Select all

Option Explicit

Public Sub dos()
Dim MyCommand As String
Dim TaskId As Long

  MyCommand = "cmd /c systeminfo | ""Time Zone"" > c:\Trash\data.txt"
  TaskId = Shell(MyCommand, 1)

End Sub
Please point out the errors in my code.

Many thanks in advance.
Regards
Don

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Help with Shell

Post by Rudi »

Try this:

Code: Select all

Public Sub dos()
Dim MyCommand As String
Dim TaskId As Long

  MyCommand = "cmd /c systeminfo |find ""Time Zone"" > c:\data.txt"
  TaskId = Shell(MyCommand, 1)

End Sub
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
Don Wells
5StarLounger
Posts: 689
Joined: 27 Jan 2010, 16:45
Location: Ottawa, Ontario, Canada

Re: Help with Shell

Post by Don Wells »

    After a great deal of effort I have been unable to capture the System Time Zone using the Shell command. It is my belief that the problem stems from the manner in which the command prompt handles quotation marks when they enclose a pipe, coupled with the command line including a VBA Class Member (Time).

    I have developed a work-around (with its own problems) presented here, but would deeply appreciate guidance on extracting the system time zone using the Shell command.
Regards
Don

User avatar
HansV
Administrator
Posts: 78402
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Help with Shell

Post by HansV »

The code posted by Rudi works for me if I change the path - recent versions of Windows don't like users saving files in the root of C:\. So try C:\Temp\data,txt or similar.
I have attached the result I got from running the code. Note that there is no indication of DST/Summer Time (my time zone is UTC+1, but with Summer Time it's currently UTC+2)'
data.txt
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
Don Wells
5StarLounger
Posts: 689
Joined: 27 Jan 2010, 16:45
Location: Ottawa, Ontario, Canada

Re: Help with Shell Problem Solved

Post by Don Wells »

Thank you Rudi and Hans.
Regards
Don

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Help with Shell

Post by Rudi »

When I was experimenting with your query, I changed your original path to write directly to the C:\ drive as I was too lazy :crazy: to create a 'Trash' folder on C:\. I forgot to change it back to your original path before I posted. Similar to Hans, the code I posted worked every time for me too.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
Don Wells
5StarLounger
Posts: 689
Joined: 27 Jan 2010, 16:45
Location: Ottawa, Ontario, Canada

Re: Help with Shell (an afterthought)

Post by Don Wells »

If anyone should happen to stumble upon this thread in search of code to return the system Time Zone, I would recommend they use the code developed by Chip Pearson which is available here. It is orders of magnitude faster than that previously shown in this thread.
Regards
Don

User avatar
HansV
Administrator
Posts: 78402
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Help with Shell

Post by HansV »

Thanks for the tip!
Best wishes,
Hans