Modifying number in file [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

Modifying number in file [SOLVED]

#1 Post by abc0502 » 01 Sep 2012 01:26

Hi,
I have a problem writing a code, I posted a topic before asking for a code to fix page numbers,
and Foxidrive helped me and wrote that code.

The problem now it that i need to replace the page numbers if it not equal to a certin numbers taken
from a separate file.

The file that needs to be modified looks like this:

Code: Select all

Parent_line1/1,Black,notBold,notItalic,open,TopLeftZoom,1,0,0.0
Parent_line2/2,Black,notBold,notItalic,closed,TopLeftZoom,1,0,0.0
   Chiled_line2-1/2,Black,notBold,notItalic,open,TopLeftZoom,211,8,0.0 
   Chiled_line2-2/2,Black,notBold,notItalic,open,TopLeftZoom,554,8,0.0 
   Chiled_line2-3/3,Black,notBold,notItalic,open,TopLeftZoom,176,8,0.0
Parent_line3/4,Black,notBold,notItalic,closed,TopLeftZoom,0,0,0.0
   Chiled_line3-1/4,Black,notBold,notItalic,open,TopLeftZoom,56,10,0.0 
   Chiled_line3-2/4,Black,notBold,notItalic,open,TopLeftZoom,281,10,0.0 
   Chiled_line3-3/4,Black,notBold,notItalic,open,TopLeftZoom,577,10,0.0 
   Chiled_line3-4/5,Black,notBold,notItalic,open,TopLeftZoom,56,10,0.0


"The space in front of the child lines is a TAB character not a space, this is the only way to know if the line is parent or child"

The numbers between the "/" and the "," is the page numbers that needs to be changed if needed.
and The list that hold the correct page numbers is like this

Code: Select all

1
2
5


each line of that list represent a Parent_line, so line one has number "1" and it is supposed to be the same page number in "Parent_line1"
in the first file, if not it should be then replaced and check it's child_lines to fix the difference in pages

so "parent_line2" should have page number equal to "2" and "Parent_line3" should have page number "5"

In "Parent_line3" it has page number "4" and it supposed to be "5" so it will be changed to "5" but we will need then to change all it's
"Chiled_lines" page numbers by adding the difference between the child page numbers and the old parent number which is 4.

This is the code i mad till now but it's not giving me what i want
book.txt is the one that has the first file and the list is the file that has the list of numbers.

Code: Select all

Setlocal EnableDelayedExpansion
   For /F "tokens=1,2,* delims=/," %%a in ('type "book.txt"') Do (
      set "original=%%b"
      :: check parent or child line
      Echo "%%a" |find "   " >nul
         :: if line is parent
         IF !errorlevel! EQU 1 (
            :: parent line
            set c=0
            For /F "tokens=1 delims= " %%A in ('type "list.txt"') Do (
                     set /a c+=1
                     IF "%%b" EQU "%%A" (
                        :: no change
                        echo %%a/%%b,%%c>>"new_book.txt"
                     ) else (
                        :: change
                        set "new=%%A"
                        set /a diff = new - original
                              Echo "%%a" |find "   " >nul
                              IF !errorlevel! EQU 1 (
                                 echo %%a/%%A,%%c>>"new_book.txt"
                              ) else (
                                 IF "%%b" LSS "%%A" ( echo %%a/%%A,%%c>>"new_book.txt" )
                                 IF "%%b" EQU "%%A" (
                                    set /a num = original + diff
                                    echo %%a/!num!,%%c>>"new_book" )   )
                     )
            
            )
         ) else (
         set c=0
            For /F "tokens=1 delims= " %%A in ('type "list.txt"') Do (
               set "new=%%A"
               set /a diff = new - original
                     set /a c+=1
                     IF "%%b" EQU "%%A" (
                                    set /a num = original + diff
                                    echo %%a/!num!,%%c>>"new_book.txt" )
                     IF "%%b" LSS "%%A" ( echo %%a/%%A,%%c>>"new_book.txt" )
            )               
         )
   )
)
pause


and this is the topic Foxidrive helped me to write the code that made the book.txt file viewtopic.php?f=3&t=3623

