Solved

Writing Shell command results to a test file (VB6)

Posted on 2004-10-07
7
434 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA loop through headers using value 3 57
VBA: Select SQL query based on a config Sheet v2 11 38
clicking a shape in a frame array vb6 3 40
TT Auto Dashboard 13 85
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now