--------------------------------------------------------------------------------------------------------
                      [BUG/PRB.] VFP 9.0 FIX - CHECK THE DESTINATION FILE FOR THE COPY TO TEXTFILE DELIMITED COMMAND
                               January 2024
                 --------------------------------------------------------------------------------------------------------
                                     CCB



1. BUG:

     In vfp9 (and vfp6, vfp7, vfp8), sometimes it causes the error "Invalid path or file name (Error 202)"
     or the error "File already exists as part of a database (Error 1529)" for the COPY TO textfile DELIMITED command.

     There is a test program:

     *PROC testcopytotextfiledelimited

     =STRTOFILE(CHR(0x30)+REPL("a",7)+CHR(0x00)+CHR(0x02)+CHR(0x00)+CHR(0x00)+REPL("a",1000),"test.tmp")

     SELE 0
     CREATE CURSOR tmp1 (fld1 c(10))
     APPEND BLANK
     REPL fld1 WITH "aaa"
     COPY TO "test.tmp" DELIMITED
     USE

     RETURN

     * END OF PROC TESTCOPYTOTEXTFILEDELIMITED.

     We think it will run fine, but vfp will cause the error "Invalid path or file name (Error 202)".


2. CAUSE:

     In vfp9 (and vfp6, vfp7, vfp8), for the following COPY TO commands:
     COPY TO test.dbf
     COPY TO test.dbf TYPE FOXPLUS | FOX2X 
     If the destination file exists, and it is a DBF file (or it seems it is a DBF file),
     vfp will check the destination file,
     if it is a table of a database (or it is not a free table),
     vfp will cause the error "File already exists as part of a database" (Error 1529).

     In vfp9 (and vfp6, vfp7, vfp8), for the following COPY TO commands:
     COPY TO test.txt TYPE DIF | MOD | SDF | SYLK | WK1 | WKS | WR1 | WRK | CSV | XLS | XL5
     vfp will not check the destination file.

     In vfp9 (and vfp6, vfp7, vfp8), for the following COPY TO commands:
     COPY TO test.txt DELIMITED WITH Delimiter | WITH BLANK | WITH TAB | WITH CHARACTER Delimiter
     vfp will check the destination file.

     For example, run the following code in vfp9,
     COPY TO "test.tmp" DELIMITED
     for the destination file "test.tmp", it seems it is a DBF file (the first byte is CHR(0x30)),
     vfp will check the destination file,
     but in fact, it is not a valid DBF file,
     so vfp will cause the error "Invalid path or file name (Error 202)".

     In Visual FoxPro Advanced, for the following COPY TO commands:
     COPY TO test.txt DELIMITED WITH Delimiter | WITH BLANK | WITH TAB | WITH CHARACTER Delimiter
     vfp will NOT check the destination file.


