0

I am trying to write a command to a file so that it can be run in the future. How do I write the line netstat -ano | findstr :25565 >pid.txt to the file stop.bat?

Code:

echo @echo off> stop.bat
echo netstat -ano | findstr :25565 >pid.txt>> stop.bat
echo FOR /F "usebackq tokens=5" %%%%A in (pid.txt) do (>> stop.bat
echo    taskkill /F /PID %%%%A>> stop.bat
echo    exit /b>> stop.bat
echo )>> stop.bat

Output in file "stop.bat":

@echo off
FOR /F "usebackq tokens=5" %%A in (pid.txt) do (
    taskkill /F /PID %%A
    exit /b
)
Compo
  • 36,585
  • 5
  • 27
  • 39
John Smith
  • 41
  • 6
  • 3
    You use the carrot as an escape character to use those special characters as literal characters. `echo ^|^>` – Squashman Dec 27 '21 at 01:43

1 Answers1

0

Thanks @Squashman, fixed it by using ^ before each literal character soecho netstat -ano | findstr :25565 >pid.txt>> stop.bat has changed to echo netstat -ano ^| findstr :25565 ^>pid.txt>> stop.bat and the file output now reads:

@echo off
netstat -ano | findstr :25565 >pid.txt
FOR /F "usebackq tokens=5" %%A in (pid.txt) do (
    taskkill /F /PID %%A
    exit /b
)
John Smith
  • 41
  • 6