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