Command Prompt Window: Automating and copying window content

User avatar
Doc.AElstein
BronzeLounger
Posts: 1499
Joined: 28 Feb 2015, 13:11
Location: Hof, Bayern, Germany

Command Prompt Window: Automating and copying window content

Post by Doc.AElstein »

Operating System Command Prompt Shell Black Window thing: Automating: getting it up; Commands; copying contents to a text file
Hi, festive greetings to everyone, hope you are all well.

I think this is a Windows General question, with a possible leaning towards VBA …

One of my Winter computer projects this year involves a lot of tedious similar measurements associated with internal settings, and fiddling with internet stuff… some of which I am currently getting mostly manually via the black cmd window thingy. ( Black cmd window thing.JPG : https://imgur.com/okgmnt4" onclick="window.open(this.href);return false; , https://imgur.com/V7KpWld" onclick="window.open(this.href);return false; , https://imgur.com/KguV4xr" onclick="window.open(this.href);return false; , https://imgur.com/JVrPIUD" onclick="window.open(this.href);return false; , )
Mostly I am initially needing to do this in XP operating system.

I was wondering if anyone knows of ways to automate all or some of this.
Automation will both:
_a) Save me time
_b) help me get more consistent results: Some things which I am looking at react differently if a command is given at a specific time after another: It is difficult to accurately do a command at a specific time when doing them manually.

Broadly I can summarise what I am currently doing into two main actions:

Action 1
I bring up the black command window console thing, for example using
Microsoft Symbol+r
cmd
OK

