# Stage TXT Import Task
# Date: 2026-03-28
# Context: CTONWTXT.BAT now uploads C:\STAGE\*.TXT from DOS machines to T:\STAGE\%MACHINE%\

## What happened

1. CTONWTXT.BAT was never being called -- fixed, now called from CTONW.BAT on every boot
2. Destination changed from broken X: (Novell serve.sys check) to T:\STAGE\%MACHINE%\
3. DOS 6.22 can't MD on existing dirs without error, so dirs are pre-created on NAS
4. All TS-* machine folders pre-created under /data/test/STAGE/ on D2TESTNAS

## What needs to run

Save the script below as C:\Shares\testdatadb\import-all-stage.js and run it:

    cd C:\Shares\testdatadb
    node import-all-stage.js

## What it does

- Scans \\D2TESTNAS\test\STAGE\TS-*\*.TXT (~8,100 files across 10 machines)
- Parses each TXT datasheet (Date, Model, SN)
- Decodes hex-prefix serial numbers for 8.3 filename encoding:
    - Letter prefix = hex digit: A=10, B=11, C=12, ..., H=17, etc.
    - Example: H8236-12.TXT has SN: 178236-12 inside the file
    - Example: A819-1.TXT has SN: A819-1 inside -> decoded to 10819-1
    - The SN line inside H-prefix files already has the full numeric serial
    - The SN line inside A-prefix files still has the encoded serial
- Cross-references against testdata.db by (serial_number, model_number)
- Inserts MISSING records as log_type='SHT' with test_station from folder name
- Copies ALL files to X:\For_Web\{decoded_serial}.TXT (the web share)

## Machines with data

TS-4L:  3,082 files (largest)
TS-4R:  2,741 files
TS-1R:    509 files
TS-8R:    478 files
TS-3R:    435 files
TS-11R:   325 files
TS-8L:    285 files
TS-11L:   248 files
TS-27:     10 files (already imported this session)
TS-1L:      1 file

## Serial number encoding (8.3 filename scheme)

The QuickBASIC ATE software encodes long serial numbers to fit DOS 8.3 filenames.
The first two digits get replaced with a hex letter if the serial is too long:

    178236-12  ->  H8236-12.TXT  (17 -> H, which is char code 72, 72-55=17)
    10819-1    ->  A819-1.TXT    (10 -> A, which is char code 65, 65-55=10)

Decode: letter.charCodeAt(0) - 55 = numeric prefix
Only applies if filename starts with [A-Z] followed by digits.

## TS-27 already done

10 files from TS-27 were already imported earlier this session into the DB as SHT records.
The import script uses INSERT OR REPLACE so re-running is safe.

## Previous CTONWTXT.BAT issues (resolved)

- v1.0: Never called, checked for Novell serve.sys, used X: drive parameter
- v2.0: Called from CTONW, but used mixed-case "Stage" path -> failed on DOS
- v2.1: All uppercase STAGE, but had MD commands that fail on existing dirs
- v2.2: Same issue
- v2.3: Removed MD entirely, dirs pre-created on NAS. CURRENT VERSION.
