1. 程式人生 > >使用批處理實現解析文字資料檔案寫入資料庫表

使用批處理實現解析文字資料檔案寫入資料庫表

本文例項講述通過批處理解析文字資料檔案寫入資料庫表的實現方法。分享給大家供大家參考。具體實現方法如下:

@echo off & setlocal enabledelayedexpansion
:param
    set data_path=
    set /p data_path=請輸入卡交易檔案存放目錄:
    @echo 你輸入的卡交易檔案存放目錄為:"%data_path%" 
    call :stringlenth "%data_path%" num
    ::@echo 字串長度為:%num% 
    ::判斷是否輸入了檔案存放路徑,否則提示重新輸入
    if
%num% gtr 0 ( goto upload ) else goto param :stringLenth ::字串長度計運算元程式 set thestring=%~1 ::@echo 字串 %thestring% ::引數%1 為字串"%str%",%~1則去掉"%str%"的雙引號。 if not defined theString ( ::設定如果字串為空,長度為0 set Return=0 set %2=0 goto :eof ) set Return=0 :stringLenth_continue set
/a Return+=1 set thestring=%thestring:~0,-1% ::偏移量為 1,擷取倒數第1位前的所有字元 if defined thestring goto stringLenth_continue ::引數%2 為返回變數 num的名稱,不能含空格或特殊字元 if not "%2"=="" set %2=%Return% goto :eof :upload ::讀取config.ini配置檔案 for /f "tokens=1,2 delims==" %%b in (%~dp0conf\conf.ini) do
( if "%%b"=="user" set user=%%c if "%%b"=="pass" set pass=%%c if "%%b"=="dbsid" set dbsid=%%c ) ::迴圈處理卡交易檔案 for /r %data_path% %%a in (R92EXTCI*) do ( set houzui=%%~xa ::備份資料檔案,先判斷時候有後綴,由於sqlldr預設資料檔案預設字尾為.dat if not defined houzui ( copy %%a %%a.dat && move %%a %~dp0databak\prda\%%~na ::獲取第一行的資料,用於後續擷取一些資料 set /p firstline=<%%a.dat set datafile=%%a.dat ) else ( set /p firstline=<%%a set datafile=%%a ) ::頭記錄編號1 set headno1=!firstline:~28,16! ::頭記錄編號2 set headno2=!firstline:~54,11! ::不帶字尾檔名 set filename=%%~na ::獲取應該匯入總條數 for /f "delims=" %%i in (!datafile!) do set lastline=%%i set totalnum=!lastline:~60,3! ::實現動態把控制檔案的headno1val、headno2val、filenameval替換為實際值 REM for /f "delims=" %%i in ('type %~dp0ctl\prda\garance_prda_sqlldr_sample.ctl') do ( REM set str=%%i REM 變數巢狀替換命令 REM call, set str=%%str:headno1val=!headno1!%% REM call, set str=%%str:headno2val=!headno2!%% REM call, set str=%%str:filenameval=!filename!%% REM echo !str! REM echo !str!>> %~dp0ctl\prda\garance_prda_sqlldr.ctl REM ) ::相關資料插入資料庫表 sqlplus !user!/!pass!@!dbsid! @%~dp0sql\prda\upload_pada_head.sql !filename! !headno1! !headno2! !totalnum! sqlldr userid=!user!/!pass!@!dbsid! control=%~dp0ctl\prda\garance_prda_sqlldr.ctl data=!datafile! log=%~dp0log\garance_prda.log bad=%~dp0bad\garance_prda.bad ::開始進行檢查 if exist %~dp0data_temp\prda\checkexist.txt del %~dp0data_temp\prda\checkexist.txt %~dp0bin\sqluldr264.exe user=!user!/!pass!@!dbsid! sql=%~dp0sql\prda\garance_prda_checkexist.sql file=%~dp0data_temp\prda\checkexist.txt head=no>nul for /f %%n in (%~dp0data_temp\prda\checkexist.txt) do ( set isexist=%%n ) ::計算實際匯入筆數 if exist %~dp0data_temp\prda\checknum.txt del %~dp0data_temp\prda\checknum.txt %~dp0bin\sqluldr264.exe user=!user!/!pass!@!dbsid! sql=%~dp0sql\prda\garance_prda_checknum.sql file=%~dp0data_temp\prda\checknum.txt head=no>nul for /f %%n in (%~dp0data_temp\prda\checknum.txt) do ( set checknum=%%n ) if exist %~dp0data_temp\prda\checkexist.txt ( if exist %~dp0data_temp\prda\checknum.txt ( if !isexist! gtr 0 ( echo "=================檔案資訊已經存在,請核對後再重新匯入=======================" ) else ( sqlplus !user!/!pass!@!dbsid! @%~dp0sql\prda\appendprda.sql echo "=================ATM檔案%%a已經匯入=======================" if !checknum! neq !totalnum! ( echo "=================應匯入!totalnum!,實際匯入!checknum!,請核對!!=======================" )else ( echo "=================成功匯入筆數!checknum!=======================" ) ) ) else ( echo =================檢查成功匯入筆數時出錯======================= ) ) else ( echo =================檢查檔案資訊是否已存在時出錯======================= ) ::等待一會,後續進行下個檔案的解析匯入 ping 127.0.0.1 -n 8 >nul ) pause