Windows Batch error: “'ping' is not recogn

2020-04-11 04:23发布

问题:

I am trying to run this command in windows:

ping -n 5 127.0.0.1 > nul

I get the error:

'ping' is not recognized as an internal or external command operable 
program or batch fie. 

Why can't windows find ping? Here is my script where it does not work:

@ECHO OFF

::set path
SET path=M:\\5.bmp

:findfile
IF EXIST %path% (
    ECHO File found
) ELSE (
    ECHO File not found
    ping -n 5 127.0.0.1 > nul
    goto findfile
)

回答1:

You have overridden the PATH environment variable, so the command processor can no longer find the ping executable.

The fix is nice and simple - just use a different variable name!

:: set path
SET MyPath=M:\\5.bmp

:findfile
IF EXIST %MyPath% (

Please note that if you genuinely wanted to set the path environment variable, you should append to it like so:

REM Set temporarily for this session
SET PATH=%PATH%;C:\Some\Folder

REM Set permanently (but note - this change will not be made to this session)
SETX PATH=%PATH%;C:\Some\Folder


回答2:

Hi to fix "ping" please follow below steps

Go to Properties in My computer

Advanced system settings ----->Advanced -----> Environment Variables Select 'PATH' from the list of system variables and edit and set PATH to c:\windows\system32 ; this will solve your problem.

-----> if still u have a problem, do the below steps...

Control Panel --> System and security --> Windows Firewall --> Advanced settings --> Inbound rules --> New rule --> custom rule

in Protocol and ports: Protocol: ICMPv4 on the same panel go to customize, choose "Specific ICMP types", check the box "echo request"

The rest is trivial; go to next... next... and save it.

You should be done. This box responds ping from this point on.

Cheers

Prasad



回答3:

How to diagnose this error:

'ping' is not recognized as an internal or external command operable 
program or batch fie. 

Because your path environment variable does not contain the directory that points to the executable: ping.exe. So the question becomes why can't your command line program cmd.exe locate ping.exe?

You can print out your path variable on the commandline like this:

echo %PATH%

Which prints for me:

C:\WINDOWS;C:\WINDOWS\system32;C:\Program Files\jZip;C:\MinGW\bin

The above string is delimited by semicolons(;). Notice the element called: C:\WINDOWS\System32, that is the element that defines where ping.exe can be located.

Solutions

  1. You didn't specify C:\WINDOWS\System32 in your path variable. Make sure it is there.
  2. ping.exe is missing. Find out who deleted it and put it back.
  3. ping.exe is corrupt. Run it where it sits: C:\WINDOWS\System32\ping.exe, or replace ping.exe.
  4. You have overridden the PATH variable before you look for it.