The output of the book.txt file should be look like this:

Code: Select all

Parent_line1/1,Black,notBold,notItalic,open,TopLeftZoom,1,0,0.0
Parent_line2/2,Black,notBold,notItalic,closed,TopLeftZoom,1,0,0.0
   Chiled_line2-1/2,Black,notBold,notItalic,open,TopLeftZoom,211,8,0.0 
   Chiled_line2-2/2,Black,notBold,notItalic,open,TopLeftZoom,554,8,0.0 
   Chiled_line2-3/3,Black,notBold,notItalic,open,TopLeftZoom,176,8,0.0
Parent_line3/5,Black,notBold,notItalic,closed,TopLeftZoom,0,0,0.0
   Chiled_line3-1/5,Black,notBold,notItalic,open,TopLeftZoom,56,10,0.0 
   Chiled_line3-2/5,Black,notBold,notItalic,open,TopLeftZoom,281,10,0.0 
   Chiled_line3-3/5,Black,notBold,notItalic,open,TopLeftZoom,577,10,0.0 
   Chiled_line3-4/6,Black,notBold,notItalic,open,TopLeftZoom,56,10,0.0
Last edited by abc0502 on 01 Sep 2012 05:05, edited 1 time in total.

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

Re: Modifying number in file

#2 Post by abc0502 » 01 Sep 2012 02:06

i tried what squashman suggested before In the link in first post, and managed to do this till now, but when it come to the Parent_line3, it doesn't show the page number,
and how i can go to check ("parent_line3") it's child lines?

Code: Select all

@echo off
cls

Setlocal EnableDelayedExpansion

<num.txt (
   For /F "tokens=1,2,* delims=/," %%A in ('type "book.txt"') Do (
         set num_file=
         set /p num_file=

            Echo "%%A" |find "   " >nul
            IF !errorlevel! EQU 1 (
                     if !num_file! EQU %%B (
               
                        echo %%A/%%B,%%C
                  
                     ) else ( echo %%A/!num_file!,%%C )
               
            ) else ( echo %%A/%%B,%%C )

   )
)
pause

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

Re: Modifying number in file

#3 Post by foxidrive » 01 Sep 2012 02:53

We don't have all the details but it seems that this task should have a simpler solution, by going back a few steps and looking for a tool that writes the bookmarks properly.

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

Re: Modifying number in file

#4 Post by abc0502 » 01 Sep 2012 04:22

@ Foxidrive tnaks, for your replay, i already used a command line tool to extract bookmarks but due to the operation i make on that bookmarks the page numbers changes, But fortunately I just finished the code and it now works great :D

here it is

Code: Select all

Setlocal EnableDelayedExpansion
<num.txt (
   For /F "tokens=1,2,* delims=/," %%A in ('type "book.txt"') Do (
         set /p num_file=
            Echo "%%A" |find "   " >nul
            :: if line is parent
            IF !errorlevel! EQU 1 (
                  set "parent_line=%%B"
                     :: if the line page match, then no changes
                     if !num_file! EQU %%B (   echo %%A/%%B,%%C
                           
                        ) else (
                           set curr=%%B
                           :: this variable will be used later in Child lines
                           set /a diff = num_file - curr
                           echo %%A/!num_file!,%%C )
            ) else (
               set curr_c=%%B
               IF "!parent_line!" EQU "!curr_c!" (
                     set /a new = curr_c + diff
                     echo %%A/!new!,%%C
                  ) else (
                     set /a new = curr_c + diff
                     echo %%A/!new!,%%C ) )
   )
)
pause

the problem was in that line

Code: Select all

 set num_file=

it was resetting the value of the num_file every time it go throw the bookmark file.
after i removed that it become easier

:mrgreen:

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

Re: Modifying number in file

#5 Post by foxidrive » 01 Sep 2012 04:33

Well done. It's a fiddly procedure to get all the numbers right for those bookmarks.

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

Re: Modifying number in file

#6 Post by abc0502 » 01 Sep 2012 05:05

foxidrive wrote:Well done. It's a fiddly procedure to get all the numbers right for those bookmarks.

Thanks, and now this topic considered solved

Post Reply