# NWTOC.BAT System Analysis - Dataforth DOS Machine Updates

**Analysis Date:** 2026-01-19
**System:** DOS 6.22 with Microsoft Network Client 3.0
**Target Machines:** TS-4R, TS-7A, TS-12B, and other Dataforth test stations

---

## Current State

### Existing Infrastructure

**UPDATE.BAT (Backup - Computer to Network)**
- Backs up entire C:\ to T:\[MACHINE]\BACKUP
- Uses XCOPY /S /E /Y /D /H /K /C /Q
- Supports machine name from %MACHINE% environment variable or command-line parameter
- Fixed for DOS 6.22 on 2026-01-19
- Status: WORKING

**STARTNET.BAT (Network Client Startup)**
- Starts Microsoft Network Client (NET START)
- Maps T: to \\D2TESTNAS\test
- Maps X: to \\D2TESTNAS\datasheets
- Called from AUTOEXEC.BAT during boot
- Status: WORKING

**AUTOEXEC.BAT (System Startup)**
- Sets MACHINE environment variable (e.g., SET MACHINE=TS-4R)
- Configures PATH, PROMPT, TEMP
- Calls STARTNET.BAT to initialize network
- Mentions NWTOC and CTONW commands but they don't exist yet
- Status: WORKING, needs NWTOC/CTONW integration

### Missing Components

**NWTOC.BAT (Network to Computer - MISSING)**
- Should pull updates from T:\COMMON\ProdSW\ and T:\[MACHINE]\ProdSW\
- Should update C:\BAT\, C:\ATE\, C:\NET\
- Should handle AUTOEXEC.BAT and CONFIG.SYS updates safely
- Should trigger reboot when system files change
- **Status: DOES NOT EXIST - Must create**

**CTONW.BAT (Computer to Network - MISSING)**
- Should upload local changes to network for sharing
- Counterpart to NWTOC.BAT
- **Status: DOES NOT EXIST - Must create**

---

## Update Workflow Architecture

### Update Path Flow

```
STEP 1: Admin Places Updates
   \\AD2\test\COMMON\ProdSW\*.bat          → All machines get these
   \\AD2\test\COMMON\DOS\AUTOEXEC.NEW      → New AUTOEXEC.BAT for all
   \\AD2\test\COMMON\DOS\CONFIG.NEW        → New CONFIG.SYS for all
   \\AD2\test\TS-4R\ProdSW\*.*             → Machine-specific updates

STEP 2: NAS Sync (Automatic, bidirectional)
   D2TESTNAS: /root/sync-to-ad2.sh
   Syncs: \\AD2\test ↔ /mnt/test (NAS local storage)
   Frequency: Every 15 minutes (cron job)

STEP 3: DOS Machine Update (Manual or Automatic)
   User runs: NWTOC
   Or: Called from AUTOEXEC.BAT at boot

   T:\COMMON\ProdSW\*.bat       → C:\BAT\
   T:\TS-4R\ProdSW\*.bat        → C:\BAT\
   T:\TS-4R\ProdSW\*.exe        → C:\ATE\
   T:\COMMON\DOS\AUTOEXEC.NEW   → C:\AUTOEXEC.BAT (via staging)
   T:\COMMON\DOS\CONFIG.NEW     → C:\CONFIG.SYS (via staging)

STEP 4: Reboot (If system files changed)
   NWTOC.BAT detects AUTOEXEC.NEW or CONFIG.NEW
   Calls STAGE.BAT to prepare reboot
   STAGE.BAT modifies AUTOEXEC.BAT to call REBOOT.BAT once
   User reboots (or automatic reboot)
   REBOOT.BAT applies changes, deletes itself
```

---

## Critical Problems to Solve

### Problem 1: System File Updates Are Dangerous

**Issue:** Cannot overwrite AUTOEXEC.BAT or CONFIG.SYS while DOS is running

**Why it matters:**
- COMMAND.COM keeps files open
- Overwriting causes corruption or crash
- System becomes unbootable if interrupted

