Solved

Writing Shell command results to a test file (VB6)

Posted on 2004-10-07
7
440 Views
Last Modified: 2008-02-01
Greetings everyone - I have a question for the VB6 folks in the room.

I am trying to build a Shell command that will execute a command line application and write the results of the command to a text file.  Now, in DOS this is easy, I just add >>FilePath\Filename.txt to the end of the command, and viola! instant results file.

Apparently it's not that easy in VB.

Here's the code I am using:

If boolAllowLogin Then
'1. Assign values to global variables
gstrDepServer = ReadINIKey("servers", cmbEnvironment, "C:\Data\bmui.ini")
gstrUser = txtUserName
gstrPwd = txtPassword

'2. Check to see if the user has rights on the environment
strFileName = "C:\Data\LoginResults.txt"
strShellCmd = "bundlemgr"
strShellCmd = strShellCmd & " -ds " & gstrDepServer
strShellCmd = strShellCmd & " -u " & gstrUser
strShellCmd = strShellCmd & " -pwd " & gstrPwd
strShellCmd = strShellCmd & " -c list"
strShellCmd = strShellCmd & ">>" & strFileName

Shell (strShellCmd)
End If

I have set breakpoints and watched the code run through, it does build and execute the Shell Command, so I know the flag is getting set correctly, but the results file never gets created.  If I take the text that gets built above and paste it into a command line, it executes correctly and creates the file I expect to see, so I know the base syntax is correct.  I just can't seem to make VB do it correctly.  Am I missing something here?

Thanks

Greg
0
Comment
Question by:Jzaltheral
  • 4
  • 3
7 Comments
 
LVL 26

Expert Comment

by:EDDYKT
ID: 12248810
Try this


strShellCmd = strShellCmd & " >> " & strFileName


add space between >>
0
 
LVL 2

Author Comment

by:Jzaltheral
ID: 12249141
EDDYKT,

That yields up the same results.

Thanks
Greg
0
 
LVL 26

Accepted Solution

by:
EDDYKT earned 250 total points
ID: 12249274
Or try


strShellCmd = environ("COMSPEC") & " /c bundlemgr"
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 2

Author Comment

by:Jzaltheral
ID: 12249371
That gives me an invalid path option.

It does return the correct path to my CMD.EXE file, but I think it's expecting bundlemgr to be there instead of where it really is.  And the bundlemgr location is in the path on the machine.
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 12249508
then use the fullpath for bundlemgr can you
0
 
LVL 2

Author Comment

by:Jzaltheral
ID: 12250616
As Emeril would say: Bam!

Ok, I had to tweak some things, but I did get it to work.  I had to remove the space from the directory name that the executeable lives in and then EDDYKT's solution works quite well.  Here's the completed code.

    If boolAllowLogin Then
    '1. Assign values to global variables
        gstrDepServer = ReadINIKey("servers", cmbEnvironment, "C:\Data\bmui.ini")
        gstrUser = txtUserName
        gstrPwd = txtPassword
         
    '2. Build a shell command to check for login access
        strFileName = "C:\Data\LoginResults.txt"                'Set the filename
        strShellCmd = Environ("COMSPEC") & " /c C:\BundleManager\bundlemgr"  'Set the application
        strShellCmd = strShellCmd & " -ds " & gstrDepServer     'Add the dep server
        strShellCmd = strShellCmd & " -u " & gstrUser           'Add the username
        strShellCmd = strShellCmd & " -pwd " & gstrPwd          'Add the password
        strShellCmd = strShellCmd & " -c list"                  'Add the app command
        strShellCmd = strShellCmd & " >> " & strFileName        'Add the results file
   
    '3. Exceute the Shell Command and write the results file
        RetVal = Shell(strShellCmd, vbHide)
    End If

This produces the expected results when executed.
Thanks EDDYKT.

Greg
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 12250645
>>I had to remove the space from the directory name


if you have space then

""c:\program files\file""


etc
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Adding to a VBA? 6 78
How to set the sa password in a vb6 code for sql connection 9 60
Modifying Conditional Format from VBA code 3 60
IF ELSE Statement in Excel Macro VBA 16 75
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question