我在福州一家公司主要从事基于DELPHI+SQL的管理软件测试工作,开发人员提交测试的SQL脚本出现重复创建表、视图、过程是开发人员经常犯的错误之一,脚本很庞大时用普通的查看方法肯定是看不出来的,有时甚至一段重复的过程出现3-4次。于是花了些时间写了个批处理希望与大家交流。
@echo off
:: Code by Jarkun 2009-6-5 CMD@XP
title Search
setlocal enabledelayedexpansion
:star
cls
color 0a
echo.
echo - 查找重复脚本( 按"q"退出 ) -
echo (把文本直接拖入窗口)
echo.
set num=0
set/p filepath= 请输入文本地址:
if /i "!filepath!"=="q" exit
if /i "!filepath!"=="" goto star
echo.
for /f "tokens=3" %%i in ('findstr /ir "CREATE.*PROCEDURE" %filepath%') do (
for /f "tokens=1* delims=( " %%a in ("%%i") do (
set /a %%a+=1
set str=%%a --- 重复 !%%a! 次
if "!%%a!" GTR "1" if not "!str!"=="" echo !str! &set /a num+=1
)
)
for /f "tokens=3" %%i in ('findstr /ir "CREATE.*VIEW" %filepath%') do (
for /f "tokens=1* delims=( " %%a in ("%%i") do (
set /a %%a+=1
set str=%%a --- 重复 !%%a! 次
if "!%%a!" GTR "1" if not "!str!"=="" echo !str! &set /a num+=1
)
)
for /f "tokens=3" %%i in ('findstr /ir "CREATE.*TABLE" %filepath%') do (
for /f "tokens=1* delims=( " %%a in ("%%i") do (
set /a %%a+=1
set str=%%a --- 重复 !%%a! 次
if "!%%a!" GTR "1" if not "!str!"=="" echo !str! &set /a num+=1
)
)
echo.
echo 结果 !num! [ 按"o"输出到文本;按"q"退出! ]
:cho
set/p shr= 请选择:
if /i "!shr!"=="q" exit
if /i "!shr!"=="o" goto out
IF ERRORLEVEL 0 goto cho
ut
set num2=0
set num=0
echo ----------------------------------------------- >>分析结果.txt
echo ★分析结果 >>分析结果.txt
echo ----------------------------------------------- >>分析结果.txt
for /f "tokens=3" %%i in ('findstr /ir "CREATE.*PROCEDURE" %filepath%') do echo %%i >>SearchTemp
for /f "tokens=3" %%i in ('findstr /ir "CREATE.*VIEW" %filepath%') do echo %%i >>SearchTemp
for /f "tokens=3" %%i in ('findstr /ir "CREATE.*TABLE" %filepath%') do echo %%i >>SearchTemp
for /f "tokens=1* delims=( " %%a in ('sort SearchTemp') do (
set str= --- 重复 !%%a! 次
if not defined _%%a (
echo %%a >>分析结果.txt
set /a num2+=1
if "!%%a!" GTR "1" if not "!str!"=="" echo !str! >>分析结果.txt &set /a num+=1
)
set "_%%a=%%a"
)
echo. >>分析结果.txt
echo --------------------------------------- >>分析结果.txt
echo 合计:!num2! 重复:!num! >>分析结果.txt
echo. >>分析结果.txt
del SearchTemp
goto :eof