**Solution: File Staging**
```bat
REM NWTOC.BAT detects new system files
IF EXIST T:\COMMON\DOS\AUTOEXEC.NEW GOTO STAGE_UPDATES
IF EXIST T:\COMMON\DOS\CONFIG.NEW GOTO STAGE_UPDATES

:STAGE_UPDATES
REM Copy to staging area
COPY T:\COMMON\DOS\AUTOEXEC.NEW C:\AUTOEXEC.NEW
COPY T:\COMMON\DOS\CONFIG.NEW C:\CONFIG.NEW

REM Call staging script
CALL C:\BAT\STAGE.BAT

REM Tell user to reboot
ECHO.
ECHO [WARNING] System files updated - reboot required
ECHO.
ECHO Run: REBOOT command or press Ctrl+Alt+Del
PAUSE
```

### Problem 2: Users Don't Know When to Reboot

**Issue:** System file changes require reboot but user doesn't know

**Why it matters:**
- Updated AUTOEXEC.BAT doesn't take effect until reboot
- Machine runs with outdated configuration
- New software might depend on new environment variables

**Solution: Automatic Reboot Detection**
```bat
REM STAGE.BAT modifies AUTOEXEC.BAT to run REBOOT.BAT once

REM Backup current AUTOEXEC.BAT
COPY C:\AUTOEXEC.BAT C:\AUTOEXEC.SAV

REM Add one-time reboot call to top of AUTOEXEC.BAT
ECHO @ECHO OFF > C:\AUTOEXEC.TMP
ECHO IF EXIST C:\BAT\REBOOT.BAT CALL C:\BAT\REBOOT.BAT >> C:\AUTOEXEC.TMP
TYPE C:\AUTOEXEC.BAT >> C:\AUTOEXEC.TMP
COPY C:\AUTOEXEC.TMP C:\AUTOEXEC.BAT
DEL C:\AUTOEXEC.TMP

REM Create REBOOT.BAT
ECHO @ECHO OFF > C:\BAT\REBOOT.BAT
ECHO ECHO Applying system updates... >> C:\BAT\REBOOT.BAT
ECHO IF EXIST C:\AUTOEXEC.NEW COPY C:\AUTOEXEC.NEW C:\AUTOEXEC.BAT >> C:\BAT\REBOOT.BAT
ECHO IF EXIST C:\CONFIG.NEW COPY C:\CONFIG.NEW C:\CONFIG.SYS >> C:\BAT\REBOOT.BAT
ECHO DEL C:\AUTOEXEC.NEW >> C:\BAT\REBOOT.BAT
ECHO DEL C:\CONFIG.NEW >> C:\BAT\REBOOT.BAT
ECHO COPY C:\AUTOEXEC.SAV C:\AUTOEXEC.BAT >> C:\BAT\REBOOT.BAT
ECHO DEL C:\BAT\REBOOT.BAT >> C:\BAT\REBOOT.BAT
```

### Problem 3: File Update Verification

**Issue:** How do we know if update succeeded or failed?

**Why it matters:**
- Network glitch could corrupt files
- Partial updates leave machine broken
- No way to roll back

**Solution: Date/Size Comparison and Backup**
```bat
REM Use XCOPY /D to copy only newer files
XCOPY /D /Y T:\COMMON\ProdSW\*.bat C:\BAT\

REM Keep .BAK backups
FOR %%F IN (C:\BAT\*.BAT) DO (
    IF EXIST %%F COPY %%F %%~nF.BAK
)

REM Verify critical files
IF NOT EXIST C:\BAT\NWTOC.BAT GOTO UPDATE_FAILED
IF NOT EXIST C:\BAT\UPDATE.BAT GOTO UPDATE_FAILED
```

### Problem 4: Update Order Dependencies

**Issue:** Files might depend on each other (PATH changes, new utilities)

**Why it matters:**
- New batch files might call new executables
- New AUTOEXEC.BAT might reference new directories
- Wrong order = broken system

**Solution: Staged Update Order**
```bat
REM 1. Update system files first (staged for reboot)
REM    AUTOEXEC.BAT, CONFIG.SYS

REM 2. Update network client files
REM    C:\NET\*.* (if needed)

REM 3. Update batch files
REM    C:\BAT\*.bat

REM 4. Update test programs last
REM    C:\ATE\*.*
```

---

## DOS 6.22 Limitations

### Cannot Use (These are Windows NT/2000/XP features)

- `IF /I` (case-insensitive) → Must use exact case
- `%ERRORLEVEL%` variable → Must use `IF ERRORLEVEL n`
- `FOR /F` loops → Only simple FOR loops work
- `&&` and `||` operators → Must use GOTO
- Long filenames → 8.3 only (NWTOC.BAT not NETWORK-TO-COMPUTER.BAT)
- `IF EXIST path\*.ext` with wildcards → Must use DIR or FOR loop

