writing in notepad through through shell command

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ravipanand
    New Member
    • Sep 2008
    • 4

    writing in notepad through through shell command

    Hi;

    I m using following code for writing system IP in notepad from shell command

    shell("ipconfig > c:\ipfile.txt")

    but it do nothing( no errors comes also)

    please solve this problem

    Thanx
    Ravi
  • FishVal
    Recognized Expert Specialist
    • Jun 2007
    • 2656

    #2
    Hello, Ravi.

    I'm not sure whether stdout could be directed to text file using command line parameter, but you can easily get it by Access (did you mean this ?) application. Read the following - Waiting for Shell to Finish.

    Regards,
    Fish.

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      Something very similar can easily be implemented within a Batch File, and even opened within Access if you so desire:
      Code:
      @Echo Off
      cls
      IPConfig.exe > C:\Windows\IP_Config.txt
      Notepad.exe C:\Windows\IP_Config.txt

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32668

        #4
        Firstly, using parentheses to surround the Shell() parameters is only correct if you Call the function :
        Code:
        Call Shell("IPConfig >C:\IPFile.Txt")
        Also, check out ShellWait() Function for synchronous execution of commands.

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          Originally posted by NeoPa
          Firstly, using parentheses to surround the Shell() parameters is only correct if you Call the function :
          Code:
          Call Shell("IPConfig >C:\IPFile.Txt")
          Also, check out ShellWait() Function for synchronous execution of commands.
          That's not exactly True, is it NeoPa? Since the Shell Function returns a Variant (Double) representing the Program's task ID if successful, otherwise 0. Call can only be used on a Function when you wish to ignore the Return Value. All this is applicable especially in the Thread's context, otherwise, aside from opening the actual Text File, how would you know if it executed correctly?
          Code:
          Dim RetVal As Variant
          RetVal = Shell("C:\WINDOWS\SYSTEM32\CALC.EXE", vbNormalFocus)
          
          If RetVal <> 0 Then
            MsgBox "Shell executed successfully!"
          Else
            MsgBox "Command did not execute"
          End If

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32668

            #6
            You're absolutely right ADezii.

            What I should have said is that using parentheses to surround the Shell() parameters is only correct if you are using the function as a function rather than as a subroutine. That is to say that a value is returned. This can be done by assigning the value to something, using it in some evaluation or using Call to state explicitly to ignore the resultant value.

            Functions are allowed (VBA is rather stupid that way) to be called as subroutines in code. This has a similar effect to using Call but it is less obvious what the coder is dealing with. The parentheses are not used whan calling a function as a subroutine.

            Comment

            • FishVal
              Recognized Expert Specialist
              • Jun 2007
              • 2656

              #7
              Just out of curiosity.

              Did anybody get that
              Code:
              IPConfig >C:\IPFile.Txt
              work as expected?

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32668

                #8
                I don't understand the question Fish?

                What are you not sure about?

                Comment

                • FishVal
                  Recognized Expert Specialist
                  • Jun 2007
                  • 2656

                  #9
                  Originally posted by NeoPa
                  I don't understand the question Fish?

                  What are you not sure about?
                  Does that put stdout stream to text file?
                  That is basically what I'm not sure about.

                  Did anybody get IPConfig output to text file via Shell() function or even Start>Run... ?

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32668

                    #10
                    Aah. I see you mean did anybody get to work, the code :
                    Code:
                    Call Shell("IPConfig >C:\IPFile.Txt")
                    Trying to work out what you were asking I tried it and it didn't work. The redirection characters' functionality must be provided by the interpreter. I will work out a replacement version that should invoke the standard interpreter and work. Give me a minute.

                    Comment

                    • NeoPa
                      Recognized Expert Moderator MVP
                      • Oct 2006
                      • 32668

                      #11
                      Originally posted by FishVal
                      Did anybody get IPConfig output to text file via Shell() function or even Start>Run... ?
                      No. It doesn't work from either method.

                      I'm just preparing a version that should work.

                      Comment

                      • FishVal
                        Recognized Expert Specialist
                        • Jun 2007
                        • 2656

                        #12
                        From command line it works well, BTW. From batch file too. ADezii has made a good point.

                        Comment

                        • NeoPa
                          Recognized Expert Moderator MVP
                          • Oct 2006
                          • 32668

                          #13
                          The following code can be run from the Immediate Pane :
                          Code:
                          strCmd = """%C"" /C IPConfig >C:\IPFile.Txt" : _
                          strCmd = Replace(strCmd, "%C", Environ("ComSpec")) : _
                          Call Shell(strCmd)
                          This is a bit different from the code you would use if you want it in a module, but the basics are all there.

                          Comment

                          • ADezii
                            Recognized Expert Expert
                            • Apr 2006
                            • 8834

                            #14
                            Originally posted by NeoPa
                            The following code can be run from the Immediate Pane :
                            Code:
                            strCmd = """%C"" /C IPConfig >C:\IPFile.Txt" : _
                            strCmd = Replace(strCmd, "%C", Environ("ComSpec")) : _
                            Call Shell(strCmd)
                            This is a bit different from the code you would use if you want it in a module, but the basics are all there.
                            @NeoPa & FishVal, I was able to handle the entire process within Access, namely:
                            1. Execute the Batch File (IP.Bat) listed below:
                              Code:
                              @echo off
                              cls
                              IPConfig.exe > C:\Windows\IP_Config.txt
                            2. Display the results of IP_Config.txt in a Text Box [txtIP]:
                              Code:
                              Dim blnRetVal As Boolean
                              Dim strTextLine As String
                              Dim strBig As String
                              Const con_BATCH_FILE_PATH As String = "C:\IP.bat"
                              Const con_Text_FILE_PATH As String = "C:\Windows\IP_Config.txt"
                               
                              blnRetVal = Execute_Program(con_BATCH_FILE_PATH, "", "")
                              
                              Open con_Text_FILE_PATH For Input As #1
                              
                              Do While Not EOF(1)             'Loop until end of file.
                                Line Input #1, strTextLine    'Read line into variable.
                                If Len(strTextLine) > 0 Then
                                  strBig = strBig & Trim$(strTextLine) & vbCrLf
                                End If
                              Loop
                              Close #1                        'Close file.
                              
                              Me![txtIP] = strBig             'Assign String to Text Box
                            3. OUTPUT in txtIP:
                              Code:
                              Windows IP Configuration
                              Ethernet adapter Local Area Connection:
                              Connection-specific DNS Suffix  . :
                              IP Address. . . . . . . . . . . . : 192.168.1.102
                              Subnet Mask . . . . . . . . . . . : 255.255.255.0
                              Default Gateway . . . . . . . . . : 192.168.1.1
                            4. Just let me know if this is a little extreme, I do this occasionally! (LOL)! I'll Post the Declarations and Function only if anyone is actually interested.

                            Comment

                            • FishVal
                              Recognized Expert Specialist
                              • Jun 2007
                              • 2656

                              #15
                              Here is an example using Windows Script Host Model library.

                              [code=vb]
                              Private Sub btnIPConfig_Cli ck()

                              Dim objWshShell As New IWshRuntimeLibr ary.WshShell
                              Dim objApp As IWshRuntimeLibr ary.WshExec

                              Set objApp = objWshShell.Exe c("IPConfig")
                              While objApp.Status = WshRunning
                              Wend
                              Me.txbStdOut = objApp.StdOut.R eadAll

                              Set objApp = Nothing
                              Set objWshShell = Nothing

                              End Sub
                              [/code]

                              Comment

                              Working...