DOS – Batch – Process Files But Skip Selected Files

Objective

We are using ScriptDB to generate SQL files from a live Production system.

Before applying the files to a new system, we need to exclude some of the files and apply batch changes to the remaining files.

So here is what we did:

  1. Issue ScriptDB to generate SQL files for each DB Object
  2. Prepare an exclusion list; listing files we will like to skip
  3. Ran our batch file

Here is what the batch file does

  1. It reads an exception file; that lists the objects to be skipped
  2. If the entry should be skipped it makes a copy of it.
    • The copy file has the same name as the original file with .bak appended
    • The Original file is then removed

And, so what we end up is a folder that has the files we need and the files we want to skipped delineated by the .bak extention

Code


@ECHO OFF

@REM Please make sure to keep the EnableDelayedExpansion
Setlocal EnableDelayedExpansion

IF %1.==. (
    Echo Please specify Source Folder as the first argument
	Goto End
 ) 

set folderSource=%1

set folderBase=%folderSource%
echo FolderBase is !folderBase!

@REM The Full Location of the except text file
set "exceptSourceFolder=."
set "exceptSourceFile=skippedFiles.txt"
set "exceptSourceFileFull=%exceptSourceFolder%\%exceptSourceFile%"

echo Exception Source File is !exceptSourceFileFull!

set /p DUMMY=Hit ENTER to Start Processing Skipped Files

set "BAKEXT=.bak"

echo %exceptSourceFileFull%

for /f %%f in (%exceptSourceFileFull%)  do (

	ECHO Processing Except entry %%f ...
	
	set _filename=%folderBase%\%%f

	ECHO Processing File !_filename!
	
	@REM Get full file name by adding except file name to base folder
	set _fullFileTest=!_filename!
	ECHO "Full Name of File !_fullFileTest!"

	@REM Get Backup file name
	@REM set "fullFileTestBak=%fullFileTest%%BAKEXT%"
	set "_fullFileTestBak=!_filename!%BAKEXT%"
	ECHO "Test File (BAK) !_fullFileTestBak!"


	@REM Copy file to backup file
	if exist !_fullFileTest! (

	  @REM If Backup file alreay exists, please delete	it
	  if exist "!_fullFileTestBak!" (
	  
		echo "Removing backup file !_fullFileTestBak_! ..."
		
		del !_fullFileTestBak!
		
		echo "Removed backup file !_fullFileTestBak!
		
	) 
	
		echo "Copy file !_fullFileTest! as backup file !_fullFileTestBak! ..."
		copy !_fullFileTest! !_fullFileTestBak! 1>NUL
	 
		echo "Del file !_fullFileTest! ..."
		del !_fullFileTest!

	) else ( 
	
			@REM Echo Skipped File not found - !_fullFileTest! 
	)
	
	echo.
	echo.
	
)

set /p DUMMY=Hit ENTER to Exit Processing Skipped Files

Goto End

:End



Explanation

  1. Please keep in mind that you need the “Setlocal EnableDelayedExpansion
  2. And, that instead of using % to access local variables , we use !

 

Folder

Here is the folder tree that ScriptDB generates for us.

FolderExplorer

Sample Exclusion File


Programmability\StoredProcedures\CRM.GetCSRAppointmentsLog.sql
Programmability\StoredProcedures\CRM.GetPaymentsLog.sql

 

Credits

Jeff Mercado

JeffM

 

Listening

Looking for something else, but felt like Leanne Rimes.

Here she sings Reba McEntire’s The Greatest Man I Never Knew.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s