I’m trying to use a batch file to find a particular section on a .cfg file, and use that as a variable.
Specifically, I’m trying to read Idapi32.cfg (Borland configuration file), to find the path to one the alias’ inside it.
I couldn’t think of any way to do this neatly, so I’ve been trying to do this:
– use CMD’s TYPE feature to get the .cfg file as a string (its one line of text) and set this string to a variable
– take the first five characters of that variable, and use IF string1==string2 to see if the first five characters are the section I need
– if not, reset the variable to be the same minus the first character and check again.
I know that my loop works, because I can see it working, but I can’t understand why I get different results between running
TYPE "C:\programdata\borland shared\bde\idapi32.cfg"
and
for /f "usebackq tokens=* delims==" %%a in (`type "c:\programdata\borland shared\bde\idapi32.cfg"`) do (echo %%a)
I would expect the echo to be identical to the TYPE command results?? Or am I missing something??
Edit
I’ve just added the results of the TYPE and the FOR, to show the difference in results
C:\>type "C:\programdata\borland shared\bde\idapi32.cfg"
♥ DRIVERS ☺ ♥ PARADOX ☺ ♥ INIT ☺ ♥ ☺ NET DIR ♦ W:\WINFILES ☻ ☻ ☻ ♥ DATAB
ASES ☺ ♥ MS Access Database ☺ ♥ DB OPEN ☺ ♥ ☺ DATABASE NAME ♦ ♥ ☺ USER NAME
♦ ♥ ☺ ODBC DSN ♦ MS Access Database ♥ ☺ OPEN MODE ♦ READ/WRITE ♥ ☺ SCHEMA CACH
E SIZE ♦ 8 ♥ ☺ SQLQRYMODE ♦ ♥ ☺ LANGDRIVER ♦ ♥ ☺ SQLPASSTHRU MODE ♦ SHARED AUT
OCOMMIT ♥ ☺ SCHEMA CACHE TIME ♦ -1 ♥ ☺ MAX ROWS ♦ -1 ♥ ☺ BATCH COUNT ♦ 200 ♥ ☺ E
NABLE SCHEMA CACHE ♦ FALSE ♥ ☺ SCHEMA CACHE DIR ♦ ♥ ☺ ENABLE BCD ♦ FALSE ♥ ☺ RO
WSET SIZE ♦ 20 ♥ ☺ BLOBS TO CACHE ♦ 64 ☻ ♥ DB INFO ☺ ♥ ☺ TYPE ♦ Microsoft Acce
ss Driver (*.mdb) ♥ ☺ PATH ♦ ☻ ☻ ♥ Excel Files ☺ ♥ DB OPEN ☺ ♥ ☺ DATABASE N
AME ♦ ♥ ☺ USER NAME ♦ ♥ ☺ ODBC DSN ♦ Excel Files ♥ ☺ OPEN MODE ♦ READ/WRITE ♥
☺ SCHEMA CACHE SIZE ♦ 8 ♥ ☺ SQLQRYMODE ♦ ♥ ☺ LANGDRIVER ♦ ♥ ☺ SQLPASSTHRU MODE
♦ SHARED AUTOCOMMIT ♥ ☺ SCHEMA CACHE TIME ♦ -1 ♥ ☺ MAX ROWS ♦ -1 ♥ ☺ BATCH COUN
T ♦ 200 ♥ ☺ ENABLE SCHEMA CACHE ♦ FALSE ♥ ☺ SCHEMA CACHE DIR ♦ ♥ ☺ ENABLE BCD ♦
FALSE ♥ ☺ ROWSET SIZE ♦ 20 ♥ ☺ BLOBS TO CACHE ♦ 64 ☻ ♥ DB INFO ☺ ♥ ☺ TYPE ♦ M
icrosoft Excel Driver (*.xls) ♥ ☺ PATH ♦ ☻ ☻ ♥ dBASE Files ☺ ♥ DB OPEN ☺ ♥
☺ DATABASE NAME ♦ ♥ ☺ USER NAME ♦ ♥ ☺ ODBC DSN ♦ dBASE Files ♥ ☺ OPEN MODE ♦ R
EAD/WRITE ♥ ☺ SCHEMA CACHE SIZE ♦ 8 ♥ ☺ SQLQRYMODE ♦ ♥ ☺ LANGDRIVER ♦ ♥ ☺ SQLP
ASSTHRU MODE ♦ SHARED AUTOCOMMIT ♥ ☺ SCHEMA CACHE TIME ♦ -1 ♥ ☺ MAX ROWS ♦ -1 ♥
☺ BATCH COUNT ♦ 200 ♥ ☺ ENABLE SCHEMA CACHE ♦ FALSE ♥ ☺ SCHEMA CACHE DIR ♦ ♥ ☺
ENABLE BCD ♦ FALSE ♥ ☺ ROWSET SIZE ♦ 20 ♥ ☺ BLOBS TO CACHE ♦ 64 ☻ ♥ DB INFO ☺
♥ ☺ TYPE ♦ Microsoft dBase Driver (*.dbf) ♥ ☺ PATH ♦ ☻ ☻ ♥ Text Files ☺ ♥ D
B OPEN ☺ ♥ ☺ DATABASE NAME ♦ ♥ ☺ USER NAME ♦ ♥ ☺ ODBC DSN ♦ Text Files ♥ ☺ OPE
N MODE ♦ READ/WRITE ♥ ☺ SCHEMA CACHE SIZE ♦ 8 ♥ ☺ SQLQRYMODE ♦ ♥ ☺ LANGDRIVER ♦
♥ ☺ SQLPASSTHRU MODE ♦ SHARED AUTOCOMMIT ♥ ☺ SCHEMA CACHE TIME ♦ -1 ♥ ☺ MAX ROWS
♦ -1 ♥ ☺ BATCH COUNT ♦ 200 ♥ ☺ ENABLE SCHEMA CACHE ♦ FALSE ♥ ☺ SCHEMA CACHE DIR
♦ ♥ ☺ ENABLE BCD ♦ FALSE ♥ ☺ ROWSET SIZE ♦ 20 ♥ ☺ BLOBS TO CACHE ♦ 64 ☻ ♥ DB IN
FO ☺ ♥ ☺ TYPE ♦ Microsoft Text Driver (*.txt; *.csv) ♥ ☺ PATH ♦ ☻ ☻ ♥ MS Acce
ss 97 Database ☺ ♥ DB OPEN ☺ ♥ ☺ DATABASE NAME ♦ ♥ ☺ USER NAME ♦ ♥ ☺ ODBC DS
N ♦ MS Access 97 Database ♥ ☺ OPEN MODE ♦ READ/WRITE ♥ ☺ SCHEMA CACHE SIZE ♦ 8 ♥
☺ SQLQRYMODE ♦ ♥ ☺ LANGDRIVER ♦ ♥ ☺ SQLPASSTHRU MODE ♦ SHARED AUTOCOMMIT ♥ ☺
SCHEMA CACHE TIME ♦ -1 ♥ ☺ MAX ROWS ♦ -1 ♥ ☺ BATCH COUNT ♦ 200 ♥ ☺ ENABLE SCHEMA
CACHE ♦ FALSE ♥ ☺ SCHEMA CACHE DIR ♦ ♥ ☺ ENABLE BCD ♦ FALSE ♥ ☺ ROWSET SIZE ♦
20 ♥ ☺ BLOBS TO CACHE ♦ 64 ☻ ♥ DB INFO ☺ ♥ ☺ TYPE ♦ Microsoft Access Driver (*
.mdb) ♥ ☺ PATH ♦ ☻ ☻ ♥ NewSites ☺ ♥ DB INFO ☺ ♥ ☺ TYPE ♦ STANDARD ♥ ☺ PATH
♦ C:\welcome\progs\sitesdatasetups\hoste\book ♥ ☺ ENABLE BCD ♦ FALSE ♥ ☺ DEFAULT
DRIVER ♦ PARADOX ☻ ☻ ♥ WinPOS32DB ☺ ♥ DB INFO ☺ ♥ ☺ TYPE ♦ STANDARD ♥ ☺ PAT
H ♦ C:\TaskPOS\WINPOS~1\ ♥ ☺ ENABLE BCD ♦ FALSE ♥ ☺ DEFAULT DRIVER ♦ Paradox ☻ ☻
♥ PreparationDB ☺ ♥ DB INFO ☺ ♥ ☺ TYPE ♦ STANDARD ♥ ☺ PATH ♦ C:\TaskPOS\Win
POS32DB ♥ ☺ ENABLE BCD ♦ FALSE ♥ ☺ DEFAULT DRIVER ♦ PARADOX ☻ ☻ ♥ IOLiteW21125
25165259313 ☺ ♥ DB INFO ☺ ♥ ☺ TYPE ♦ STANDARD ♥ ☺ PATH ♦ W:\Book ♥ ☺ ENAB
It goes on…
C:\>for /f "usebackq tokens=* delims==" %a in (`type "c:\programdata\borland sha
red\bde\idapi32.cfg"`) do (echo %a)
C:\>(echo ♥SQLQRYMODE♦IOLiteW2112525165259313☻ )
♥SQLQRYMODE♦IOLiteW2112525165259313☻
If I could get the full result of the “TYPE” command above into a variable, I can work with it (I think)… I just can’t work out why the results of the FOR are different?
Looks like you are working with a binary file.
CMD does not play nicely with null characters.
Also CMD is limited to 8191 characters per line.
So it looks like your efforts to use CMD are doomed unless you use some 3rd party tool that can process a binary file. Or switch to another language like VBScript or JScript.