Przejdź do treści
Biblioteka skilli
Programowanie · Budowanie i kod

win-gbk-bat

/win-gbk-bat

Pisze i naprawia pliki .bat i .cmd z chińskimi znakami na komputerach z chińskimi ustawieniami regionalnymi, bez zniekształceń w wyjściu.

Licencja
MIT
Ostatnia zmiana repozytorium
05.11.2026

Ocena i bezpieczeństwo

98/ 100Klasa: dobry
Format25 / 25
Frontmatter z nazwą i opisem, poprawna nazwa, opis do 1024 znaków, nazwa zgodna z folderem.
Odkrywalność25 / 25
Opis mówi, kiedy użyć skilla, ma sensowną długość i jest w 3. osobie.
Treść35 / 35
Co najmniej 150 słów i do 500 linii, nagłówki, listy kroków, przykłady albo kod.
Higiena13 / 15
Bez zaślepek typu TODO, bez ścieżek Windows, z licencją.
Ryzykobrak sygnałów w skanie
Pliki w paczce
2
Skrypty
1
Komentarze HTML
0
Adresy URL w SKILL.md
0
Przestarzałość

Brak sygnałów. Repozytorium zmienione 05.11.2026, bez odwołań do wycofanych modeli i API.

Licencja

MIT, otwarta: można używać i zmieniać, z zachowaniem informacji o autorze.

Co sprawdzić przed instalacją
  • 1 skrypt w paczce. Przejrzyj je przed instalacją, bo Claude może je uruchomić.

Skrypty: scripts/gbk_bat.ps1

Co robi i kiedy

Pisze i naprawia pliki .bat i .cmd z chińskimi znakami na komputerach z chińskimi ustawieniami regionalnymi, bez zniekształceń w wyjściu.

Wyzwalacze z opisu

不是内部或外部命令
Oryginalny opis (EN)

Write and debug Windows .bat/.cmd batch files containing Chinese (or any non-ASCII) text on Chinese-locale Windows. Use whenever creating or editing a .bat/.cmd that contains 中文, or when a batch script misparses/garbles Chinese — symptoms include "不是内部或外部命令" on lines that look fine, ????? replacing Chinese in output, commands fragmented mid-line, or echo printing mojibake. Also covers chcp pitfalls and PowerShell 5.1 encoding traps.

Jak działa

  1. The reliable procedure
  2. Converter usage
  3. Rules that prevent the classic failures
  4. Worked example (from a real incident)
Sekcje z nagłówków SKILL.md, w oryginale · 58 linii, 632 słów
Podgląd SKILL.md
---
name: win-gbk-bat
description: Write and debug Windows .bat/.cmd batch files containing Chinese (or any non-ASCII) text on Chinese-locale Windows. Use whenever creating or editing a .bat/.cmd that contains 中文, or when a batch script misparses/garbles Chinese — symptoms include "不是内部或外部命令" on lines that look fine, ????? replacing Chinese in output, commands fragmented mid-line, or echo printing mojibake. Also covers chcp pitfalls and PowerShell 5.1 encoding traps.
---

# Writing Chinese .bat files on Chinese-locale Windows (GBK)

cmd.exe parses batch files **in the console's current codepage**, not UTF-8:

- Double-clicking a .bat → console opens at system ANSI codepage = **936 (GBK)** on Chinese Windows.
- Agent/terminal shells (VSCode, agent bash tools) often run at **65001 (UTF-8)**.

A .bat saved as UTF-8 with Chinese therefore garbles on double-click; a .bat saved as GBK garbles when run from a UTF-8 shell. Both failures produce the same symptoms: Chinese fragments executed as commands (`'<机器人名>' 不是内部或外部命令`), `?????` output, mis-parsed lines.

A second, sneakier failure: **LF-only line endings combined with DBCS (GBK) bytes make cmd misalign line boundaries** — the parser splits mid-line and executes random Chinese fragments as commands. Batch files with non-ASCII content MUST use CRLF.

## The reliable procedure

Never write the .bat directly to its final path in one step, and never trust the on-disk state of a previously-corrupted .bat. Instead:

