@echo off
chcp 65001 >nul
setlocal EnableDelayedExpansion

:: Ask the user to enter the port number
set /p PORT=Enter the port number to CHECK: 

:: Find the PID of the process that's using this port (UDP only)
for /f "tokens=5" %%a in ('netstat -aon ^| findstr :%PORT% ^| findstr /i "UDP"') do (
    set PID=%%a
)

:: If no process was found using that port
if not defined PID (
    echo No process found using port %PORT%.
    pause
    exit /b
)

:: Display the PID of the found process
echo Found a process with PID: %PID%

:: Get the name of the process using the PID
for /f "tokens=*" %%a in ('wmic process where processid^=%PID% get name ^| findstr /v "Name"') do (
    set PROC_NAME=%%a
)

:: Show the process name
echo Process name: %PROC_NAME%
echo.

:: Ask if the user wants to terminate the process
set /p CHOICE=Do you want to terminate this process? (Y/N): 

if /i "%CHOICE%"=="Y" (
    echo Terminating %PROC_NAME% (PID: %PID%)...
    taskkill /PID %PID% /F
    echo Process terminated.
) else (
    echo The process was NOT terminated.
)

pause
exit /b