3. RESOLUTION:

     We can write some code to fix the BUG.

     Label4ba44c ::
             push ebp                                                        ;0x004ba44c :        55
             mov eax , 02E94h                                                ;0x004ba44d :        b8942e0000
             lea ebp ,  dword ptr [ esp - 120 ]                              ;0x004ba452 :        8d6c2488
             call Fun42c118                                                  ;0x004ba456 :        e8bd1cf7ff
             mov eax ,  dword ptr [ Data937090 ]                             ;0x004ba45b :        a190709300
             mov  dword ptr [ ebp + 116 ] , eax                              ;0x004ba460 :        894574
             mov eax ,  dword ptr [ Data9393ac ]                             ;0x004ba463 :        a1ac939300
             movzx eax ,byte ptr [eax]                                       ;0x004ba468 :        0fb600
             push ebx                                                        ;0x004ba46b :        53
             xor ebx , ebx                                                   ;0x004ba46c :        33db
             sub eax , 012h                                                  ;0x004ba46e :        83e812
             push esi                                                        ;0x004ba471 :        56
             push edi                                                        ;0x004ba472 :        57
             mov  byte ptr [ ebp + 0FFFFD263h ] , bl                         ;0x004ba473 :        889d63d2ffff
             mov  byte ptr [ ebp + 0FFFFD261h ] , bl                         ;0x004ba479 :        889d61d2ffff
             mov  byte ptr [ ebp + 0FFFFD264h ] , bl                         ;0x004ba47f :        889d64d2ffff
             mov  byte ptr [ ebp + 0FFFFD266h ] , bl                         ;0x004ba485 :        889d66d2ffff
             mov  byte ptr [ ebp + 0FFFFD267h ] , bl                         ;0x004ba48b :        889d67d2ffff
             mov  byte ptr [ ebp + 0FFFFD265h ] , bl                         ;0x004ba491 :        889d65d2ffff
             mov  byte ptr [ ebp + 0FFFFD262h ] , bl                         ;0x004ba497 :        889d62d2ffff
             mov  byte ptr [ ebp + 0FFFFD260h ] , bl                         ;0x004ba49d :        889d60d2ffff
             mov  byte ptr [ ebp + 0FFFFD25Eh ] , bl                         ;0x004ba4a3 :        889d5ed2ffff
             mov  byte ptr [ ebp + 0FFFFD23Ch ] , bl                         ;0x004ba4a9 :        889d3cd2ffff
             mov  byte ptr [ ebp + 0FFFFD25Fh ] , bl                         ;0x004ba4af :        889d5fd2ffff
             mov  byte ptr [ ebp + 0FFFFD25Ah ] , bl                         ;0x004ba4b5 :        889d5ad2ffff
             mov  byte ptr [ ebp + 0FFFFD244h ] , 022h                       ;0x004ba4bb :        c68544d2ffff22
             mov  byte ptr [ ebp + 0FFFFD24Ch ] , 02Ch                       ;0x004ba4c2 :        c6854cd2ffff2c
             mov  dword ptr [ ebp + 0FFFFD250h ] , ebx                       ;0x004ba4c9 :        899d50d2ffff
             je Label57a897                                                  ;0x004ba4cf :        0f84c2030c00
             sub eax , 09h                                                   ;0x004ba4d5 :        83e809
             je Label57a88d                                                  ;0x004ba4d8 :        0f84af030c00
             sub eax , 0A3h                                                  ;0x004ba4de :        2da3000000
             je Label57a88d                                                  ;0x004ba4e3 :        0f84a4030c00
             sub eax , 03h                                                   ;0x004ba4e9 :        83e803
             je Label57a858                                                  ;0x004ba4ec :        0f8466030c00
             sub eax , 09h                                                   ;0x004ba4f2 :        83e809
             je Label57a84e                                                  ;0x004ba4f5 :        0f8453030c00
             call Fun52d193                                                  ;0x004ba4fb :        e8932c0700
             mov  dword ptr [ ebp + 0FFFFD240h ] , eax                       ;0x004ba500 :        898540d2ffff
             mov eax ,  dword ptr [ Data9393ac ]                             ;0x004ba506 :        a1ac939300
             mov  al , byte ptr [eax]                                        ;0x004ba50b :        8a00
             inc  dword ptr [ Data9393ac ]                                   ;0x004ba50d :        ff05ac939300
             cmp al , 028h                                                   ;0x004ba513 :        3c28
             jne Label57a2c1                                                 ;0x004ba515 :        0f85a6fd0b00
             mov eax ,  dword ptr [ Data9393ac ]                             ;0x004ba51b :        a1ac939300
             cmp  byte ptr [eax] , 04h                                       ;0x004ba520 :        803804
             je Label57a2cb                                                  ;0x004ba523 :        0f84a2fd0b00
             call Fun522cc9                                                  ;0x004ba529 :        e89b870600
             mov  dword ptr [ ebp + 0FFFFD234h ] , eax                       ;0x004ba52e :        898534d2ffff
             mov eax ,  dword ptr [ Data9393ac ]                             ;0x004ba534 :        a1ac939300
             cmp  byte ptr [eax] , 0CCh                                      ;0x004ba539 :        8038cc
             je Label48861c                                                  ;0x004ba53c :        0f84dae0fcff

     Label4ba542 ::
             cmp  byte ptr [eax] , 0D2h                                      ;0x004ba542 :        8038d2
             je Label57a2db                                                  ;0x004ba545 :        0f8490fd0b00

     Label4ba54b ::
             cmp  byte ptr [eax] , 0C0h                                      ;0x004ba54b :        8038c0
             je Label57a2ed                                                  ;0x004ba54e :        0f8499fd0b00

     Label4ba554 ::
             pushd 04h                                                       ;0x004ba554 :        6a04
             lea esi ,  dword ptr [ ebp + 0FFFFD268h ]                       ;0x004ba556 :        8db568d2ffff
             call Fun4ba369                                                  ;0x004ba55c :        e808feffff
             mov  ecx , dword ptr [ ebp + 0FFFFD26Ch ]                       ;0x004ba561 :        8b8d6cd2ffff
             mov  eax , dword ptr [ ebp + 0FFFFD270h ]                       ;0x004ba567 :        8b8570d2ffff
             mov  dword ptr [ ebp + 0FFFFD248h ] , ecx                       ;0x004ba56d :        898d48d2ffff
             mov  dword ptr [ ebp + 0FFFFD238h ] , eax                       ;0x004ba573 :        898538d2ffff
             call Fun4ba424                                                  ;0x004ba579 :        e8a6feffff
             mov eax ,  dword ptr [ Data9393ac ]                             ;0x004ba57e :        a1ac939300
             cmp  byte ptr [eax] , 0D4h                                      ;0x004ba583 :        8038d4
             je Label57a2ff                                                  ;0x004ba586 :        0f8473fd0b00

     Label4ba58c ::
             mov  cl , byte ptr [eax]                                        ;0x004ba58c :        8a08
             movzx edx , cl                                                  ;0x004ba58e :        0fb6d1
             add edx , 0FFFFFF46h                                            ;0x004ba591 :        81c246ffffff
             cmp edx , 01Bh                                                  ;0x004ba597 :        83fa1b
             jbe Label57a30a                                                 ;0x004ba59a :        0f866afd0b00

     Label4ba5a0 ::
             cmp  byte ptr [eax] , 0BEh                                      ;0x004ba5a0 :        8038be
             je Label57a46a                                                  ;0x004ba5a3 :        0f84c1fe0b00

     Label4ba5a9 ::
             cmp  byte ptr [ ebp + 0FFFFD25Ah ] , bl                         ;0x004ba5a9 :        389d5ad2ffff
             jne Label4ba5f8                                                 ;0x004ba5af :        7547
             cmp  byte ptr [ ebp + 0FFFFD267h ] , bl                         ;0x004ba5b1 :        389d67d2ffff
             jne Label4ba5f8                                                 ;0x004ba5b7 :        753f
             cmp  byte ptr [ ebp + 0FFFFD265h ] , bl                         ;0x004ba5b9 :        389d65d2ffff
             jne Label4ba5f8                                                 ;0x004ba5bf :        7537
             cmp  byte ptr [ ebp + 0FFFFD266h ] , bl                         ;0x004ba5c1 :        389d66d2ffff
             jne Label4ba5f8                                                 ;0x004ba5c7 :        752f
             cmp  byte ptr [ ebp + 0FFFFD262h ] , bl                         ;0x004ba5c9 :        389d62d2ffff
             jne Label57a51e                                                 ;0x004ba5cf :        0f8549ff0b00
             cmp  byte ptr [ ebp + 0FFFFD264h ] , bl                         ;0x004ba5d5 :        389d64d2ffff
             mov ecx , offset Data919ea0                                              ;0x004ba5db :        b9a09e9100
             jne Label57a51e                                                 ;0x004ba5e0 :        0f8538ff0b00

     Label4ba5e6 ::
             lea edi ,  dword ptr [ ebp + 0FFFFD254h ]                       ;0x004ba5e6 :        8dbd54d2ffff
             sub edi , ecx                                                   ;0x004ba5ec :        2bf9

     Label4ba5ee ::
             mov  dl , byte ptr [ecx]                                        ;0x004ba5ee :        8a11
             mov  byte ptr [ edi + ecx ] , dl                                ;0x004ba5f0 :        88140f
             inc ecx                                                         ;0x004ba5f3 :        41
             cmp dl , bl                                                     ;0x004ba5f4 :        3ad3
             jne Label4ba5ee                                                 ;0x004ba5f6 :        75f6

     Label4ba5f8 ::
             mov esi , eax                                                   ;0x004ba5f8 :        8bf0
             mov  eax , dword ptr [ ebp + 0FFFFD234h ]                       ;0x004ba5fa :        8b8534d2ffff
             mov dword ptr [ Data9393ac ] ,  eax                             ;0x004ba600 :        a3ac939300
             lea eax ,  dword ptr [ ebp + 0FFFFFF6Ch ]                       ;0x004ba605 :        8d856cffffff
             push eax                                                        ;0x004ba60b :        50
             xor edi , edi                                                   ;0x004ba60c :        33ff
             lea ebx ,  dword ptr [ ebp + 0FFFFD254h ]                       ;0x004ba60e :        8d9d54d2ffff
             call Fun52a205                                                  ;0x004ba614 :        e8ecfb0600
             cmp eax , 02h                                                   ;0x004ba619 :        83f802
             je Label57a528                                                  ;0x004ba61c :        0f8406ff0b00
             cmp eax , 04h                                                   ;0x004ba622 :        83f804
             je Label57a528                                                  ;0x004ba625 :        0f84fdfe0b00
             cmp  byte ptr [esi] , 051h                                      ;0x004ba62b :        803e51
             mov  dword ptr [ Data9393ac ] , esi                             ;0x004ba62e :        8935ac939300
             je Label57a532                                                  ;0x004ba634 :        0f84f8fe0b00
             mov  edi , dword ptr [ ebp + 0FFFFD248h ]                       ;0x004ba63a :        8bbd48d2ffff

     Label4ba640 ::
             cmp  byte ptr [ ebp + 0FFFFD260h ] , 00h                        ;0x004ba640 :        80bd60d2ffff00
             jne Label57a57a                                                 ;0x004ba647 :        0f852dff0b00
             cmp  byte ptr [ ebp + 0FFFFD263h ] , 00h                        ;0x004ba64d :        80bd63d2ffff00
             jne Label48862e                                                 ;0x004ba654 :        0f85d4dffcff

     Label4ba65a ::
             cmp  byte ptr [ ebp + 0FFFFD261h ] , 00h                        ;0x004ba65a :        80bd61d2ffff00
             jne Label57a5d1                                                 ;0x004ba661 :        0f856aff0b00

     Label4ba667 ::
             cmp  dword ptr [ ebp + 0FFFFD250h ] , 00h                       ;0x004ba667 :        83bd50d2ffff00
             jne Label57a5e8                                                 ;0x004ba66e :        0f8574ff0b00

     Label4ba674 ::
             cmp  byte ptr [ ebp + 0FFFFD25Ah ] , 00h                        ;0x004ba674 :        80bd5ad2ffff00
             jne Label4ba6b6                                                 ;0x004ba67b :        7539
             cmp  byte ptr [ ebp + 0FFFFD267h ] , 00h                        ;0x004ba67d :        80bd67d2ffff00
             jne Label4ba6b6                                                 ;0x004ba684 :        7530
             cmp  byte ptr [ ebp + 0FFFFD265h ] , 00h                        ;0x004ba686 :        80bd65d2ffff00
             jne Label4ba6b6                                                 ;0x004ba68d :        7527
             cmp  byte ptr [ ebp + 0FFFFD262h ] , 00h                        ;0x004ba68f :        80bd62d2ffff00
             jne Label4ba6b6                                                 ;0x004ba696 :        751e
             cmp  byte ptr [ ebp + 0FFFFD266h ] , 00h                        ;0x004ba698 :        80bd66d2ffff00
             jne Label4ba6b6                                                 ;0x004ba69f :        7515


     ;
     ;                 ---------------------------------------------------------------------------------------------
     ;                      VFP 9.0 FIX - CHECK THE DESTINATION FILE FOR THE COPY TO TEXTFILE DELIMITED COMMAND
     ;                                  June 2021
     ;                 ---------------------------------------------------------------------------------------------
     ;                                     CCB
     ;
     ; Check the destination file for the COPY TO textfile DELIMITED command.
     ;
     ; 2021/6/11, by ccb
     ;

             cmp dword ptr vfpa_sys9061_data,00h
             jne Label4ba6a1
             cmp  byte ptr [ ebp + 0FFFFD264h ] , 00h
             jne Label4ba6b6


     Label4ba6a1 ::
             xor ecx , ecx                                                   ;0x004ba6a1 :        33c9
             lea edx ,  dword ptr [ ebp + 0FFFFFF6Ch ]                       ;0x004ba6a3 :        8d956cffffff
             call Fun52a2ee                                                  ;0x004ba6a9 :        e840fc0600
             test eax , eax                                                  ;0x004ba6ae :        85c0
             jne Label57a608                                                 ;0x004ba6b0 :        0f8552ff0b00

     Label4ba6b6 ::
             pushd 01h                                                       ;0x004ba6b6 :        6a01
             lea eax ,  dword ptr [ ebp + 0FFFFFF6Ch ]                       ;0x004ba6b8 :        8d856cffffff
             push eax                                                        ;0x004ba6be :        50
             call Fun52a2da                                                  ;0x004ba6bf :        e816fc0600
             test eax , eax                                                  ;0x004ba6c4 :        85c0
             je Label4bafdb                                                  ;0x004ba6c6 :        0f840f090000
             cmp  byte ptr [ ebp + 0FFFFD261h ] , 00h                        ;0x004ba6cc :        80bd61d2ffff00
             jne Label4ba730                                                 ;0x004ba6d3 :        755b
             cmp  byte ptr [ ebp + 0FFFFD262h ] , 00h                        ;0x004ba6d5 :        80bd62d2ffff00
             jne Label57a61d                                                 ;0x004ba6dc :        0f853bff0b00
             cmp  byte ptr [ ebp + 0FFFFD264h ] , 00h                        ;0x004ba6e2 :        80bd64d2ffff00
             jne Label57a61d                                                 ;0x004ba6e9 :        0f852eff0b00
             cmp  byte ptr [ ebp + 0FFFFD266h ] , 00h                        ;0x004ba6ef :        80bd66d2ffff00
             jne Label57a61d                                                 ;0x004ba6f6 :        0f8521ff0b00
             cmp  byte ptr [ ebp + 0FFFFD260h ] , 00h                        ;0x004ba6fc :        80bd60d2ffff00
             jne Label57a61d                                                 ;0x004ba703 :        0f8514ff0b00
             cmp  byte ptr [ ebp + 0FFFFD25Ah ] , 00h                        ;0x004ba709 :        80bd5ad2ffff00
             jne Label57a61d                                                 ;0x004ba710 :        0f8507ff0b00
             cmp  byte ptr [ ebp + 0FFFFD267h ] , 00h                        ;0x004ba716 :        80bd67d2ffff00
             jne Label57a61d                                                 ;0x004ba71d :        0f85fafe0b00
             cmp  byte ptr [ ebp + 0FFFFD265h ] , 00h                        ;0x004ba723 :        80bd65d2ffff00
             jne Label57a61d                                                 ;0x004ba72a :        0f85edfe0b00

     Label4ba730 ::
             pushd 00h                                                       ;0x004ba730 :        6a00
             lea ebx ,  dword ptr [ ebp + 0FFFFFE64h ]                       ;0x004ba732 :        8d9d64feffff
             call Fun4ba279                                                  ;0x004ba738 :        e83cfbffff
             test eax , eax                                                  ;0x004ba73d :        85c0
             mov  dword ptr [ edi + 20 ] , eax                               ;0x004ba73f :        894714
             jne Label57a629                                                 ;0x004ba742 :        0f85e1fe0b00

     Label4ba748 ::
             test  byte ptr [ edi + 33 ] , 04h                               ;0x004ba748 :        f6472104
             jne Label57a632                                                 ;0x004ba74c :        0f85e0fe0b00

     Label4ba752 ::
             cmp  byte ptr [ ebp + 0FFFFD263h ] , 00h                        ;0x004ba752 :        80bd63d2ffff00
             jne Label488681                                                 ;0x004ba759 :        0f8522dffcff
             mov eax ,  dword ptr [ Data9393ac ]                             ;0x004ba75f :        a1ac939300
             mov  cl , byte ptr [eax]                                        ;0x004ba764 :        8a08
             cmp cl , 02Fh                                                   ;0x004ba766 :        80f92f
             je Label57a6b1                                                  ;0x004ba769 :        0f8442ff0b00
             cmp cl , 030h                                                   ;0x004ba76f :        80f930
             je Label57a6b1                                                  ;0x004ba772 :        0f8439ff0b00

     Label4ba778 ::
             mov  cl , byte ptr [eax]                                        ;0x004ba778 :        8a08
             cmp cl , 03h                                                    ;0x004ba77a :        80f903
             mov eax ,  dword ptr [ Data9370f8 ]                             ;0x004ba77d :        a1f8709300
             je Label4ba78d                                                  ;0x004ba782 :        7409
             cmp cl , 0FEh                                                   ;0x004ba784 :        80f9fe
             jne Label4baeea                                                 ;0x004ba787 :        0f855d070000

     Label4ba78d ::
             mov  ecx , dword ptr [ ebp + 0FFFFD240h ]                       ;0x004ba78d :        8b8d40d2ffff
             xor edx , edx                                                   ;0x004ba793 :        33d2
             cmp  dword ptr [ ecx + 52 ] , edx                               ;0x004ba795 :        395134
             jne Label4baeea                                                 ;0x004ba798 :        0f854c070000
             cmp  dword ptr [ ecx + 80 ] , edx                               ;0x004ba79e :        395150
             jne Label4baeea                                                 ;0x004ba7a1 :        0f8543070000
             mov  ecx , dword ptr [eax]                                      ;0x004ba7a7 :        8b08
             cmp  dword ptr [ ecx + 0DCh ] , edx                             ;0x004ba7a9 :        3991dc000000
             mov  byte ptr [ ebp + 0FFFFD25Eh ] , 01h                        ;0x004ba7af :        c6855ed2ffff01
             jne Label4baeea                                                 ;0x004ba7b6 :        0f852e070000
             jmp Label4baef1                                                 ;0x004ba7bc :        e930070000