1. Write the content as a **UTF-8 source file** (any name, e.g. `foo.bat.txt`) using your file-writing tool — it emits UTF-8 correctly (verified: Chinese bytes intact for both new files and overwrites).
2. Convert to the final .bat with `scripts/gbk_bat.ps1` (see below). It:
   - **deletes the stale target first** (a corrupted .bat may be re-clobbered by unknown processes; don't fight it, regenerate),
   - **normalizes line endings to CRLF** (the DBCS/LF misalignment fix),
   - writes **GBK (codepage 936)**,
   - **round-trip verifies** by reading the result back as GBK and comparing to the source; exits non-zero on mismatch.
3. Test in a **codepage-936 subshell**, because the agent shell itself is usually UTF-8:

```bat
cmd /c "chcp 936 >nul & C:\path\to\foo.bat < enter.txt > out.log 2>&1"
```

with `enter.txt` containing one blank line (feeds `pause`), then read `out.log` **as GBK**:

```powershell
$gbk=[Text.Encoding]::GetEncoding(936)
[IO.File]::ReadAllText('out.log', $gbk)
```

If the log is still locked by the running cmd, open with `FileShare.ReadWrite`:

```powershell
$fs=[IO.File]::Open($log,'Open','Read','ReadWrite'); $sr=New-Object IO.StreamReader($fs,$gbk); $sr.ReadToEnd()
```

## Converter usage

`scripts/gbk_bat.ps1 -Src <utf8-source> -Dst <final-bat>` — converts one file, CRLF-normalizes, verifies, exits 1 on mismatch. Loop over several pairs in your own wrapper if needed.

## Rules that prevent the classic failures

- **No `chcp 65001` inside a GBK bat.** `chcp` changes the parser codepage mid-file; lines after it get re-decoded and DBCS fragments break apart. A GBK bat on a cp936 console displays Chinese correctly with no chcp at all.
- **Prefer `goto :label` flow over large parenthesized `if/else` blocks.** Parenthesized blocks are parsed as units and are where DBCS/LF misalignment does the most damage. The goto style also keeps every line independent.
- **PowerShell 5.1 reads a BOM-less .ps1 as ANSI** — Chinese literals in a UTF-8-no-BOM .ps1 break parsing at load time ("字符串缺少终止符"). Either keep helper .ps1 ASCII-only, or write it with a UTF-8 **BOM**.
- **`pause` hangs forever with `<nul` stdin** — when automating a bat that ends in `pause`, feed a real file containing one blank line instead.
- Don't trust a .bat that "worked yesterday": if its mtime or content looks off, regenerate from the UTF-8 source rather than patching bytes in place.
- GBK covers essentially all common Simplified Chinese + full-width punctuation. If a character round-trips as `?`, it isn't in GBK — reword the line.

## Worked example (from a real incident)

Symptom: a desktop shortcut bat flashed and closed; output showed `'<机器人名>' 不是内部或外部命令` and `title ????? - ??????`. Root causes found, in order: (1) bat was UTF-8 while double-click console was cp936; (2) an attempted GBK conversion had been applied to a file whose Chinese had already been destroyed, leaving literal `?`; (3) the rebuilt GBK bat had LF-only endings and cmd misaligned lines mid-DBCS. Fix was exactly the procedure above — UTF-8 source, delete-first CRLF+GBK conversion with round-trip verification, chcp-936 subshell test — after which the same bat printed clean Chinese and ran correctly.
Pliki w paczce (2)
  • SKILL.md 5 KB
  • scripts/gbk_bat.ps1 1 KB

Wersje

W bazie nie ma innych skilli o nazwie win-gbk-bat.

Migawka bazy z 13.11.2026 · wersja paczki 57e05050e269 (skrót SHA-256 całej paczki)

Pobrania w ostatnich 30 dniach: 0 · ranking

Zgłoś problem z tym skillem: szkodliwa treść, błędny opis albo prośba autora o usunięcie.

Podobne skille

  • 95batch-files
    Pisze, debuguje i utrzymuje pliki wsadowe Windows (.bat i .cmd): składnia cmd.exe, zmienne środowiskowe, przepływ sterowania, przetwarzanie tekstu i obsługa błędów.
  • 100windows-shell-dev
    Pisze, sprawdza i naprawia duże skrypty .bat i .cmd wraz z osadzonym w nich PowerShellem.
  • 100embedded-powershell
    Pisze fragmenty PowerShell osadzone w skryptach wsadowych Windows: jednowierszowce, wartości w zmiennych środowiskowych i zgodność z wersją 5.1.
  • 89windows-cmd
    Pisze i naprawia skrypty wsadowe CMD: składnię plików .bat i .cmd, zakończenia linii CRLF, pętle FOR i typowe polecenia systemu Windows.
  • 100safe-text-editing
    Pilnuje, by zmiany plików i argumentów nie gubiły znaków: cudzysłowy, ukośniki, kodowanie, CRLF i kody wyjścia. Przydaje się, gdy zapisany tekst wychodzi inaczej, niż zamierzono.
  • 98powershell-windows
    Zbiera pułapki PowerShell w Windows: operatory, wyjście ASCII, głębokość ConvertTo-Json, ścieżki i obsługę błędów.

Uwagi i zgłoszenia

Wiadomość trafi do Bartosza Sokolińskiego. Adres e-mail jest potrzebny tylko do odpowiedzi. Szczegóły w polityce prywatności.