Getting the missing page numbers [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

Getting the missing page numbers [SOLVED]

#1 Post by abc0502 » 06 Aug 2012 06:40

Hi, I'm having a nother problem, my code in some point get a list of page range
in this form:
1 1
2 2
3 4
6 7
9 12

each line has two numbers separated by a space, the left column of numbers is a start page, and the right column numbers is the end pages.

As you can senn in the 4th line it start with the page number 6 but the set of number before it end with the page 4 "the page 5 is missing". and so is the 5th line.

what i need is a code that fix the missing page numbers if exist.
you can separate each column in a separate file and do the work.

I mad this code but it work on assumbtion that the left and right column is the same,
so it take each number and subtract 1 and make it as a new value but it has a downfall,
it end with a list starting from 0 and it substract every value even the one that doesn't need to be changed.

Code: Select all

For /R %%z in (Folder\Bookmarks\*.txt) Do (
   For /F "tokens=*" %%a in (Folder\range\%%~nz\tmp_max.txt) Do (
   call :check "Folder\range\%%~nz\max.txt" max %%a
   )
)

:: this work as follow:
:: call :function_name "output_file" "assaigned_variable" "list_of numbers"
:check
set %2=%3
:loop
if "%3" equ "" goto :eof
set /a max=%3 - 1
if !max!==0 set max=%3
echo !max!>>%1
goto loop

forgot to say the list must be in this form, "1 2 3 5 6 88 98"
i convert a virtecal list to horizontal using this commad

Code: Select all

For /F "tokens=1,2 delims= " %%a in ('type "Folder\all.txt"') Do (
   <nul set /p "=%%b ">>Folder\max_range.txt
)
Last edited by abc0502 on 06 Aug 2012 13:10, edited 1 time in total.

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

Re: Getting the missing page numbers

#2 Post by foxidrive » 06 Aug 2012 07:52

abc0502 wrote:Hi, I'm having a nother problem, my code in some point get a list of page range
in this form:
1 1
2 2
3 4
6 7
9 12

each line has two numbers separated by a space, the left column of numbers is a start page, and the right column numbers is the end pages.

As you can senn in the 4th line it start with the page number 6 but the set of number before it end with the page 4 "the page 5 is missing". and so is the 5th line.


But should the missing line be 5 5 or 5 6? How do we tell? What if it is a range that is missing? Like this:

1 1
2 2
3 4
6 7
9 12
18 20
20 29

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

Re: Getting the missing page numbers

#3 Post by abc0502 » 06 Aug 2012 07:57

hi foxidrive thanks for answering,
the first column will be the same "always", the end if each range is the one that will have to be changed.

in your example:
1 1
2 2
3 4
6 7
9 12
18 20
20 29

the 4 should be 5 because the next is 6, it is a sequance.
and the 7 should be 8 because the next is 9 and so is the rest

the end of a page is the next page -1, but the last number 29
will be a variable taken from a file so no worry about it
Last edited by abc0502 on 06 Aug 2012 08:02, edited 1 time in total.

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

Re: Getting the missing page numbers

#4 Post by abc0502 » 06 Aug 2012 08:01

forgot to say these ranges are taken from separate files and merged into one
and also these ranges are the final ranges after 1 or 2 process i made on them.

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

Re: Getting the missing page numbers

#5 Post by foxidrive » 06 Aug 2012 08:53

I confused myself but this seems to work by taking the left hand column and calculating one less than it, and outputting the line before it with the new number in the second column.

See if it works for you.

Code: Select all

@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /f "tokens=1,2" %%a in (file.txt) do (
if not defined olda (
set olda=%%a
) else (
set newa=%%a
set /a num=newa-1
>>file2.txt echo !olda! !num!
set oldb=%%b
set olda=%%a
)
)
>>file2.txt echo %olda% %oldb%



Input
1 1
2 2
3 4
6 7
9 12
18 20
20 29



Output
1 1
2 2
3 5
6 8
9 17
18 19
20 29

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

Re: Getting the missing page numbers

#6 Post by Squashman » 06 Aug 2012 08:54

Just an Idea. :idea:

Could you read the same file as two separate data streams? One inside the for loop and one from redirection using SET /P. Then the FOR loop could do a SKIP=1 on the file so that it would always be reading the line ahead of the file stream. Then you could compare the numbers to see if they are -1 from each other.

Code: Select all

@echo off
setlocal DisableDelayedExpansion
< file1.txt (
   for /F "skip=1 delims=" %%a in (file1.txt) do (
      set file1Line=
      set /P file1Line=
         )
)

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

Re: Getting the missing page numbers

#7 Post by abc0502 » 06 Aug 2012 11:19

Hi Foxidrive and Squashman
Thanks alot for your help,
@Foxidrive your code is working perfect, i tested on another ranges and it worked like i wan't it to be, thanks :D

@Squashman your idea is good, i was trying to do the same before posting my problem here but didn't know how thanks :D

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

Re: Getting the missing page numbers

#8 Post by Squashman » 06 Aug 2012 11:38

abc0502 wrote:Hi Foxidrive and Squashman
Thanks alot for your help,
@Foxidrive your code is working perfect, i tested on another ranges and it worked like i wan't it to be, thanks :D

@Squashman your idea is good, i was trying to do the same before posting my problem here but didn't know how thanks :D

I will give it a shot but I already got more lines of code written in my head then what Foxidrive coded. Probably won't be as efficient.

Post Reply