You are not logged in.

#1 12 Mar 2015 20:57

Sal3r0f3MUt
New Member
Registered: 12 Mar 2015
Posts: 1

Read file, Find correct line, pars each word, and assign to variables

I have a text file that has 13 lines.  Each line has five words.  I need to find the right line then pars it in to the var(s).  Something is going wrong.
The "echo.%%a" shows the correct line then it goes wrong.  Can anyone help me?

setup.cmd
set AT=ABC

@echo off & setLocal
for /f "eol=: tokens=* delims= " %%a in ('find "%AT%" Text.txt
) do (
   echo.%%a
   set /A vidv=!vidv!+1
   set var!vidv!=%%a
)
set %1=%var1%
set %2=%var2%
set %3=%var3%
set %4=%var4%
set %5=%var5%
echo This is first word  %var1% of the Text.txt file.
echo This is second word %var2% of the Text.txt file.
echo This is third word  %var3% of the Text.txt file.
echo This is forth word  %var4% of the Text.txt file.
echo This is forth word  %var5% of the Text.txt file.
echo This is fifth word  %var5% of the Text.txt file.
pause
endlocal


Text.txt
AAA 3 0 1 Install_config1_winb64
ABA 6 10 1 Install_config1_winb64
ACA 2 8 1 Install_config2_winb64
ADA 1 0 14 Install_config1_winb64
ABC 14 12 0 Install_config0_winb64


RESULTS:
C:\Users\TJS4689\Desktop|12:55:18.69>set AT=UTL
---------- Text.txt
Missing operator.
ABC 14 12 0 Install_config0_winb64
Missing operator.
The syntax of the command is incorrect.
The syntax of the command is incorrect.
The syntax of the command is incorrect.
The syntax of the command is incorrect.
The syntax of the command is incorrect.
This is first word   of the BuildConfig file.
This is second word  of the BuildConfig file.
This is thrid word   of the BuildConfig file.
This is forth word   of the BuildConfig file.
This is forth word   of the BuildConfig file.
Press any key to continue . . .

Offline

#2 03 May 2016 13:53

elias
Member
Registered: 03 May 2016
Posts: 12

Re: Read file, Find correct line, pars each word, and assign to variables

Hi,

If I understood you correctly, you want to find the line starting with ABC and then get the remaining arguments after it? (14 12 0 etc...) ?

In that case your syntax for the tokens is wrong. You have 5 tokens, you have to say: "tokens=1-5":

set AT=ABC

@echo off & setLocal enabledelayedexpansion
for /f "eol=: tokens=1-5 delims= " %%a in ('findstr "%AT%" Text.txt') do (
   set var1=%%a
   set var2=%%b
   set var3=%%c   
   set var4=%%d   
   set var5=%%e   
)
:: set %1=%var1%
:: set %2=%var2%
:: set %3=%var3%
:: set %4=%var4%
:: set %5=%var5%
echo This is first word  %var1% of the Text.txt file.
echo This is second word %var2% of the Text.txt file.
echo This is third word  %var3% of the Text.txt file.
echo This is forth word  %var4% of the Text.txt file.
echo This is fifth word  %var5% of the Text.txt file.
pause
endlocal 

Outputs:

This is first word  ABC of the Text.txt file.
This is second word 14 of the Text.txt file.
This is third word  12    of the Text.txt file.
This is forth word  0    of the Text.txt file.
This is fifth word  Install_config0_winb64    of the Text.txt file.
Press any key to continue . . .

T:\Temp>

The "Batchography" book will explain how to parse files easily. You will understand the syntax and be more confident in the future.


Book: Batchography - The Art of Batch Files Programming

Offline

Board footer

Powered by