4

Hi want to exclude 2 or more files from my batch file.

My batch file executes all SQL files in that folder.

Here is the code:

ECHO %USERNAME% started the batch process at %TIME% >output.txt 

FOR %%G IN (*.sql) DO (
//IF would go here to skip unwanted files
sqlcmd.exe  -S RMSDEV7 -E   -d ChaseDataMedia7 -i "%%G" >>output.txt
)

pause

So, how would i add and if statemant to skip the files in the loop that i don't want to execute?

Willem
  • 9,166
  • 17
  • 68
  • 92
  • Q&D but another option might be to rename both files before your loop and restore them afterwards. – Lieven Keersmaekers Jan 13 '12 at 10:59
  • @Lieven Viable, but i don't want to delete and restore 20 times a day, and want the other developers to just run the batch file. No need to worry about delete and restores. – Willem Jan 13 '12 at 11:09
  • 1
    possible duplicate of [Logical operators ("and", "or") in DOS batch](http://stackoverflow.com/questions/2143187/logical-operators-and-or-in-dos-batch) – Andriy M Jan 13 '12 at 12:07

2 Answers2

4

You can chain IFs;

FOR %%G IN (*.sql) DO (
   if not %%G==foo.sql if not %%G==bar.sql (
      some command "%%G"
   )
)
Alex K.
  • 171,639
  • 30
  • 264
  • 288
2
@echo off
setlocal EnableDelayedExpansion
set unwantedFiles=foo bar baz
for %%g in (*.sql) do (
   set "test=!unwantedFiles:%%~Ng=!"
   if "!test!" == "!unwantedFiles!" (
      echo %%~Ng.sql is not unwanted, process it:
      echo Process with %%g
   )
)

Take a name and copy unwantedFiles by removing current name. If the result is the same as before, this name is NOT in unwantedFiles, so process it...

Aacini
  • 65,180
  • 12
  • 72
  • 108