### Must Use

- `IF ERRORLEVEL n` checks if errorlevel >= n (not ==)
- Check highest error levels first (5, 4, 2, 1, 0)
- Case-sensitive string comparison (`TS-4R` ≠ `ts-4r`)
- `CALL` for batch file subroutines
- `GOTO` labels for flow control
- FOR loops: `FOR %%F IN (*.TXT) DO ECHO %%F`

### Checking for Drive Existence

**WRONG:**
```bat
IF EXIST T:\ GOTO DRIVE_OK
IF "%T%"=="" ECHO No T drive
```

**CORRECT:**
```bat
REM Method 1: Try to switch to drive
T: 2>NUL
IF ERRORLEVEL 1 GOTO NO_T_DRIVE
C:
GOTO DRIVE_OK

REM Method 2: Check for NUL device
IF NOT EXIST T:\NUL GOTO NO_T_DRIVE
```

### Checking for Files with Wildcards

**WRONG:**
```bat
IF EXIST T:\COMMON\DOS\*.NEW GOTO HAS_UPDATES
```

**CORRECT:**
```bat
REM Use FOR loop
SET HASUPDATES=0
FOR %%F IN (T:\COMMON\DOS\*.NEW) DO SET HASUPDATES=1
IF "%HASUPDATES%"=="1" GOTO HAS_UPDATES
```

---

## File Organization

### Network Share Structure

```
T:\ (\\D2TESTNAS\test)
├── COMMON\                 # Files for all machines
│   ├── ProdSW\            # Production software (batch files, tools)
│   │   ├── NWTOC.BAT      # Update script (all machines get this)
│   │   ├── UPDATE.BAT     # Backup script
│   │   ├── CHECKUPD.BAT   # Check for updates
│   │   └── *.bat          # Other batch files
│   └── DOS\               # DOS system files
│       ├── AUTOEXEC.NEW   # New AUTOEXEC.BAT for deployment
│       ├── CONFIG.NEW     # New CONFIG.SYS for deployment
│       └── *.SYS          # Device drivers
├── TS-4R\                 # Machine-specific files
│   ├── BACKUP\            # Full machine backup (UPDATE.BAT writes here)
│   └── ProdSW\            # Machine-specific software
│       ├── *.bat          # Custom batch files for this machine
│       ├── *.exe          # Test programs for this machine
│       └── *.dat          # Configuration data
├── TS-7A\                 # Another machine
└── _SYNC_STATUS.txt       # NAS sync status (monitored by RMM)
```

### Local DOS Machine Structure

```
C:\
├── AUTOEXEC.BAT           # System startup (sets MACHINE variable)
├── AUTOEXEC.SAV           # Backup before staging
├── AUTOEXEC.NEW           # Staged update (if present)
├── CONFIG.SYS             # System configuration
├── CONFIG.NEW             # Staged update (if present)
├── DOS\                   # MS-DOS 6.22 files
├── NET\                   # Microsoft Network Client 3.0
│   ├── PROTOCOL.INI       # Network configuration
│   ├── STARTNET.BAT       # Network startup script
│   └── *.DOS              # Network drivers
├── BAT\                   # Batch file directory
│   ├── NWTOC.BAT          # Network to Computer (get updates)
│   ├── CTONW.BAT          # Computer to Network (push changes)
│   ├── UPDATE.BAT         # Backup to network
│   ├── STAGE.BAT          # Stage system file updates
│   ├── REBOOT.BAT         # Apply updates after reboot (auto-deletes)
│   ├── CHECKUPD.BAT       # Check for updates without applying
│   └── *.BAK              # Backup copies of batch files
├── ATE\                   # Test programs (Automated Test Equipment)
│   ├── *.EXE              # Test executables
│   ├── *.DAT              # Test data files
│   └── *.LOG              # Test result logs
└── TEMP\                  # Temporary files
```

---

## Success Criteria

### Updates Must Work Automatically
- User runs `NWTOC` command
- All newer files are copied from network
- System files are staged properly
- User is clearly notified of reboot requirement
- Progress is visible and doesn't scroll off screen

