fix: Insert SET MACHINE at beginning of AUTOEXEC.BAT instead of appending to end

Issue: User reported "[ERROR] MACHINE variable not set" during boot,
even though DEPLOY.BAT successfully added SET MACHINE=TS-4R to AUTOEXEC.BAT.

Root cause: Using >> operator APPENDS to END of AUTOEXEC.BAT. If any
scripts, CALL commands, or other code runs before the end of AUTOEXEC.BAT
(like STARTNET.BAT, UPDATE.BAT, or other network/backup scripts), the
MACHINE variable is not yet set when those scripts run.

Solution: INSERT SET MACHINE at LINE 2 (right after @ECHO OFF), ensuring
it's set BEFORE any other commands or scripts execute.

Implementation:
1. If AUTOEXEC.BAT exists:
   - Create temp file with @ECHO OFF
   - Add SET MACHINE=%MACHINE%
   - Append rest of AUTOEXEC.BAT (excluding duplicate @ECHO OFF)
   - Replace original with temp file

2. If AUTOEXEC.BAT doesn't exist:
   - Create new file with @ECHO OFF and SET MACHINE

Changes:
BEFORE (appended to end):
  @ECHO OFF
  ... existing commands ...
  ... CALL scripts that need MACHINE ...
  SET MACHINE=TS-4R  ← TOO LATE!

AFTER (inserted at beginning):
  @ECHO OFF
  SET MACHINE=TS-4R  ← SET FIRST!
  ... existing commands ...
  ... CALL scripts that need MACHINE ... ← MACHINE already set

Files modified:
- DEPLOY.BAT: Lines 263-287 (ADD_MACHINE_VAR section)
- Now creates C:\AUTOEXEC.TMP for safe insertion
- Displays: "(Inserted at beginning, before other commands)"

Deployed to: D2TESTNAS /data/test/DEPLOY.BAT (10,564 bytes)

Testing: After reboot, MACHINE variable should be set before any
network/backup scripts run, eliminating "[ERROR] MACHINE variable not set"

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-19 17:40:14 -07:00
parent 15d1386e82
commit 5bb9df53ec

View File

@@ -262,11 +262,27 @@ IF NOT ERRORLEVEL 1 GOTO MACHINE_EXISTS
:ADD_MACHINE_VAR
REM Append MACHINE variable to AUTOEXEC.BAT
ECHO SET MACHINE=%MACHINE% >> C:\AUTOEXEC.BAT
IF ERRORLEVEL 1 GOTO AUTOEXEC_ERROR
REM Insert MACHINE variable at beginning of AUTOEXEC.BAT (not end!)
REM This ensures it's set before any scripts or commands run
IF NOT EXIST C:\AUTOEXEC.BAT GOTO CREATE_NEW_AUTOEXEC
REM AUTOEXEC.BAT exists - insert SET MACHINE at line 2 (after @ECHO OFF)
ECHO @ECHO OFF > C:\AUTOEXEC.TMP
ECHO SET MACHINE=%MACHINE% >> C:\AUTOEXEC.TMP
TYPE C:\AUTOEXEC.BAT | FIND /V "@ECHO OFF" >> C:\AUTOEXEC.TMP
COPY C:\AUTOEXEC.TMP C:\AUTOEXEC.BAT >NUL
DEL C:\AUTOEXEC.TMP
GOTO MACHINE_ADD_DONE
:CREATE_NEW_AUTOEXEC
REM No AUTOEXEC.BAT exists - create new one
ECHO @ECHO OFF > C:\AUTOEXEC.BAT
ECHO SET MACHINE=%MACHINE% >> C:\AUTOEXEC.BAT
:MACHINE_ADD_DONE
ECHO [OK] Added to AUTOEXEC.BAT: SET MACHINE=%MACHINE%
ECHO (Inserted at beginning, before other commands)
ECHO.
GOTO INSTALL_BATCH_FILES