exceptions in for loop

@echo off
setlocal

(
    echo SET DEFINE OFF;
    echo.

    for %%A in ("*sql") do (
	IF %%A~="fileslist.sql"(
	REM do nothing
	)
	ELSEIF %%A~="MAIN.sql"(
	REM do nothing
	)
	ELSE(
	echo @@%%~A
	)
	)

    echo.
    echo COMMIT;
    echo.
    echo EXIT

)>result.sql

Open in new window


Hi, Experts,

I am trying to write into "results.sql" a list of *.sql within the folder and with some text before and after the list. Which was already done.
But now i have a problem of excluding 2 .sql files within the foler, namely "fileslist.sql" and "MAIN.sql".
After i added the if elseif statement. The batch file didn't work.
Any advise if my syntax or logic is wrong?
csharp_learnerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
There is no "elseif" in batch, you replaced the "~", the = is the assignment operator in batch, not a comparison operator, comparisons are case sensitive by default, and double quotes are needed on both sides of the comparison.
@echo off
setlocal
(
	echo SET DEFINE OFF;
	echo.
	for %%A in ("*.sql") do (
		if /i not "%%~nxA"=="fileslist.sql" (
			if /i not "%%~nxA"=="MAIN.sql" (
				echo @@%%~A
			)
		)
	)
	echo.
	echo COMMIT;
	echo.
	echo EXIT
) >result.sql

Open in new window

0
oBdACommented:
On a side note:  you should move to PowerShell anyway:
$fileList = Get-ChildItem *.sql -Exclude 'fileslist.sql', 'MAIN.sql' | ForEach-Object {"@@$($_.Name)"}
@"
SET DEFINE OFF;

$($fileList -join "`r`n")

COMMIT;

EXIT
"@ | Set-Content -Path .\result.sql

Open in new window

0
Bill PrewIT / Software Engineering ConsultantCommented:
Looks like you have an example of how to nest IF statements to get the desired result.  That works if you only have 1 or 2 files.  Just for options, here is an approach that is more easily maintained and expanded if the files change or increase.  Basically we establish a list of the file names to exclude, and then as we read each file we only echo if the file name is not in the list to exclude.  Questions welcome...

@echo off
setlocal EnableDelayedExpansion

set ExcludeList=fileslist,main

(
    echo SET DEFINE OFF;
    echo.

    for %%A in ("*.sql") do (
        set Exclude=
        for %%B in (%ExcludeList%) (
            if /i "%%~B" EQU "%%~nA" (
                set Exclude=Y
            )
        )
        if not defined Exclude (
            echo @@%%~A
        )
    )

    echo.
    echo COMMIT;
    echo.
    echo EXIT

)>result.txt

Open in new window


»bp
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

csharp_learnerAuthor Commented:
Thank you experts for your comments.
oBdA: Does it mean if there's 10 .sql files to be excluded in the future, the code needs to nest 10 if statements?
Bill Prew: The code didn't generate the result.txt after execution.
0
Bill PrewIT / Software Engineering ConsultantCommented:
Sorry, was missing a "do" in there, try this.

@echo off
setlocal EnableDelayedExpansion

set ExcludeList=fileslist,main

(
    echo SET DEFINE OFF;
    echo.

    for %%A in ("*.sql") do (
        set Exclude=
        for %%B in (%ExcludeList%) do (
            if /i "%%~B" EQU "%%~nA" (
                set Exclude=Y
            )
        )
        if not defined Exclude (
            echo @@%%~A
        )
    )

    echo.
    echo COMMIT;
    echo.
    echo EXIT

)>result.txt

Open in new window


»bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
csharp_learnerAuthor Commented:
Works great
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.