Solved

Writing Shell command results to a test file (VB6)

Posted on 2004-10-07
7
437 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to open Waze.com/livemap from address saved in DB? 26 189
VBA color chart bars 12 85
VBA error replacing data 6 39
Determine Range to Select 5 48
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

776 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