Batch language uses certain special characters for control purposes. E.g. !&<> and others. Text files containing these are difficult to process. Exclamation mark is such a character. Therefore other scripting language is useful. Frequently Visual Basic Script either alone or in hybrid script is a solution. If running Scripting Host is allowed and enabled, this will work.
Enclose filenames (or variables which will expand to a filename) with quotes to include names with spaces.
This script is closest to the original....
@echo off
set script="%TEMP%\stringslice.vbs"
echo wscript.echo mid(wscript.arguments(0), (wscript.arguments(1)+1),wscript.arguments(2))>%script%
for %%a in ("*.txt") do type "%%a" >> outfile.out
for /f "tokens=*" %%b in (outfile.out) do (
for /f "delims==" %%c in ( 'cscript //nologo %script% "%%b" 0 30' ) do echo %%c>>final.out
)
del %script%
Note that outfile.out and final.out with grow each time you run the script.
In fact the processing of outfile.out can be done in one line but the above is clearer to read I think
@echo off
set script="%TEMP%\stringslice.vbs"
echo wscript.echo mid(wscript.arguments(0), (wscript.arguments(1)+1),wscript.arguments(2))>%script%
for %%a in ("*.txt") do type "%%a" >> outfile.out
for /f "tokens=*" %%b in (outfile.out) do for /f "delims==" %%c in ( 'cscript //nologo %script% "%%b" 0 30' ) do echo %%c>>final.out
del %script%
You can eliminate outfile.out altogether by taking the 2nd token of the output of findstr /R "." ("." is regular expression to match all characters). [You could use type *.txt but it produces screen echo of each filename.]
@echo off
set script="%TEMP%\stringslice.vbs"
echo wscript.echo mid(wscript.arguments(0), (wscript.arguments(1)+1),wscript.arguments(2))>%script%
for /f "tokens=1,2 delims=:" %%a in ('findstr /R "." *.txt') do (
for /f "delims==" %%c in ( 'cscript //nologo %script% "%%b" 0 30') do (
echo %%c>>final.out
)
)
del %script%
In fact you can do all the processing on one line but it is 153 characters long.
@echo off
set script="%TEMP%\stringslice.vbs"
echo wscript.echo mid(wscript.arguments(0), (wscript.arguments(1)+1),wscript.arguments(2))>%script%
for /f "tokens=1,2 delims=:" %%a in ('findstr /R "." *.txt') do for /f "delims==" %%c in ( 'cscript //nologo %script% "%%b" 0 30') do echo %%c>>final.out
del %script%