Make a Bat file to auto upload sketch

I am trying to make a Bat file that waits for a board to be connected then uploads the code.
Here is the bat file so far:

@echo off
setlocal enabledelayedexpansion

:loop
echo Waiting for XIAO ESP32S3 board to be connected...
for /f "tokens=1,2 delims=:" %%a in ('mode') do (
  set port=%%a
  if "!port:~0,3!"=="COM" (
    echo Detected port: !port!
    arduino-cli board list | findstr /C:"XIAO_ESP32S3" >nul
    if !errorlevel! equ 0 (
      echo Xiao ESP32S3 board detected on !port!
      echo Uploading sketch...
      arduino-cli upload --port !port! --fqbn esp32:esp32:XIAO_ESP32S3 "D:Receiver.ino"
      echo Upload complete. Please disconnect the board and connect the next one.
      pause
    )
  )
)
goto loop

Where I am still having issues is that the Bat file does not detect a COM port. Does anyone have any suggestions? thanks

Care to share your solution? That's what this forum is all about.

Use the error to show which port is in use... and assume it is your target.

@echo off
setlocal enabledelayedexpansion

:loop
echo Waiting for Xiao ESP32S3 board to be connected...
REM Loop through all serial ports---- This is a note
for /f "tokens1=2 delims==" %%a in ('wmic path Win32_SerialPort get DeviceID /value') do (
  set port=%%a
  REM Remove any trailing whitespace or carriage return characters
  set port=!port:~0,-1!
  REM Check if the port starts with "COM"
  if "!port:~0,3!"=="COM" (
    echo Detected port: !port!
    REM Check if the detected a port
    if !errorlevel! equ 0 (
      echo Xiao ESP32S3 board detected on !port!
      echo Uploading sketch...
      REM make sure the file path is correct
      arduino-cli upload --port !port! --fqbn esp32:esp32:XIAO_ESP32S3 "File Path/program.ino"
      echo Upload complete. Please disconnect the board and connect the next one.
      pause
    )
  )
)
goto loop

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.