tomasz86 Posted April 1, 2013 Author Posted April 1, 2013 I was thinking about files which contain some language specific characters, etc.
jaclaz Posted April 1, 2013 Posted April 1, 2013 I was thinking about files which contain some language specific characters, etc.You mean UNICODE? jaclaz
tomasz86 Posted April 1, 2013 Author Posted April 1, 2013 Never mind I'll let you know if I encounter any problems.By the way, isFOR /F "tokens=1,2 delims=, " %%a in ('TYPE 1.inf') do echo %%a,%%bany different from FOR /F "tokens=1,2 delims=, " %%a in ('FINDSTR "^" 1.inf') do echo %%a,%%b?
jaclaz Posted April 1, 2013 Posted April 1, 2013 Never mind I'll let you know if I encounter any problems.By the way, isFOR /F "tokens=1,2 delims=, " %%a in ('TYPE 1.inf') do echo %%a,%%bany different from FOR /F "tokens=1,2 delims=, " %%a in ('FINDSTR "^" 1.inf') do echo %%a,%%b?Yes it is, one uses TYPE, the other uses FINDSTR More seriously, try them on a UNICODE .inf and check the differences.See here:http://www.robvanderwoude.com/type.phpConvert Unicode to ASCII vvTYPE is UNICODE compliant (and the result will normally be "plain text"), FINDSTR is not.jaclaz
tomasz86 Posted April 2, 2013 Author Posted April 2, 2013 (edited) Try with a "plain":FOR /F "tokens=1,2 delims=, " %%a in ('TYPE 1.inf') do echo %%a,%%bI was wrong. It still doesn't ECHO lines starting with ";" when EOL isn't defined I'll probably use: FOR /F delims^=^ eol^= %%A IN (1.inf) DO ECHO %%Awhen I want to display full lines and FOR /F tokens^=1-2^ eol^=^delims^=^,^ %%A IN (1.inf) DO ECHO %%A,%%Bwhen I need to specify delims, one of which is space. Edited April 2, 2013 by tomasz86
jaclaz Posted April 2, 2013 Posted April 2, 2013 I was wrong. It still doesn't ECHO lines starting with ";" when EOL isn't defined Yes, the semicolon is the default EOL of the FOR, you can do something like:FOR /F "eol=§ tokens=1,2 delims=, " %%a in ('TYPE 1.inf') do echo %%a,%%bThe § character is in "Windows", in command line it will "look" different, it's ALT+0167, but you can use any other "extended ASCII" character that won't be normally used in a .inf.jaclaz
tomasz86 Posted April 2, 2013 Author Posted April 2, 2013 You seem to like "§" a lot but do you remember that it was also present in your original SPLITINF script... and the problem was that it didn't work when system locale was set to Korean and in the end I had to replace it with something else which was "}#}" so I'd like to avoid using such characters.
jaclaz Posted April 2, 2013 Posted April 2, 2013 You seem to like "§" a lot but do you remember that it was also present in your original SPLITINF script... and the problem was that it didn't work when system locale was set to Korean and in the end I had to replace it with something else which was "}#}" so I'd like to avoid using such characters.Naah it's only because it is easily available on my italian keyboard, as said it is up to you to find a character that is not used in "Korean" (or "other rare language" files).BTW it is possible (but you will have to experiment) that you can use one of the "lower" ASCII Characters, in the range 0 to 31 decimal, really cannot say. jaclaz
tomasz86 Posted April 11, 2013 Author Posted April 11, 2013 I wanted to use both double quotes and spaces as delimiters but I didn't know how to do it so I searched for a solution in the Internet but couldn't really find anything useful. Finally I've managed to figure it out by myself and I'm posting this in case someone else encounters a similar problem:FOR /F delims^=^"^ %%A IN ("abc = "1"") DO ECHO "%%A"It will display just "abc"Notice that there are two spaces before %%A.
Yzöwl Posted April 11, 2013 Posted April 11, 2013 The example you have provided shows us only that the space has been delimited because it has split the string before it has seen your double quote and output only the first token.See how these work:@ECHO OFF & SETLOCAL ENABLEEXTENSIONSECHO/DELIMITED DOUBLE QUOTES ONLYFOR /F TOKENS^=1-3^ DELIMS^=^" %%A IN ("THIS IS"ENCLOSED"IN DOUBLE QUOTES") DO ECHO/[%%A] [%%B] [%%C]FOR /F USEBACKQ^ TOKENS^=1-3^ DELIMS^=^" %%A IN ('THIS IS"ENCLOSED"IN DOUBLE QUOTES') DO ECHO/[%%A] [%%B] [%%C]ECHO/&ECHO/DELIMITED DOUBLE QUOTES AND SPACESFOR /F TOKENS^=1-6^ DELIMS^=^"^ %%A IN ("THIS IS"ENCLOSED"IN DOUBLE QUOTES") DO ECHO/[%%A] [%%B] [%%C] [%%D] [%%E] [%%F]FOR /F USEBACKQ^ TOKENS^=1-6^ DELIMS^=^"^ %%A IN ('THIS IS"ENCLOSED"IN DOUBLE QUOTES') DO ECHO/[%%A] [%%B] [%%C] [%%D] [%%E] [%%F]PAUSE & GOTO :EOF
bphlpt Posted April 11, 2013 Posted April 11, 2013 (edited) Actually, if all tomasz86 is interested in is the first token in the string, then his code does seem to work, as shown by running this (NOTE two spaces before "%%A"):@ECHO OFF & SETLOCAL ENABLEEXTENSIONSECHO/TOMASZ86 METHOD FOR FIRST TOKEN ONLYFOR /F delims^=^"^ %%A IN ("THIS IS"ENCLOSED"IN DOUBLE QUOTES") DO ECHO/[%%A]FOR /F delims^=^"^ %%A IN ("THIS"TEST" IS"ENCLOSED"IN DOUBLE QUOTES") DO ECHO/[%%A]ECHO/&ECHO/DELIMITED DOUBLE QUOTES ONLYFOR /F TOKENS^=1-3^ DELIMS^=^" %%A IN ("THIS IS"ENCLOSED"IN DOUBLE QUOTES") DO ECHO/[%%A] [%%B] [%%C]FOR /F USEBACKQ^ TOKENS^=1-3^ DELIMS^=^" %%A IN ('THIS IS"ENCLOSED"IN DOUBLE QUOTES') DO ECHO/[%%A] [%%B] [%%C]ECHO/&ECHO/DELIMITED DOUBLE QUOTES AND SPACESFOR /F TOKENS^=1-6^ DELIMS^=^"^ %%A IN ("THIS IS"ENCLOSED"IN DOUBLE QUOTES") DO ECHO/[%%A] [%%B] [%%C] [%%D] [%%E] [%%F]FOR /F USEBACKQ^ TOKENS^=1-6^ DELIMS^=^"^ %%A IN ('THIS IS"ENCLOSED"IN DOUBLE QUOTES') DO ECHO/[%%A] [%%B] [%%C] [%%D] [%%E] [%%F]PAUSE & GOTO :EOFThe output for both lines of the code added to show his method is -- [THIS]If you want more flexibility over getting more than just the first token, then your code is much more complete and accurate, Yzöwl, as always.EDIT: If, however, he was trying to get the same result for VAR=VAL vs VAR = VAL vs VAR="VAL" vs VAR = "VAL", then this code would work:@ECHO OFF & SETLOCAL ENABLEEXTENSIONSECHO GET SAME RESULT FOR VAR=VAL vs VAR = VAL vs VAR="VAL" vs VAR = "VAL"FOR /F TOKENS^=1-2^ DELIMS^=^=^"^ %%A IN ("abc=1") DO ECHO/[%%A] [%%B]FOR /F TOKENS^=1-2^ DELIMS^=^=^"^ %%A IN ("abc = 1") DO ECHO/[%%A] [%%B]FOR /F TOKENS^=1-2^ DELIMS^=^=^"^ %%A IN ("abc="1"") DO ECHO/[%%A] [%%B]FOR /F TOKENS^=1-2^ DELIMS^=^=^"^ %%A IN ("abc = "1"") DO ECHO/[%%A] [%%B]PAUSE & GOTO :EOFAll four lines will echo[abc] [1]So it all depends on what he really wants to do.Cheers and Regards Edited April 11, 2013 by bphlpt
tomasz86 Posted April 12, 2013 Author Posted April 12, 2013 Hmm, actually the strings go like this:1.txtabc = 1abc= 1abc =1abc = "1 2"abc= "1 2"abc ="1 2"etc.They're not consistent so sometimes there are quotes in other cases there aren't. Some goes for spaces.I'm using this script:FOR /F tokens^=1*^ delims^=^"^=^ %%A IN ('TYPE "1.txt"') DO ( FOR /F delims^=^" %%C IN ("%%B") DO ECHO %%A="%%C")and the output is:abc="1"abc="1"abc="1"abc="1 2"abc="1 2"abc="1 2"
jaclaz Posted April 12, 2013 Posted April 12, 2013 (edited) Well, the output would be the same with this:FOR /F "tokens=1,2 delims==" %%A IN ('TYPE "1.txt"') DO CALL :strip_readd_quotes %%A %%BGOTO :EOF:strip_readd_quotesECHO %1="%~2"GOTO :EOFjaclaz Edited April 12, 2013 by jaclaz
tomasz86 Posted April 12, 2013 Author Posted April 12, 2013 Thanks Yours is actually better because it works even for something like this:MainCancelIntroString = "Thank you for reporting the Request. When you click ""Send Report"" button, data concerning why install failed will be sent to Microsoft"while mine doesn't.
jaclaz Posted April 12, 2013 Posted April 12, 2013 Thanks Yours is actually better because it works even for something like this:Yes and no.If you have more than two tokens NOT quoted "on the right side" of the equal sign my code snippet won't work.This may:FOR /F "tokens=1,* delims==" %%A IN ('TYPE "1.txt"') DO (CALL :strip_spaces %%ACALL :strip_readd_quotes %%BECHO !var!=!val!):strip_spacesset var=%*GOTO :EOF:strip_readd_quotesset val="%*"set val=%val:""="%IF "[]"=="[%*]" set val=GOTO :EOFbut it won't work with the example you just posted with double-double quotes inside double quotes. jaclaz
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now