Reputation: 1
I have a batch script which parses an XML file and creates a file with the extracted data (extracted file).
In the same batch script I then use the extracted file to create a file with SQL commands (sql file).
When I start to create the SQL file I get the last record (line) from the extracted file first and the statement is created correctly.
How do stop from getting the last record first at :buildsql?
@echo off
echo Beginning parsing of XML files for pagecount and docid extraction.
echo Processing...
SET /A counter = 0
FOR /F %%i IN (CDIZeroPage.TXT) DO (
IF EXIST %%i (
FOR /F "tokens=12 delims==" %%h IN (%%i) DO (CALL :pagestring %%h)
FOR /F "tokens=8 delims==" %%g IN (%%i) DO (CALL :docidstring %%g)
)
)
goto buildsql
:pagestring
echo %1 >> C:\Richtemp\PagesandDocs.txt
CALL set page=%1
goto :eof
:docidstring
echo %1 >> C:\Richtemp\PagesandDocs.txt
CALL SET docid=%1
SET /A counter+=1
goto :eof
:buildsql
echo End extracting pagecount and docid.
echo Number of files processed %counter%
echo
echo Begin building SQL
echo Processing...
SET /A counter=0
SET /A countbytwo=0
FOR /F %%i IN (PagesandDocs.txt) DO (
CALL :subroutine %%i
SET /A counter+=1
)
goto endofscript
:subroutine
SET /A countbytwo+=1
IF %countbytwo%==1 (
FOR /F "DELIMS=" %%G IN ('ECHO %1') DO (SET page=%%~G)
echo Update documents set pagecount = %page% >> C:\Richtemp\Update.txt
)
IF %countbytwo%==2 (
FOR /F "DELIMS=" %%G IN ('ECHO %1') DO (SET docid=%%~G)
echo where uniqueid = %docid% ^; >> C:\Richtemp\Update.txt
SET /A countbytwo=0
)
goto :eof
:endofscript
SET /A counter/=2
echo End building sql, number of scripts built %counter%
:eof
Upvotes: 0
Views: 71
Reputation: 3685
Your problem is here:
:docidstring
echo %1 >> C:\Richtemp\PagesandDocs.txt
CALL SET docid=%1
SET /A counter+=1
goto :eof
in the code above, you set docid. At the end of your first phase of processing, it will store last line in docid
. Then you call buildsql
, and problem is here:
IF %countbytwo%==2 (
FOR /F "DELIMS=" %%G IN ('ECHO %1') DO (SET docid=%%~G)
echo where uniqueid = %docid% ^; >> C:\Richtemp\Update.txt
SET /A countbytwo=0
)
You would think that echo
will use docid
you just set, unfortunately it's not. It gets substituted earlier on expansion with old value of docid
... which on first call .refers to your last line. Exactly same error happens with page
variable
You probably could use delayed expansion to get round this, but your code looks overly complicated. What exactly you want to achieve? If the intention was to extract items from positions 12,8 on a line and build a query expression like this:
Update documents set pagecount = #12 where where uniqueid = #8;
it will not work as intended because you first offload all your #12, and then #8 into file.
Maybe this would work for you?
setlocal
FOR /F %%i IN (CDIZeroPage.TXT) DO (
FOR /F "tokens=8-12 delims==" %%j IN (%%i) do (
ECHO Update documents set pagecount = %%n where where uniqueid = %%j ^; >>update.txt
SET /A counter+=1
)
)
echo End building sql, number of scripts built %counter%
Upvotes: 1