if then (else if) (else if) else

April 5, 2012 at 03:14:02
Specs: Windows 7
Hi,

I want to writing a script to copy 4 set file subsequently


the perfect result should be 1-->2--> 3 ---> 4 --> 1
but I don't know why the script only can handle set 3 data
and the dos prompt will display

'ELSE' is not recognized as an internal or external command,
operable program or batch file.

is it my syntax error?
Please help

The logic as follow
----------------------------------------------------------------
if set 1 not existing then
copy set 1 data
delete set 2 data

else
if set 2 not existing then
copy set 2 data
delete set 2 data

else
if set 3 not existing then
copy set 3 data
delete set 4 data

else
if set 4 not existing then
copy set 4 data

else
delete set 1 data


--------------------------------------------------------

Here is my source code , the program can not execute if don't rem the marked statement

---------
@echo on
IF NOT EXIST "d:\aaa\1.txt" (
copy d:\bbb\1.txt d:\aaa\1.txt
del d:\aaa\2.txt

) ELSE (
IF NOT EXIST "d:\aaa\2.txt" (
copy d:\bbb\2.txt d:\aaa\2.txt
del d:\aaa\3.txt

) ELSE (
IF NOT EXIST "d:\aaa\3.txt" (
copy d:\bbb\3.txt d:\aaa\3.txt
del d:\aaa\4.txt


) ELSE (
IF NOT EXIST "d:\aaa\4.txt" (
copy d:\bbb\3.txt d:\aaa\4.txt

)
ELSE (
del d:\aaa\1.txt
))


See More: if then (else if) (else if) else

Report •

#1
April 5, 2012 at 04:10:26
You have more open parens "(" than you do close parens: ")". After indenting so close parens is at the same "level" as the originating open parens, I found I needed to add the final close:

@echo on
IF NOT EXIST "d:\aaa\1.txt" (
    copy d:\bbb\1.txt d:\aaa\1.txt
    del d:\aaa\2.txt
) ELSE (
    IF NOT EXIST "d:\aaa\2.txt" (
        copy d:\bbb\2.txt d:\aaa\2.txt
        del d:\aaa\3.txt
    ) ELSE (
        IF NOT EXIST "d:\aaa\3.txt" (
            copy d:\bbb\3.txt d:\aaa\3.txt
            del d:\aaa\4.txt
        ) ELSE (
            IF NOT EXIST "d:\aaa\4.txt" (
            copy d:\bbb\3.txt d:\aaa\4.txt
            )	
        )
    )
)

^ --- Added that one

When your only tool is a hammer, every problem looks like a nail.


Report •

#2
April 5, 2012 at 04:55:31
Oh... problem solve...

Thank you very much

it is quite difficult for the dos script if no any compiler


Report •

#3
April 5, 2012 at 15:08:30
You can also make if/else structures in batch without the deep pattern. Personally I find these easier to keep track of:

IF NOT EXIST "d:\aaa\1.txt" (
    copy d:\bbb\1.txt d:\aaa\1.txt
    del d:\aaa\2.txt
) ELSE IF NOT EXIST "d:\aaa\2.txt" (
    copy d:\bbb\2.txt d:\aaa\2.txt
    del d:\aaa\3.txt
) ELSE IF NOT EXIST "d:\aaa\3.txt" (
    copy d:\bbb\3.txt d:\aaa\3.txt
    del d:\aaa\4.txt
) ELSE IF NOT EXIST "d:\aaa\4.txt" (
    copy d:\bbb\3.txt d:\aaa\4.txt
)


Report •

Related Solutions

#4
April 6, 2012 at 04:34:07
Nice construct Judago! I think I'll adapt my style to match.

When your only tool is a hammer, every problem looks like a nail.


Report •

Ask Question