### System Files Update Safely
- AUTOEXEC.BAT and CONFIG.SYS are never corrupted
- Backup copies are always created (.SAV files)
- Updates are atomic (all or nothing via staging)
- Rollback is possible if update fails

### Reboot Happens When Needed
- STAGE.BAT detects system file changes
- AUTOEXEC.BAT is modified to call REBOOT.BAT once
- REBOOT.BAT applies changes and self-deletes
- Normal AUTOEXEC.BAT is restored after update
- User sees clear "reboot required" message

### Errors Are Visible
- Don't scroll off screen (use PAUSE on errors)
- Show clear [OK], [WARNING], [ERROR] markers
- Indicate what went wrong (drive not mapped, file not found, etc.)
- Provide recovery instructions

### Progress Is Clear
- Show what's being updated
- Show where files are coming from/going to
- Show file count or progress indicator
- Compact output (one line per operation)

### Rollback Is Possible
- Keep .BAK files of all batch files
- Keep .SAV files of system files
- Document rollback procedure in comments
- Allow manual restoration if needed

---

## Implementation Plan

### Phase 1: Core Update Scripts (Priority 1)
1. **NWTOC.BAT** - Network to Computer update
   - Copy batch files from T:\COMMON\ProdSW\ → C:\BAT\
   - Copy machine-specific files from T:\%MACHINE%\ProdSW\ → C:\BAT\ and C:\ATE\
   - Detect AUTOEXEC.NEW and CONFIG.NEW
   - Call STAGE.BAT if system files need updating
   - Show clear progress and status

2. **STAGE.BAT** - Prepare for system file update
   - Copy AUTOEXEC.NEW → C:\AUTOEXEC.NEW
   - Copy CONFIG.NEW → C:\CONFIG.NEW
   - Backup current AUTOEXEC.BAT → C:\AUTOEXEC.SAV
   - Create REBOOT.BAT
   - Modify AUTOEXEC.BAT to call REBOOT.BAT once
   - Show "reboot required" warning

3. **REBOOT.BAT** - Apply staged updates (runs once after reboot)
   - Check if running (first line of AUTOEXEC.BAT)
   - Apply AUTOEXEC.NEW → AUTOEXEC.BAT
   - Apply CONFIG.NEW → CONFIG.SYS
   - Delete staging files (.NEW files)
   - Restore original AUTOEXEC.BAT (remove REBOOT.BAT call)
   - Delete itself
   - Show completion message

### Phase 2: Supporting Scripts (Priority 2)
4. **CTONW.BAT** - Computer to Network
   - Opposite of NWTOC.BAT
   - Upload local changes to T:\%MACHINE%\ProdSW\
   - Used when testing new batch files locally
   - Allows sharing between machines

5. **CHECKUPD.BAT** - Check for updates
   - Compare file dates: T:\COMMON\ProdSW\ vs C:\BAT\
   - Report what would be updated
   - Don't actually copy files
   - Quick status check

### Phase 3: Integration (Priority 3)
6. Update AUTOEXEC.BAT
   - Add optional NWTOC call (commented out by default)
   - Add CHECKUPD call to show status on boot
   - Document MACHINE variable requirement

7. Create deployment documentation
   - DEPLOYMENT_GUIDE.md - How to deploy updates
   - UPDATE_WORKFLOW.md - Complete workflow explanation
   - TROUBLESHOOTING.md - Common issues and fixes

---

## Next Steps

1. Create NWTOC.BAT with full DOS 6.22 compatibility
2. Create STAGE.BAT for safe system file updates
3. Create REBOOT.BAT for post-reboot application
4. Create CHECKUPD.BAT for status checking
5. Create CTONW.BAT for uploading local changes
6. Create comprehensive documentation
7. Test on actual TS-4R machine
8. Deploy to all Dataforth DOS machines

---

## References

- **DOS_BATCH_ANALYSIS.md** - Original UPDATE.BAT analysis and DOS 6.22 limitations
- **UPDATE.BAT** - Working backup script (C:\ to network)
- **STARTNET.BAT** - Network client startup script
- **AUTOEXEC.BAT** - System startup script with MACHINE variable
- **Dec 14, 2025 Session** - Original NWTOC/CTONW batch files (imported conversation)
- **File Structure Documentation** - .claude/FILE_ORGANIZATION.md

---

**Status:** Analysis complete, ready for implementation
**Author:** Claude Code (coordinator)
**Date:** 2026-01-19