(Microsoft Symbol+r cmd OK.jpg : https://imgur.com/TUvExlW" onclick="window.open(this.href);return false; )
In the black command window console thing which comes up, I type in after the > a command:
Ipconfig /all ( ipconfig all.jpg : https://imgur.com/BYyTzcc" onclick="window.open(this.href);return false; )
Then I type in after the > another command:
route print ( route print.jpg : https://imgur.com/IZLAD3r" onclick="window.open(this.href);return false; )
I now have the black command window console thing full with text that I want to copy. I do this currently like this:
I right click with the mouse at the top of the window, and select Edit --- Select All (Edit --- Select All cmd window.jpg : https://imgur.com/tJtkP08" onclick="window.open(this.href);return false; , https://imgur.com/S3wQPXj" onclick="window.open(this.href);return false; )
I right click with the mouse at the top of the window again , and this time, select Edit --- Copy (Edit --- Copy cmd window.jpg : https://imgur.com/s9iGu0m" onclick="window.open(this.href);return false; , https://imgur.com/MV49iDT" onclick="window.open(this.href);return false; )
I open a text file, and paste the contents from the clipboard into it, ( for example via Key combination Ctrl+v or by selecting the insert option after right mouse clicking when in the text file).
(Paste Contents from cmd window into text file.JPG : https://imgur.com/maOfUEe" onclick="window.open(this.href);return false; )

Action 2
This is the same as Action 1, except that the two commands to be used are
Ipconfig /release
Ipconfig /renew


Can anyone tell me how to automate all or some of this? It would be particularly useful if I can automate using VBA, since I know how to then further manipulate the text files using VBA
Thanks
Alan

P.S: here an attempt to summarise pictoriareally wot I am doing manually and what I want to try to automate, preferably with VBA
Action Sumary.JPG: https://imgur.com/dHgTOZC" onclick="window.open(this.href);return false;
Action Sumary.JPG
You do not have the required permissions to view the files attached to this post.
I am having difficulty logging in with this account just now.
You can find me at DocAElstein also

User avatar
HansV
Administrator
Posts: 79526
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Command Prompt Window: Automating and copying window con

Post by HansV »

Here is a brief :grin: example:

Code: Select all

Sub Test()
    Shell "cmd.exe /c ""ipconfig /all > %userprofile%\test.txt"""
End Sub
This will:
- Start a command prompt.
- Execute ipconfig /all
- Send the output to the text file test.txt in your user profile (you can specify a fixed path instead).
- Close the command prompt.
Best wishes,
Hans

User avatar
Doc.AElstein
BronzeLounger
Posts: 1499
Joined: 28 Feb 2015, 13:11
Location: Hof, Bayern, Germany

Re: Command Prompt Window: Automating and copying window con

Post by Doc.AElstein »

Thanks for the reply Hans,
I am testing versions of your code form on a few different systems / computers. I will report back when I am finished.
Alan
I am having difficulty logging in with this account just now.
You can find me at DocAElstein also

User avatar
Doc.AElstein
BronzeLounger
Posts: 1499
Joined: 28 Feb 2015, 13:11
Location: Hof, Bayern, Germany

Re: Command Prompt Window: Automating and copying window con

Post by Doc.AElstein »

Hello,
I have tested on some computers with Windows 8.1, Windows 7, Vista, and XP

Finally, it is working very well. Here is a summary and a few things that might be worth mentioning.

I used a single 4 code line version to do some tests on. Something of like this:

Code: Select all

 Sub ShellBlackCommandPromptWindowAutomatingCopyingWindowContent()
1    Shell "cmd.exe /c ""ipconfig /all > """ & ThisWorkbook.Path & "\test1B.txt"""""
2    Shell "cmd.exe /c ""route print > """ & ThisWorkbook.Path & "\test2B.txt"""""
3    Shell "cmd.exe /c ""ipconfig /release > """ & ThisWorkbook.Path & "\test3B.txt"""""
4    Shell "cmd.exe /c ""ipconfig /renew > """ & ThisWorkbook.Path & "\test4B.txt"""""
End Sub

( In some casers I used a path to my desktop instead… which revealed a simple problem which had me frustrated all day..****

Windows 7:
I tried on a several Windows 7 machines, all have Office 2003 most also have Office 2010, and a couple Office 2007
Code line 1 gives me in test1.txt what I get if I do it manually
Code line 2 gives me in test2.txt what I get if I do it manually
Code line 3 and code lines 4 give mixed up results. I have seen this strange effect several times on all my Win7 computers and at least once in all Office 2010. I Have never experienced it in Office 2003. ( I have not seen it either in Any Office 2007, but I have only a few Office 2007s , so that may not be too conclusive).
I get in test3.txt and in test4.txt either what I get manually for ipconfig /release or what I get manually ipconfig /renew.
The results seem random:
I ether get
_ in both the result which I get manually for ipconfig / release
or
_ in both I get what I get manually for ipconfig / renew.
or
_ code lines 3 an 4 get it correct, test3.txt gets what it should, ( it gets what I get manually for ipconfig / release) , test4.txt gets what it should ( it gets what I get manually for ipconfig / renew)
or
_ code lines 3 an 4 get it the wrong way around: test3.txt gets what I expect in test4.txt, and test4.txt gets what I expect in text3.txt

Further experiments of mine suggest that the commands ipconfig / release and ipconfig / renew in code lines 3 and 4 are doing what they should. So possibly the problem is restricted to what is sent to the text files. Possibly this is a bug.
So far, I have always got the results that I expect from code lines 1 and 2
I am mainly interested in the text information from code lines 1 and 2, so for windows 7 I have very close to what I want. I am not currently too interested in Windows 7.
(By the way, if those text files do not exist, then they seem to be made, as long as the path exists. (If the path does not exist, then the code does not error, but no text file is made))
I notice that in Office 2003, there is a noticeably longer pause between ipconfig / release and ipconfig / renew being done. (As I mentioned in my first post, one reason for me wanting to automise this is that I am seeing some different results from different time periods, in particular between execution of ipconfig / release and ipconfig / renew )
( #### I could be mistaken with all these conclusions.. – The different times between manual and automated execution may be actually giving different results).

Windows 8.1
I only have access to one Windows 8.1 computer, so my results may not be too conclusive yet. The results so far seem similar to those for Windows 7

Vista
I tried several Vista machines: All have Office 2003 , most also have 2010, and a few have 2007
Code line 1 gives me in test1.txt what I get if I do it manually
Code line 2 gives me in test2.txt what I get if I do it manually
Code line 3 gives me in test3.txt what I get if I do it manually, but note: it does not give me much useful information. It gives me
Der angeforderte Vorgang erfordert erhöhte Rechte. (The requested operation requires elevated privileges.) ( https://imgur.com/flR7JTD" onclick="window.open(this.href);return false; )
In order to get useful information, ( or rather that command to work), in Vista, I manually have to do things a bit differently, ( Vista needs Admin execution.jpg : https://imgur.com/5iRtxDB" onclick="window.open(this.href);return false; , https://imgur.com/b5FyQvv" onclick="window.open(this.href);return false; )
Code line 4 gives me in test4.txt what I get if I do it manually.
So in Vista I have close to what I want. It could be useful to have the text from what I get manually when “doing the ipconfig / releasewith Admin rights”, but this is less important. I can live without that for now
(As with Windows 7 , if those text files do not exist, then they seem to be made, as long as the path exists. (If the path does not exist, then the code does not error, but no text file is made))

XP results in next post…
Last edited by Doc.AElstein on 12 Dec 2019, 20:10, edited 1 time in total.
I am having difficulty logging in with this account just now.
You can find me at DocAElstein also

User avatar
Doc.AElstein
BronzeLounger
Posts: 1499
Joined: 28 Feb 2015, 13:11
Location: Hof, Bayern, Germany

Re: Command Prompt Window: Automating and copying window con

Post by Doc.AElstein »

XP
I tried a few computers. All have Office 2003 and 2010
I spent almost a day**** of frustration trying to get the code lines to work in XP, and had prepared a long post to explain all my failed attempts… but then just before posting I found by chance what the problem was and there was a simple cure ….
Googling had previously showed me that others have had similar problems, and they had either given up, or used complicated workarounds using so called Battyfiles which I gave up trying to understand after a couple of hours…
Possibly, like me initially, others had missed the simple solution…
To explain: In my XP code lines, I had been using the path to my Desktop. All path strings had co incidentally at least one space somewhere.
I think a reason why others like me missed the obvious is that the code lines rarely error. If things go wrong, you often get a blink of a black terminal window and are fooled into thinking something was done.
The problem was / is that a path like this causes the code line not to work
D:\Alans Folder\test1.txt
The problem is the space . Without a space it will work , D:\AlansFolder\test1.txt, ( provided that that is a valid path ).
If you have any spaces, then that path must be enclosed in quotes, (which within the complete command string means enclosed in a double quote pair.
“”D:\Alans Folder\test1.txt””
A final complete code line then looks something like
Shell "cmd.exe /c ""ipconfig /all > “”D:\Alans Folder\test1.txt””"""
( It does no harm to have the extra quote pair if you don’t need them, so it is probably a good idea to always use them to avoid getting a problem when you do need them)

I mostly need XP just now, so it was Sod’s Law that I had the most problem getting XP to work.
But it is working fine now. ( I have not experienced in XP the strange mixing up of the contents of the last two text files, test3.txt and test4.txt. Things are generally a bit slower in my XP computers. That may have some effect.
I am wondering if when the commands are done too quickly, some register or similar storage place does not get cleared quick enough as it should, leading to the false contents being sent out to the text files…that’s just a totally unqualified guess.. ( #### I could be mistaken – The different times between manual and automated execution may be giving different results).. )

This automation is very useful to me just now. It has already saved me a few hours this morning, and will likely save me lots of time in the next few weeks.

Thanks again
Alan

_.____________

Ref :
https://stackoverflow.com/questions/637 ... 1#59346481" onclick="window.open(this.href);return false;

“Test ipconfig.xls” : https://app.box.com/s/m8am58h0tvizd6gfaytor1i58s3t8bvo" onclick="window.open(this.href);return false;

_.________________________________
Edit a few days later:
Another strange problem I once experienced , was that the command prompt coding lines were not working on one particular XP computer. I finally tracked the problem down to a point . in the computer name. Once I removed this point . all was well.
( I could not repeat the exüeriment, as any attempts to change the name to include a point . , were not allowed….. This particulate computer I did not have from new . I don’t know how a point was made in the computer name)

Ref
Change Computer name in XP : https://www.watchingthenet.com/how-to-c ... vista.html" onclick="window.open(this.href);return false;
http://www.excelfox.com/forum/showthrea ... #post11572" onclick="window.open(this.href);return false;
I am having difficulty logging in with this account just now.
You can find me at DocAElstein also