Loop Thru File Use Line Value in Operation

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
btpoole
Posts: 15
Joined: 04 Sep 2015 06:28

Loop Thru File Use Line Value in Operation

#1 Post by btpoole » 06 Sep 2015 19:09

I am trying to open a text file, read it line by line, use each line in script commands. I am having trouble getting it to work.

The text file being opened and read looks as follows:

abc:123-def
ghi:456-jkl
mno:789-pqr

I am opening the file and looping thru with the following code:

set "spath=C:\myscript\"
FOR /F %%a in (test.txt) do (echo %%a
findstr ":" %%a | cut -f1 -d ":" > val1.txt
findstr ":" %%a | cut -f1 -d "-" > val2.txt
findstr ":" %%a | cut -f1 -d "-" > val3.txt

finalval=%val1% + %val2%
echo %finalval%
finalval > val3.txt
}

I have tried multiple ways but can't seem to get this to work. The val1.txt and val2.txt gets created but I can never get finalval to work. Either only the last line in test.txt is the only one used or nothing. I know val1 and val2 changes each time thru. The methodlogy is that the test.txt acts like an array almost, where each line is parsed, used in the operation, then the finalval is saved to text file named value currently stored in val3.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Loop Thru File Use Line Value in Operation

#2 Post by Squashman » 06 Sep 2015 20:12

Test this in a batch file.

Code: Select all

for /f "tokens=2 delims=:-" %%a in (test.txt) do echo %%a

btpoole
Posts: 15
Joined: 04 Sep 2015 06:28

Re: Loop Thru File Use Line Value in Operation

#3 Post by btpoole » 07 Sep 2015 06:50

Many thanks, after the do statement, is it possible to multiple commands on the same line, or do you have to use ( )?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Loop Thru File Use Line Value in Operation

#4 Post by Squashman » 07 Sep 2015 09:44

btpoole wrote:Many thanks, after the do statement, is it possible to multiple commands on the same line, or do you have to use ( )?

Whatever trips your trigger. I prefer readability. I usually will shy away from helping people with their batches files if they are not formatted for readability.

Post Reply