Solved Batch that outputs some tokens from a file

October 18, 2011 at 07:46:40
Specs: Windows Vista, 7Gbyte Ram / Core 2 Duo 3Ghz
For example in my server log's first line i want to take the first line and just take 5 strings from it, convert them to variables and output them to a text file called 0.txt.
Then i want to do the same with the other line and output to a file called 1.txt then the next file to a file called 2.txt and so on...

And ONLY WITH MsDos. No VB or anything else.

*This is the server log example*

trade|66|2|0|EURJPY|10000|1.30000|0|0|0|0||$
trade|66|2|0|EURUSD|545454545400|1.5550|0|0|0|0||$
trade|66|2|0|USDJPY|10222|1.6666000|0|0|0|0||$


*Part of my code that parses the above strings*

findstr "trade|" C:\Bulker\grinder\log.log>c:\Bulker\trade.txt do pause
FOR /F "tokens=5,6,7,8,9 delims=|" %%a IN (c:\Bulker\trade.txt) do (
set part1=%%a>>c:\Bulker\temp\%incremar%.txt
set part2=%%b>>c:\Bulker\temp\%incremar%.txt
set part3=%%c>>c:\Bulker\temp\%incremar%.txt
set part4=%%d>>c:\Bulker\temp\%incremar%.txt
set part5=%%e>>c:\Bulker\temp\%incremar%.txt
set /A incremar=%incremar%+1
pause
)


What am i doing wrong?


See More: Batch that outputs some tokens from a file

Report •


#1
October 18, 2011 at 08:50:54
✔ Best Answer
You need to enable "Delayed Expansion"; there are other bugs in your script and, by the way, this is NT batch since there is no DOS under Windows Vista.

setlocal EnableDelayedExpansion
set N=0
for /F "tokens=5-9 delims=|" %%a in ('type C:\Bulker\trade.txt ^| find "trade"') do (
  echo.%%a>> C:\Bulker\temp\!N!.txt
  echo.%%b>> C:\Bulker\temp\!N!.txt
  echo.%%c>> C:\Bulker\temp\!N!.txt
  echo.%%d>> C:\Bulker\temp\!N!.txt
  echo.%%e>> C:\Bulker\temp\!N!.txt
  set N+=1
)


Report •

#2
October 19, 2011 at 07:47:44
I had to modify yours slightly to make it work on my system.

setlocal EnableDelayedExpansion
set N=0
for /F "tokens=5-9 delims=|" %%a in ('type C:\Bulker\trade.txt ^| find "trade"') do (
echo.%%a>> C:\Bulker\temp\!N!.txt
echo.%%b>> C:\Bulker\temp\!N!.txt
echo.%%c>> C:\Bulker\temp\!N!.txt
echo.%%d>> C:\Bulker\temp\!N!.txt
echo.%%e>> C:\Bulker\temp\!N!.txt
set /A N+=1
)

I still have a problem that for some reason %%e increments by 1 which is retarded and strange and doesn't make any sense...
Do you know what might be going on?


Report •

#3
October 19, 2011 at 08:14:29
May you explain better the trouble you are facing since I don't understand what you posted (for some reason %%e increments by 1)?

And what did you slightly change?


Report •

Related Solutions

#4
October 19, 2011 at 23:20:58
I simply changed the set N+=1 statement in your code to set /A N+=1 because it would not increment on each loop otherwise.

Also as i said each time this DO does it's loop, %%e increments by 1.
See this screenshot.

http://imageshack.us/f/338/eroop.jpg/


Report •

#5
October 19, 2011 at 23:52:24
Ok Nevermind i managed to fix that part, just now i'm having trouble with my !N! statements.
They are always outputed as !N! and not what !N!'s value is.

Report •

#6
October 20, 2011 at 03:12:18
Sorry for my mistake, I did not notice the /A missing. Just curious about what pushed %%e to increment; maybe your fix may cause now the !N! trouble. Let me know.c

Report •

#7
October 20, 2011 at 03:22:01
I had some bad values in the file called trade.txt which the batch was reading so for some reason because of that the %%e value was incrementing constantly.

As soon as i deleted that file everything was fine.

And i'm still having trouble with the !N! Value.

I do the following below but the !N! Values remain as !N! and do not expand.

setlocal EnableDelayedExpansion
set N=0
for /F "tokens=5,6,7,8,9,10 delims=|" %%a in ('type C:\Bulker\grinder\log.log ^| find "trade"') do (
echo %%a>> C:\Bulker\temp\C!N!.txt
echo %%b>> C:\Bulker\temp\V!N!.txt
echo %%c>> C:\Bulker\temp\P!N!.txt
echo %%d>> C:\Bulker\temp\SL!N!.txt
echo %%e>> C:\Bulker\temp\TP!N!.txt
echo !N! !n! %N% %%n
)

Funny enough, this last line with the ECHO !N! works, but anything on top of it doesn't...


Report •

#8
October 20, 2011 at 07:55:24
Stated in your post set /A N+=1 vanished, be aware the N expanded in the last line may be the %N% since !N! is enabled by the Delayed Expansion that is mandatory in a For loop to take account of variable change.

Are you sure the setlocal is correctly coded.


Report •

#9
October 20, 2011 at 23:32:43
I see,
It ends up it was working all along, the output on the command line just wasn't visible as it was doing the actualy expansion but it wasn't graphically showing it.

Report •


Ask Question