By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,705 Members | 1,829 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,705 IT Pros & Developers. It's quick & easy.

writing in notepad through through shell command

P: 4
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
Sep 29 '08 #1
Share this Question
Share on Google+
19 Replies


FishVal
Expert 2.5K+
P: 2,653
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.
Sep 29 '08 #2

ADezii
Expert 5K+
P: 8,669
Something very similar can easily be implemented within a Batch File, and even opened within Access if you so desire:
Expand|Select|Wrap|Line Numbers
  1. @Echo Off
  2. cls
  3. IPConfig.exe > C:\Windows\IP_Config.txt
  4. Notepad.exe C:\Windows\IP_Config.txt
Sep 29 '08 #3

NeoPa
Expert Mod 15k+
P: 31,656
Firstly, using parentheses to surround the Shell() parameters is only correct if you Call the function :
Expand|Select|Wrap|Line Numbers
  1. Call Shell("IPConfig >C:\IPFile.Txt")
Also, check out ShellWait() Function for synchronous execution of commands.
Sep 29 '08 #4

ADezii
Expert 5K+
P: 8,669
Firstly, using parentheses to surround the Shell() parameters is only correct if you Call the function :
Expand|Select|Wrap|Line Numbers
  1. 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?
Expand|Select|Wrap|Line Numbers
  1. Dim RetVal As Variant
  2. RetVal = Shell("C:\WINDOWS\SYSTEM32\CALC.EXE", vbNormalFocus)
  3.  
  4. If RetVal <> 0 Then
  5.   MsgBox "Shell executed successfully!"
  6. Else
  7.   MsgBox "Command did not execute"
  8. End If
Sep 29 '08 #5

NeoPa
Expert Mod 15k+
P: 31,656
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.
Sep 29 '08 #6

FishVal
Expert 2.5K+
P: 2,653
Just out of curiosity.

Did anybody get that
Expand|Select|Wrap|Line Numbers
  1. IPConfig >C:\IPFile.Txt
  2.  
work as expected?
Sep 29 '08 #7

NeoPa
Expert Mod 15k+
P: 31,656
I don't understand the question Fish?

What are you not sure about?
Sep 29 '08 #8

FishVal
Expert 2.5K+
P: 2,653
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... ?
Sep 29 '08 #9

NeoPa
Expert Mod 15k+
P: 31,656
Aah. I see you mean did anybody get to work, the code :
Expand|Select|Wrap|Line Numbers
  1. 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.
Sep 29 '08 #10

NeoPa
Expert Mod 15k+
P: 31,656
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.
Sep 29 '08 #11

FishVal
Expert 2.5K+
P: 2,653
From command line it works well, BTW. From batch file too. ADezii has made a good point.
Sep 29 '08 #12

NeoPa
Expert Mod 15k+
P: 31,656
The following code can be run from the Immediate Pane :
Expand|Select|Wrap|Line Numbers
  1. strCmd = """%C"" /C IPConfig >C:\IPFile.Txt" : _
  2. strCmd = Replace(strCmd, "%C", Environ("ComSpec")) : _
  3. 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.
Sep 29 '08 #13

ADezii
Expert 5K+
P: 8,669
The following code can be run from the Immediate Pane :
Expand|Select|Wrap|Line Numbers
  1. strCmd = """%C"" /C IPConfig >C:\IPFile.Txt" : _
  2. strCmd = Replace(strCmd, "%C", Environ("ComSpec")) : _
  3. 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:
    Expand|Select|Wrap|Line Numbers
    1. @echo off
    2. cls
    3. IPConfig.exe > C:\Windows\IP_Config.txt
  2. Display the results of IP_Config.txt in a Text Box [txtIP]:
    Expand|Select|Wrap|Line Numbers
    1. Dim blnRetVal As Boolean
    2. Dim strTextLine As String
    3. Dim strBig As String
    4. Const con_BATCH_FILE_PATH As String = "C:\IP.bat"
    5. Const con_Text_FILE_PATH As String = "C:\Windows\IP_Config.txt"
    6.  
    7. blnRetVal = Execute_Program(con_BATCH_FILE_PATH, "", "")
    8.  
    9. Open con_Text_FILE_PATH For Input As #1
    10.  
    11. Do While Not EOF(1)             'Loop until end of file.
    12.   Line Input #1, strTextLine    'Read line into variable.
    13.   If Len(strTextLine) > 0 Then
    14.     strBig = strBig & Trim$(strTextLine) & vbCrLf
    15.   End If
    16. Loop
    17. Close #1                        'Close file.
    18.  
    19. Me![txtIP] = strBig             'Assign String to Text Box
  3. OUTPUT in txtIP:
    Expand|Select|Wrap|Line Numbers
    1. Windows IP Configuration
    2. Ethernet adapter Local Area Connection:
    3. Connection-specific DNS Suffix  . :
    4. IP Address. . . . . . . . . . . . : 192.168.1.102
    5. Subnet Mask . . . . . . . . . . . : 255.255.255.0
    6. 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.
