3

I can run on command line this loop:

for /f "tokens=2" %i in ('arp -a ^| Find/i "dynamic"') do echo %i

but if I run the same bat file I get:

^C^C^C^Cthe process tried to write to a nonexistent pipe^C^C^C^C^C^C^C^C^

C:\1\test.bat:

for /f "tokens=2" %%i in ('arp -a ^ |Find/i "dynamic"') do echo %%i

Returned by your arp -a command:

C:\Users\test>arp -a
Interface: xxx.xx.xxx.40 --- 0xbe
Internet Address      Physical Address
 xxx.xx.xxx.18           xx-xx-xx-xx-xx-xx     dynamic
 xxx.xx.xxx.26           xx-xx-xx-xx-xx-xx     dynamic
 xxx.xx.xxx.34           xx-xx-xx-xx-xx-xx     dynamic
 xxx.xx.xxx.114          xx-xx-xx-xx-xx-xx     dynamic

thanks for any help //Simon

Compo
  • 36,585
  • 5
  • 27
  • 39
user8785018
  • 55
  • 1
  • 1
  • 6

3 Answers3

3

You've introduced an additional whitespace character before the pipe, |. The caret, ^, therefore has the effect of escaping the whitespace and the pipe isn't escaped as it should be.

@For /F "Tokens=2" %%i In ('arp -a ^| Find /I "dynamic"') Do @(Echo %%i)>>"%~dp0output.txt"
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Thanks, Compo, but it's the same result again :( – user8785018 Jan 18 '18 at 14:26
  • 1
    Because there's nothing wrong with the command I have given you, the error must be caused by what is being returned by your `arp -a` command. How about you enter `arp -a` at the command prompt, then copy and paste the resulting output into your original question as an [edit](https://stackoverflow.com/posts/48318204/edit). When you do that, select the newly pasted section and press the button which looks like this **`{}`**, to put it into a readable format. – Compo Jan 18 '18 at 14:36
  • I copied and paste the resulting output of arp -a to my main question! – user8785018 Jan 19 '18 at 09:45
  • 1
    There is nothing wrong with the command, I have given you and nothing wrong with the output you have added to your question. Perhaps you should check that your script hasn't got any strange encoding or control characters inside it. Use the built-in `notepad.exe` and paste my command above directly into it, then add `Pause` to the line below it and optionally `@Echo Off` to the line above it and `save as > all files > Location\And\NameOfYourChoosing.cmd`. Now execute it, check the output, and let me know what the result is. – Compo Jan 19 '18 at 10:18
  • Hi combo, Nothing happens, but when I try to close cmd window the result will be: – user8785018 Jan 19 '18 at 10:46
  • ^C^C^C^C^C^C^C^C^C^C^C^^C^C^C^C^C^^C^C^C^C^^C^^C^C^^C^C^C^C^C^C^C^C^The process tried to write to a nonexistent pipe. The process tried to write to a nonexistent pipe. The process tried to write to a nonexistent pipe. The process tried to write to a nonexistent pipe. The process tried to write to a nonexistent pipe. The process tried to write to a nonexistent pipe. – user8785018 Jan 19 '18 at 10:46
  • Please explain the process you used to try to close the window; _and please try to use my correct name_. – Compo Jan 19 '18 at 11:01
  • @user8785018: **don't** name your batchfile `arp.bat`! – Stephan Jan 19 '18 at 11:32
  • ... or have some other `arp.bat` around. – Stephan Jan 19 '18 at 11:52
  • … or `arp.cmd`! – Compo Jan 19 '18 at 11:57
  • Compo! I want to apologize for typing your name wrong! Sorry :( – user8785018 Jan 19 '18 at 12:28
  • I wasn't being serious about the name thing… Did you determine if it was your script filename or whether there is a file in the current directory or a location defined in `%PATH%` which also has the name `arp` with an extension as defined in `%PATHEXT%`?. _(you can see both of those things by entering `Set Path` at the Command prompt)_. – Compo Jan 19 '18 at 13:14
  • My file called test.bat is located in directory c:\1\. Set Path command is: PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC Can you do this differently? What I want is that I only have MAC addresses med "arp -a" command from the netwerk printed to a text file. I intended to send %%i to set "MAC = %% i" and @echo! MAC !. But the loop (For) does not! work. Thanks for all help! – user8785018 Jan 19 '18 at 15:52
  • 1
    What is returned if you enter `Where arp` into your Command prompt? and you still haven't explained what you meant by, and how you were trying to close the window – Compo Jan 19 '18 at 16:21
  • c:\1>Where arp C:\Windows\System32\ARP.EXE – user8785018 Jan 19 '18 at 16:24
  • Did you actually enter the commands into `notepad.exe` as I asked, and not some other editor, or did you think the fact I said `notepad.exe` was less important than it was. I need to determine that the content matches the ASCII text I used in my answer! – Compo Jan 19 '18 at 16:28
  • close the window: X close button in the top-right corner of the window. – user8785018 Jan 19 '18 at 16:32
  • Just double click the batch file, it will close itself when you press the pause button; or add `Exit` as a new last line; or just enter `Exit` at the prompt after running your script in the usual way. Or open a new Command prompt window and just enter this `Call C:\1\test.bat` – Compo Jan 19 '18 at 16:33
  • Nothing happen! If I press enter or pause nothing happens. Why do I try to close cmd windows with X close button and come: ^C^The process tried to write to a nonexistent pipe. – user8785018 Jan 19 '18 at 16:41
  • If I do not close cmd windows computer get very slow! I guess (?) it starts processes in the background with cmd that uses memory! In the end, I have to turn off the computer with the off button. – user8785018 Jan 19 '18 at 16:54
  • I have updated my answer, just add that single line, exactly like it is into notepad and save as `test.bat` overwriting the existing one. Now run it by doubleclicking it. The result you want will be written to a file named output.txt in the same directory! – Compo Jan 19 '18 at 17:00
  • Thanks thanks thanks! :) I am very grateful for your patience and help and respect your knowledge. My final print in output.txt should have compiler names as well as me. Names should be same with increased numbers, from 1 to as long as arp finds mac addresses. Ex. two-column printing with between MAC Computername1 MAC Computername2 – user8785018 Jan 19 '18 at 17:44
