check file contains only printable 7-bit ASCII characters

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Sponge Belly
Posts: 223
Joined: 01 Oct 2012 13:32
Location: Ireland

check file contains only printable 7-bit ASCII characters

#1 Post by Sponge Belly » 10 Apr 2024 14:59

Hello All! :)

A little proof-of-concept program I put together:

Code: Select all

@echo off & setLocal enableExtensions disableDelayedExpansion
(call;) %= sets errorLevel to 0 =%

for /f "tokens=*" %%A in ('chcp') do for %%B in (%%A) do set "cp=%%~nB"
mode con cp select=437 >nul

((for /l %%I in (1 1 70) do pause) >nul & set /p "tab=") <"%ComSpec%"
set "tab=%tab:~0,1%"

mode con cp select=%cp% >nul

set "sp= "
set "nos=0123456789"
set "lc=abcdefghijklmnopqrstuvwxyz"
set "printable=!#$%%&'()*+,-./%nos%:;<=>?@%UC%[\]^_`%lc%{|}~"

(for /f useBackq^ delims^="%printable%"^%tab%^%sp% %%A in ("%~1") do goto die
) || echo(no non-printable characters found in file
goto end

echo(non-printable characters found in file
(call) %= sets errorLevel to 1 =%
endLocal & goto :EOF
Lines can exceed the 8191-character limit and then some.

Just one thing… if someone could tell me how to find the line number where offending characters occur, I’d be extremely grateful!

- SB

Post Reply