Sep 30 '08 #14

FishVal
Expert 2.5K+
P: 2,653
Here is an example using Windows Script Host Model library.

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnIPConfig_Click()
  2.  
  3.     Dim objWshShell As New IWshRuntimeLibrary.WshShell
  4.     Dim objApp As IWshRuntimeLibrary.WshExec
  5.  
  6.     Set objApp = objWshShell.Exec("IPConfig")
  7.     While objApp.Status = WshRunning
  8.     Wend
  9.     Me.txbStdOut = objApp.StdOut.ReadAll
  10.  
  11.     Set objApp = Nothing
  12.     Set objWshShell = Nothing
  13.  
  14. End Sub
  15.  
Sep 30 '08 #15

NeoPa
Expert Mod 15k+
P: 31,656
Hi
it is also not creating IPFile in c:
This was found in a report from the OP. It was submitted roughly 5 1/2 hours ago (before this post).
Sep 30 '08 #16

NeoPa
Expert Mod 15k+
P: 31,656
Unfortunately, as there is no reference to what you're talking about (there are after all, many suggestions in this thread), it's hard to know how to respond.

@ADezii & Fish
Although these ideas may be interesting in their own right, as the (fairly) simple command can be run directly from within Access it does seem a little overkill.

It does make me wonder if what I posted was understood clearly. Maybe I overcomplicated it somewhat, but I wanted a copy / pastable solution that would enable anyone to test the theory for themselves. It certainly produces the required results (Referring to post #13).
Sep 30 '08 #17

ADezii
Expert 5K+
P: 8,669
Unfortunately, as there is no reference to what you're talking about (there are after all, many suggestions in this thread), it's hard to know how to respond.

@ADezii & Fish
Although these ideas may be interesting in their own right, as the (fairly) simple command can be run directly from within Access it does seem a little overkill.

It does make me wonder if what I posted was understood clearly. Maybe I overcomplicated it somewhat, but I wanted a copy / pastable solution that would enable anyone to test the theory for themselves. It certainly produces the required results (Referring to post #13).
Perhaps I was the one who misinterpreted the OP's request. Redirecting the Output of the IPConfig Command was only half the equation, displaying the Output within the context of Access was the other. That's why the logic in Post Numbers 3 and 14. Oh well, it wasn't the first, and it surely won't be the last time, that I was wrong (LOL).
Sep 30 '08 #18

NeoPa
Expert Mod 15k+
P: 31,656
Not at all ADezii. You may well be right. I simply interpreted the "in Notepad" as meaning in a (the) format that Notepad uses (IE .Txt).

Your psychic powers may well be more sensitive than mine on this issue ;)
Sep 30 '08 #19

NeoPa
Expert Mod 15k+
P: 31,656
Ravi,

I notice you have just tried again to post the same message as you tried before. When you want to post you need to click on the Reply button rather than the Report one.

Reporting merely sends a private message to the moderators letting them know you feel something is wrong with that post.

I hope you are more successful in future.
Sep 30 '08 #20

Post your reply

Sign in to post your reply or Sign up for a free account.