Results 1 to 5 of 5

Thread: How do I use VB to open a CMD window, give commands, and gather output as a variable

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    25

    Post How do I use VB to open a CMD window, give commands, and gather output as a variable

    Hey all, this would be my first time posting here. And I would generally not ask, but I have been struggling with this for two days now and I would sincerely appreciate any help that I know you coding-gurus could provide.

    I am still a noob, but I try not to be. Up until now I had been fine running Google searches. I run Visual Basic 2010 in Visual Studio 2010. And my goal is the following:

    I need to have a GUI made with Visual Basic that, on a button click: opens up a command prompt, runs dsquery, and then collects the resulting text from dsquery as a variable or an array. I thought this would be easy, and indeed, I was instantly able to find the shell("") command. The shell("") command works perfectly if I use the following code:

    Shell("cmd /c dsquery * -scope subtree -attr ""cn"" -filter ""(&(objectclass=computer) (objectcategory=computer))"" -limit 0 >""H:\Desktop\ComputerOutput\AllWsg.txt""")

    This works perfectly, and from here, I can easily do what I want by using streamreaders and streamwriters to manipulate text documents.

    My question, in so many words, is this:

    How would I go about porting the result from the shell("") command to a VB variable, without ever having a text document? Is this even possible?

    I googled, and found plenty of examples of redirect.standardinput and redirect.standardoutput. However, no matter what syntax or combination I tried: when I looked at my resulting variable in a messagebox, it would be blank. Indeed, it seemed like when I would use redirect.standardinput, the command prompt would flash in and out of existance, completely ignoring my read / write commands.

    Is there any way to either:
    A) Make the shell("") command port to a variable
    or
    B) Use some other set of commands to do that?
    or
    C) Explain why redirect.standardinput may fail like that

    Thank you all in advance, any help would be great!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How do I use VB to open a CMD window, give commands, and gather output as a varia

    Don't use Shell in VB.NET. It's a holdover from VB6 that exists primarily to allow VB6 code to upgrade easily. In VB.NET you should be using Process.Start, which provides an explicit interface for redirecting standard input and output streams. There is an example in the VB.NET CodeBank forum, I think by gigemboy, that shows how to automate a command prompt in this manner.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    25

    Question Re: How do I use VB to open a CMD window, give commands, and gather output as a varia

    Hey jmcilhinney, thanks for the reply.

    I searched this forum before posting, and found the post you referenced. I actually found a different thread talking about shell("") where you basically copy pasted your response. That's what I meant when I said:

    "I... found plenty of examples of redirect.standardinput and redirect.standardoutput... no matter what syntax or combination I tried: when I looked at my resulting variable in a messagebox, it would be blank... it seemed like when I would use redirect.standardinput, the command prompt would flash in and out of existance, completely ignoring my read / write commands."

    I basically tried this thread, and no matter what, I get no response.

    http://www.vbforums.com/showthread.p...tion-2003-2005

    Post numbers: 36, 38, & 40 describe my issue, and then the person who had the issue says he resolved it without changing anything? This is not helpful at all =(

    Then post numbers: 45, 46, 47, 49, 50 are from a user with my issue who eventually "solves" this it by using some javascript code. But then he ends that post saying it fails to do what he wants.

    Anyone know why this code might not work for me? This is brutal, I am really clueless =(

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How do I use VB to open a CMD window, give commands, and gather output as a varia

    I'm afraid that I've never actually done this sort of thing myself so I can't really advise. I'm just aware of that CodeBank thread and generally direct people there. Sorry not to be of more help.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    25

    Re: How do I use VB to open a CMD window, give commands, and gather output as a varia

    It's been a few months now and I have fixed my issue. Basically, I was programming in a weird enterprise environment and I didn't have enough permissions to do what I wanted to. So here's my fix: to get the code to work correctly, first, I build the project. Then, I go to my project's bin/debug folder and copy the .exe of my project. I paste that .exe into my C:\temp folder. THEN from my desktop I right click run as admin a .bat file that I created. This .bat file contains nothing but "start C:\temp\filename.exe"

    When I call the program like that, I am able to circumvent the administrative difficulties that I was experiencing. I hope writing this saves some other newbie days of fiddling.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width