Batch Script to Trim lines in text to first 30 or 50 characters only

batchcommand linescript

I am now new to scripts but i find it really difficult understanding "for" command (especially with that tokens and delimiters etc) . Saying so, i think that for command can be used to do what i am doing. If its not and there is an easier way, ignore my ignorance 🙁

Say i have multiple lines in a text file abc.txt with each line starting and ending with " (quotes)
E.g. a file of 3 lines

"hey what is going on @mike220. I am working on your car. Its engine is in very bad condition" 
"Because if you knew, you'd get shredded and do it with certainty"
"@honey220 Do you know someone who has busted their ass on a diet only for results to come to a screeching halt after a few weeks"

How can i trim each line, within the quotes, to a Fixed length say 30 or 50 or 100 characters (including spaces)
I want to enter the number of character in batch and it can trim accordingly and produce a file def.txt with trimmed lines within quotes.

Say i enter 50, results of above example should be

"hey what is going on @mike220. I am working on you"
"Because if you knew, you'd get shredded and do it"
"@honey220 Do you know someone who has busted their"

Thanks

P.S. if you use For command, kindly please explain the command.

EDIT:
Though the answer provided worked, there is an issue with non english text. I am getting garbled text in Output file for non english text in input file . Any help @barlop
here is the nonenglish text ( 1 line)

"फाइल है इसको ना पढ़े आपको कोई मतलांब नही"

Best Answer

This does it

@echo off

setlocal enableextensions enabledelayedexpansion

set /p num=Enter chars to show between quotes: 
set /a num=%num%+1

for /f "delims=" %%f in (a.txt) do (
set a=%%f

echo !a:~0,%num%!^"
)

So, using your example, that's in a.txt So each line starts with a quote, and there are spaces too though it works if there aren't spaces as well.

"hey what is going on @mike220. I am working on your car. Its engine is in very bad condition" 
"Because if you knew, you'd get shredded and do it with certainty"
"@honey220 Do you know someone who has busted their ass on a diet only for results to come to a screeching halt after a few weeks"

And running the batch script

C:\blah>a.bat
Enter chars to show between quotes: 3
"hey"
"Bec"
"@ho"

C:\blah>a.bat
Enter chars to show between quotes: 50
"hey what is going on @mike220. I am working on you"
"Because if you knew, you'd get shredded and do it "
"@honey220 Do you know someone who has busted their"

C:\blah>

I have now added two lines, to make it output to a file def.txt

@echo off


setlocal enableextensions enabledelayedexpansion

del def.txt 2>nul


set /p num=Enter chars to show between quotes: 
set /a num=%num%+1

for /f "delims=" %%f in (a.txt) do (
set a=%%f

echo !a:~0,%num%!^"
echo !a:~0,%num%!^" >>def.txt
)


C:\crp\dlsnex>a
Enter chars to show between quotes: 2
"he"
"Be"
"@h"

C:\crp\dlsnex>type def.txt
"he"
"Be"
"@h"

C:\crp\dlsnex>

for statements in cmd are a bit tricky. To understand this one, just start off trying to write a for statement that prints every line of a file, without trimming, and use a simple file like one with two lines abcdefg and hijklmn. Then this for statement for /f %f in (a.txt) do @echo %f then you build it up. But that is where you start, if you are to figure out how to do it. Once you have that, add the "delims=" see what effect it has(you might need some spaces in the lines of the file, to see the effect). Then look at how you can get a substring in batch. set a=abcdefg echo %a:~0,2% (prints ab).

Related Question