4. APPLIES TO:

     VFP 6.0.8167.0
     VFP 6.0.8961.0 (SP5)

     VFP 7.0.0.9262
     VFP 7.0.0.9465 (SP1)

     VFP 8.0.0.2521
     VFP 8.0.0.3117 (SP1)

     VFP 9.0.0.2412
     VFP 9.0.0.3504 (SP1)
     VFP 9.0.0.4611 (SP2)
     VFP 9.0.0.5015 (SP2)
     VFP 9.0.0.5411 (SP2)
     VFP 9.0.0.5721 (SP2)
     VFP 9.0.0.5815 (SP2)
     VFP 9.0.0.6303 (SP2)
     VFP 9.0.0.6602 (SP2)
     VFP 9.0.0.7423 (SP2)

     The bug has been fixed in VFP Advanced.

     IMPORTANT NOTE:
     In vfp9 (and vfp6, vfp7, vfp8), we can delete the destination file before run the COPY TO command, for example,
     IF FILE("test.tmp")
        DELETE FILE ("test.tmp")
     ENDIF
     COPY TO ("test.tmp") DELIMITED
     and then there is no the bug.


5. REFERENCE WEBSITES:

     1, baiyujia.com:
     http://www.baiyujia.com
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix178.asp


6. OTHER:

     For reference only, there is no guarantees.

     Any questions or suggestions, please send me an email at ccb2000@163.com.