Solved

Writing Shell command results to a test file (VB6)

Posted on 2004-10-07
7
432 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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…

707 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

15 Experts available now in Live!

Get 1:1 Help Now