help with looping with lines [SOLVED]

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

help with looping with lines [SOLVED]

#1 Post by abc0502 » 23 Jul 2012 18:33

Hi, I'm writing a code to compine the pdf bookmarks based on it's first country letters "UK, US, .." that rjobaan was asking about,
This is a part of a bookmark file to explain on:
the spaces in the 2nd and 3rd are one "tab" not a space

Code: Select all

UK/1,Black,notBold,notItalic,closed,TopLeftZoom,1,0,0.0
   KPI1/1,Black,notBold,notItalic,open,TopLeftZoom,238,8,0.0
   KPI2/1,Black,notBold,notItalic,open,TopLeftZoom,349,8,0.0

The first line we will call it the parent line, and the 2nd and 3rd lines is child lines now i managed to make a batch that loop throw the bookmark file and based on the first 2 letters "the country, US,..etc" it make a file named with that name and copy that line to it.

but, I'm stuck in how to make a code that loop throw the bookmark file and when it find a child line "the child line will always start with "tab then KPI" it go to the previous line to see if it is parent line or not,
If it finds that the previous line is a parent line it copy the child line to the file named with the first 2 letters of that parent line "UK",
but if it isn't a parent line it go back another line to check if it is a parent line or not, so it keep going back till it find the first parent line and then it copy that child line to the file named based on the first 2 letters of the parent line

I hope i explained that well,and If possible just need the code to work on that example.

Edit: just to clearify the Parent line won't start with "KPI" " If we ignored the 'TAB' " But the Child Line will
Thanks
:)
Last edited by abc0502 on 24 Jul 2012 21:28, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: help with looping with lines

#2 Post by foxidrive » 24 Jul 2012 01:27

Will every line that has a tab in the first position be a child of the previous parent line?

Is so then store each parent line, or just the two characters, and when you encounter a line that starts with a TAB then automatically send it to the parent file.


Or in pseudo code:
Check the first character and if it is not a TAB then (set country=twocharacters) else (echo line to %country% file)

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: help with looping with lines

#3 Post by abc0502 » 24 Jul 2012 15:44

Hi, foxidrive sorry i didn't post the full bokkmark file, there will be another parent lines starting with US and FR too,
so i wanted it the code to be wworking in general not stricted in pre-defined options so it can work on anybookmark file
and setting the country like waht you said will make that code work for just that file.
here is the full file:

Code: Select all

UK/1,Black,notBold,notItalic,closed,TopLeftZoom,1,0,0.0
        KPI1/1,Black,notBold,notItalic,open,TopLeftZoom,238,8,0.0
        KPI2/1,Black,notBold,notItalic,open,TopLeftZoom,349,8,0.0
        KPI3/1,Black,notBold,notItalic,open,TopLeftZoom,462,8,0.0
US/2,Black,notBold,notItalic,open,TopLeftZoom,1,0,0.0
NL/3,Black,notBold,notItalic,closed,TopLeftZoom,1,0,0.0
        KPI1/3,Black,notBold,notItalic,open,TopLeftZoom,213,8,0.0
        KPI2/3,Black,notBold,notItalic,open,TopLeftZoom,469,8,0.0
        KPI3/4,Black,notBold,notItalic,open,TopLeftZoom,63,8,0.0
FR/5,Black,notBold,notItalic,open,TopLeftZoom,1,0,0.0
GR/6,Black,notBold,notItalic,open,TopLeftZoom,1,0,0.0
PL/7,Black,notBold,notItalic,open,TopLeftZoom,1,0,0.0


foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: help with looping with lines

#4 Post by foxidrive » 24 Jul 2012 19:34

So the UK entry has three child entries, and so does the NL.

If that is right then my algorithm will work for any file in that format, with 1,2, or 200 child entries.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: help with looping with lines

#5 Post by foxidrive » 24 Jul 2012 20:09

Here is some working code: The find command needs to have a TAB in there.

It assumed that only the child lines will have a TAB character - but you can use findstr to ensure it is at the start of the line if necessary.


@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /f "delims=" %%a in (bookmark.txt) do (
echo "%%a" |find " " >nul
if !errorlevel! EQU 1 (
set "country=%%a"
set "country=!country:~0,2!"
>>"!country!.txt" echo %%a
) else (
>>"!country!.txt" echo %%a
)
)

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: help with looping with lines

#6 Post by abc0502 » 24 Jul 2012 20:16

Hi, Foxidrive thanks alot that work perfectly, you don't know how that helped me
thanks :D

Post Reply