flyingbird013
flyingbird013

Reputation: 476

Batch : Echo can not save to text file

I want to save to text file, result of batch script bellow:

My Run.bat contains :

for /f "tokens=5" %%a in ('findstr /L "Total " max_page.csv') do echo %%a> max_page.txt
set /p max_page=<max_page.txt
for /l %%i IN (1,1,%max_page%) do (
echo OPTIONS (SKIP=0, errors=1000) LOAD DATA  APPEND INTO TABLE tb_data FIELDS TERMINATED BY X'09' TRAILING NULLCOLS (no "TRIM (:no)",date_pymn DATE "DD-MM-YYYY") > data.ctl
)

Where my max_page.csv contains :

aaaaaaaaaaaa
bbbbbbbbbbbb
cccccccccccc

Total 6434 Record : 65 Page

Result of line for /f "tokens=5" %%a in ('findstr /L "Total " max_page.csv') do echo %%a> max_page.txt is 65 and saved to txt file (max_page.txt).

If I execute the RUN.bat, display on the screen error LOAD was unexpected at this time. LOAD is part of line echo OPTIONS (SKIP=0, errors=1000) LOAD DATA APPEND INTO TABLE tb_data FIELDS TERMINATED BY X'09' TRAILING NULLCOLS (no "TRIM (:no)",date_pymn DATE "DD-MM-YYYY") > data.ctl

Where mistake my script? Thank you for your suggestion

Upvotes: 0

Views: 133

Answers (2)

Magoo
Magoo

Reputation: 80023

The easy way is to escape the ) characters that are erroneously being interpreted as end-of-statement with a caret ^

Hence (2 carets inserted)

echo OPTIONS (SKIP=0, errors=1000^) LOAD DATA  APPEND INTO TABLE tb_data FIELDS TERMINATED BY X'09' TRAILING NULLCOLS (no "TRIM (:no)",date_pymn DATE "DD-MM-YYYY"^) > data.ctl

There appears to be no logical reason for your second for loop - it merely writes the line of text to the file and then overwrites that file with the same line 64 more times (given max_page gets set to 65.)

Similarly, 'max_page.txt` is created and overwritten until the last "Total" line in the source file. It would be easier to use

for /f "tokens=5" %%a in ('findstr /L "Total " max_page.csv') do set max_page%%a

and not create max_page.txt at all...

Upvotes: 1

RGuggisberg
RGuggisberg

Reputation: 4750

The first ) in

echo OPTIONS (SKIP=0, errors=1000) LOAD DATA  APPEND INTO TABLE tb_data FIELDS TERMINATED BY X'09' TRAILING NULLCOLS (no "TRIM (:no)",date_pymn DATE "DD-MM-YYYY") > data.ctl

is being interpreted as the end of the FOR loop. Since you only have one line in the loop, get rid of the opening and closing parens and put the entire FOR construct on one line like this

for /l %%i IN (1,1,%max_page%) do echo OPTIONS (SKIP=0, errors=1000) LOAD DATA  APPEND INTO TABLE tb_data FIELDS TERMINATED BY X'09' TRAILING NULLCOLS (no "TRIM (:no)",date_pymn DATE "DD-MM-YYYY") > data.ctl

Upvotes: 1

Related Questions