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:


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

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

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


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

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

rem Processing lines in the buffer file

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


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

goto :end

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"


exit /b


::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

exit /b


DEL %fileOutput%


Some handy references I used:

0 reacties:

Post a Comment