0

to add the computername (when I interpret your comment correct):

@echo off
for /f "tokens=1,2" %%a in ('arp -a^|find "dynam"') do (
  for /f "tokens=5" %%c in ('ping -a -n 1 %%a^|find "["') do (
    >>"%~dp0output.txt" echo %%a    %%b %%c
  )
)

shows IP-address, MAC-address, Computer-name. Adapt the echo line to your needs.
You may have to adapt the tokens (tokens=5) (inner for) for your language.
You may want to add delims=. to the inner for to get the computer name only.

according to your comments:

@echo off
setlocal EnableDelayedExpansion
set number=0
for /f "tokens=1,2" %%a in ('arp -a^|find "dynam"') do (
  set /a number +=1
  for /f "tokens=5" %%c in ('ping -a -n 1 %%a^|find "["') do (
    >>"%~dp0output.txt" echo %%b Computer!number!
  )
)

Output:

00-24-fe-xx-xx-xx  Computer1
f4-f5-e8-xx-xx-xx  Computer2
48-d6-d5-xx-xx-xx  Computer3
d4-85-64-xx-xx-xx  Computer4
80-86-f2-xx-xx-xx  Computer5
24-77-03-xx-xx-xx  Computer6
fc-db-b3-xx-xx-xx  Computer7
80-1f-02-xx-xx-xx  Computer8
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thank you that was great. But I want the output text to print new names on computers that only increase numbers on them, from eg computername1, computername2, etc. up to how much MAC addressees are available. I tried to resolve this with new variable set / A NUMBER + = 1 and send to echo coputername%number%, but it only writes computername1 to everyone. Probably it does not go to FOR lopp. How can I solve this? Thanks for any help! – user8785018 Jan 20 '18 at 11:17
  • you are nearly there. Read about [delayed expansion](https://stackoverflow.com/a/30284028/2152082). – Stephan Jan 20 '18 at 12:18
  • Note that `arp -a` lists also old entries (computers that may be no longer available). If you just want active nodes, use `arp -d` before (clear chache), followed by a `ping -n 1 xxx.xx.xxx.255` ('xxx.xx.xxx' = your subnet, 255 for "Broadcast") – Stephan Jan 20 '18 at 12:23
  • I tried it setlocal enabledelayedexpansion in still output text is the same computername1 for all computers. – user8785018 Jan 20 '18 at 12:44
  • did you think to change `coputername%number%` to `coputername!number!` too? – Stephan Jan 20 '18 at 12:45
  • I have tested both of them coputername%number% and coputername!number! But it is the same result, printing comutername1. – user8785018 Jan 20 '18 at 12:50
0

I've received this error repeatedly when accidentally piping a batch script to MSYS grep when I really meant to grep a file afterward:

> call foo.cmd | grep pattern path\to\file
The process tried to write to a nonexistent pipe.
The process tried to write to a nonexistent pipe.
The process tried to write to a nonexistent pipe.
The process tried to write to a nonexistent pipe.
...

I haven't been able to identify exactly what foo.cmd needs to do to result in this error (my foo.cmd is pretty long and complicated), but I figured I'd add this here anyway. Hopefully it helps someone (even myself in a few months). What I meant to say was:

> call foo.cmd && grep pattern path\to\file
bambams
  • 745
  • 1
  • 8
  • 18