last day (21 days later) » 

2:37 PM
2
A: Batch Script to Trim lines in text to first 30 or 50 characters only

barlopThis 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 ther...

 
plz plz plz barlop explain me this.... for /f "delims=" %%f in (a.txt) do ( set a=%%f echo !a:~0,%num%!^" .......... like you explain this to a 5th grade child learning batch, each letter at a time. I am just sick of not being able to understand this "FOR command (yes i am not from programming background) ...........AND one more thing i need the output in def.txt file not echo :)
 
to learn programming or scripting you have to break things down and build simple things grass up. So to understand it properly, the best way isn't so much to get a broken down explanation of every line.. But to write something simple yourself and to build it up. So you start by trying to understand something very simple. Like a for statement that dumps every line of a file (not trimming). And you look at how, given a variable e.g. a=abcdefg how do you output just part of it. like just the first 3 characters.
 
ok i understood something. 1) So %f prints 1 line why you used a=%%f i.e. extra "%" ?. 2)what does ! in start and end mean? and what does ^ and " mean in command --- !a:~0,%num%!^" 3) What does "delims=" mean? Thanks. For 3 characters in your example i would need %a:~0,3% right?
 
@SuperUserMan some of these are things that are unique to batch.. If you try something from cmd, you use %f, if you try something in a batch you see it doesn't work, and that you should use %%f in batch. You can try it with any simple line, from the command line vs from batch file. And it can be %a or whatever for %a in (a.txt) do echo %a. In batch you use %%a.
The exclamation marks are something completely ridiculous about batch that is a surprise to programmers. Normally you'd do %var% as in your variable surrounded by percentages. But in -some- cases, that doesn't work like using %var% within a for or if can be funny, and when that happens you have to A)have this line setlocal enableextensions enabledelayedexpansion and B)use !var! instead of %var% And if you are running things just in cmd.exe and you want !var! to work you need to do cmd /v:on then you can do echo !var!
The ^" well, you could always try ". But " has a special meaning, and to just give a plain " without any special meaning, you do what is called 'escaping' you escape it. And to escape it, you use the escape character, which in cmd.exe is ^. So, ^" is what I did there. I did ^" instinctively but generally you'd know you need to when you get errors not doing so. Getting batch files to do what you want is in some ways more difficult than programming. 'cos with programming you have a compiler that gives error messages and also you don't have the sillyness that i've described like needing !var!
escaping can also be something you do to a literal character to get its special meaning.. As an example, open cygwin and do $ echo -e 'abc\ndef'<ENTER> and putting the \ before the n, makes \n, which is read as a new line, not just a regular 'n'.
 
Thanks barlop. I understood everything regarding "for" in your answer except what does "delims=" mean? in context to my example? Thanks a lot again for your time :)
 
2:37 PM
well.. there's some terminology...
if you had what are called in computer science, "records, and fields"
like, a database.
storing data about an item..
John,25,ginger
Ted,32,grey
then comma would be your delimiter. comma is what is separating the fields
and each record has fields
delimited by the delimiter, the comma.
If you did a for statement to show every line of a file but you didn't put in anything about a delimiter, then it assumes the delimiter is space.
And, it makes %f only equal to the first field
What is why for /f %f in (a.txt) do @echo %f <-- doesn't work so well when a.txt has spaces.
To get around that, do "delims=" then it knows there are no delimiters.. it's saying delims = nothing
Another concept is tokens/tokenization, which is how a string is broken up
saying "tokens=*" in batch is similar to "delims=" I suppose one could've done both.. but I did delims= and it worked
 
Ok i understood lot more now. So if say db has
John,25,ginger,@monkey
& i set for /f "delims=@" %%f in (a.txt) do @echo %%f that means it will echo
John,25,ginger,

Can you gimme another example of tokens thing?if possible or time permits.
 
3:00 PM
and logically if putting nothing about delimiter means delimiter is space, does it mean if we run command for /f %%f in (a.txt) do @echo %%f on
John,25,ginger
it should echo
John,25,ginger itself as there is no space anyway
Am i making sense or all shit? Sorry
 
3:47 PM
@barlop i have hit a problem. The text file abc which i was using has no english text too. NOw when i use this command to generate trimmed def.txt, that non english text is garbled. Is this related to some encoding problem? how can i solve it. Else all this discussion would be fruitless except that i understood for command!
 
4:01 PM
I suppose it would just say john,25,ginger, but one could test it..
yes
as for the non-english text..
try updating your question, editing the file with the non-english text into your question
And try cmd /u, see if it helps.
 
4:24 PM
@SuperUserMan ^
 
 
2 hours later…
6:48 PM
i edited the question for non english text. Where do i need to use cmd /u is it command? dont think so :(
 

  last day (21 days later) »