• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 36
  • Last Modified:

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?
0
csharp_learner
Asked:
csharp_learner
  • 2
  • 2
  • 2
1 Solution
 
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 PrewCommented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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 PrewCommented:
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
 
csharp_learnerAuthor Commented:
Works great
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now