標籤:use 自己 需要 rem 問題 序號 his off file
公司需要處理SQL的指令碼問題,每個檔案刪除最後3行,檔案數目較多,本來是別人的活,但是他解決不了,我就幫忙看了下,最後解決完畢,比網上有的速度快得多,主要是理解了for這個命令,會產生序號,而不用自己去單獨產生序號,那樣很麻煩。
for /f "tokens=*" %%d in (‘dir /ad /b‘) do (
cd %%d
for /f "tokens=*" %%f in (‘dir /b *.sql‘) do (
findstr /c:"use paradb;" %%f && goto fir_end
echo use paradb; > temp.temp
echo. >> temp.temp
type "%%f" >> temp.temp
del "%%f"
ren temp.temp "%%f"
:fir_end
rem "end one cir"
)
cd ..
)
:sec
set ERRORLEVEL=0
for /f "tokens=*" %%f in (‘dir /a-d /b *.sql‘) do (
if "%%f" == "histable.sql" (
findstr /c:"use hisdb;" %%f && goto sec_end
echo use hisdb; > temp.temp
) else (
findstr /c:"use paradb;" %%f && goto sec_end
echo use paradb; > temp.temp
)
echo. >> temp.temp
type "%%f" >> temp.temp
del "%%f"
ren temp.temp "%%f"
:sec_end
rem "end one cir"
)
@echo off
call:delline xxx.sql 1
goto EOF
:delline
set file=%1
for /f "tokens=2 delims=:" %%i in (‘find /c /v ".*" %file%‘) do set /a res=%%i-%2
(for /f "tokens=1* delims=:" %%a in (‘findstr /n .* %file%‘) do (
if %%a geq %res% goto MOV //這個地方知道%a,技巧滿分,初學的人不知道,試了好久才知道。
echo.%%b)) > temp.temp
goto EOF
:MOV
del /f /q %file%
ren temp.temp %file%
goto EOF
:EOF
Windows 下指令碼處理