Friday, August 31, 2012

Batch file that loops through files

This week I had to create a batch file that loops through folders and files. Dependent on the path, I needed to create a new.SQL file and append the file names to the text file.

This is the script:

:BEGIN

REM Creating CreateStatements Files...

@echo off

rem This script makes a CreateStatementsFiles.
rem Version: 3
rem Last script update on: 2012-08-30 by Joshua Moesa

:SET_ENVIRONMENT_VARIABLES
rem Setting environment variables
set rootPathBatchFile=C:\DBSCRIPTS\
set scriptRootPath=%rootPathBatchFile%scripts\
set fileOutput=%rootPathBatchFile%output.txt
set fileOutputSub=%rootPathBatchFile%outputSubRoutine.txt

:INIT
rem Cleaning-up
::Cleanup all mk_-files
FOR /R %rootPathBatchFile% %%G IN (mk_*.sql) DO (
DEL %%G
)

:CREATE_BUFFER

rem Printing all SQL filenames to buffer file
FOR /R %rootPathBatchFile% %%G IN (cr_*.sql) DO echo %%G >> %fileOutput%

::DEBUG
::echo %%~pG >> %fileOutput%

:PROCESS_BUFFER_FILE
rem Processing lines in the buffer file

FOR /F "tokens=*" %%A in (%fileOutput%) do (
call :process %%A
)

goto CLEANUP

:process
::echo %1
set fullFileName=%1
call :parse "%fullFileName%"

goto :end

:parse
setlocal
set list=%1
set list=%list:"=%
FOR /f "tokens=4,5,6 delims=\" %%a IN ("%list%") DO (
  if not "%%a" == "" call :sub %%a %%b %%c
  if not "%%b" == "" call :parse "%%b %%b %%c"
)

endlocal

exit /b

:sub
setlocal

::DEBUG
::echo In subroutine
::echo %1 %2 %3 >> %fileOutputSub%
::echo %scriptRootPath%%1\%2\makefile.txt >> %fileOutputSub%

if not exist %scriptRootPath%%1\%2\mk_%2.sql (
 echo prompt create %2 > %scriptRootPath%%1\%2\mk_%2.sql
)

echo @@%3 >> %scriptRootPath%%1\%2\mk_%2.sql

endlocal
exit /b


:CLEANUP

DEL %fileOutput%



:end


Some handy references I used:

0 reacties:

Post a Comment