Here is a "robust" pure batch solution that preserves !
and empty lines. But it is still limited to ~8190 max line length, and it is painfully slow for large files. This will not modify lines that have less than 7 characters.
@echo off
setlocal disableDelayedExpansion
set "file=file.txt"
>"%file%.new" (
for /f "delims=" %%A in ('findstr /n "^"` "file.txt"') do (
set "s=%%A"
setlocal enableDelayedExpansion
set "s=!s:*:=!"
if not defined s (
echo(
) else if "!s:~7!" equ "" (
echo(!s!
else (
echo(!s:~0,7!-!s:~7!
)
endlocal
)
)
move /y "%file%.new" "%file%" >nul
And here is a truly robust and fast solution that uses JREPL.BAT - a regular text processing utility that is pure script (batch/JScript) that runs natively on any Windows machine from XP onward - no 3rd party executable required. This solution also does not modify lines that have fewer than 7 characters.
From the command line:
jrepl "^.{7}" "$&-" /f file.txt /o -
From within a batch script
call jrepl "^.{7}" "$&-" /f file.txt /o -