1

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ricky Payne
  • 149
  • 4
  • 14
  • Like dbenham stated, it's not a good idea to try it with pure batch, but you can do it anyway [SO: converting a binary file to HEX representation using batch file](http://stackoverflow.com/a/4648636/463115) – jeb Aug 31 '12 at 13:38

2 Answers2

2

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.

dbenham
  • 127,446
  • 28
  • 251
  • 390
0

I am not able to reproduce your issue - for me the output is exactly the same (see scratch program below for my code).

However, I have two suggestions.

  1. Read up on variable expansion in loops. Setting a variable inside a loop does not work the way one assumes it will.

  2. I would suggest using FINDSTR instead of TYPE as it should be much easier to use - I've given an example at the end of the code below.

Scratch program:

@ECHO OFF

ECHO Param1=Value1 > config.cfg
ECHO Param2=Value2 >> config.cfg
ECHO Param3=Value3 >> config.cfg

ECHO Echo configuration File using TYPE
ECHO.

TYPE config.cfg

ECHO.
ECHO Outputting config file using LOOP
ECHO.

for /f "usebackq tokens=* delims==" %%a in (`type config.cfg`) do (echo %%a)

ECHO.
ECHO Searching for %1

REM /B means "find at beginning of word"
REM /I means "search is case insensitive"
FINDSTR /B /I %1 config.cfg
RB.
  • 36,301
  • 12
  • 91
  • 131
  • I think the issue is possibly one of two things... The single line is VERY long (my assumption was that the TYPE and FOR would take as much as CMD can handle, and drop the remainder??) Or possibly it may be something to do with off characters (CMD interprets them as smiley faces, hearts etc. notepad shows them as spaces. Findstr wouldn't work, as I need to be able to set a variable as the string starting from approx 10 characters after the string I've found. – Ricky Payne Aug 31 '12 at 11:25