Sunday, October 21, 2007

Day playing with command Line

Today I have to modify and create batch file which want to import list of text files with CVS format(deliminated by comma) to SQL Server 2000. The list of files are same filenames with different date and time eg.abcde_21061977_123600.txt,abcde_21061977_123611.txt. I need to create a new file which has a list of files name starting with "abcde". I also created the log file to trace the batch file is sucefully copied the data to the SQL server.

The source code of batch file is as follow.

@echo off
REM author By VICTORIA WIN, Oct 23 2007

REM Setting configuable variables
REM===========================================================

SET BCP_Datafile_path=D:\xxx\xxx\xxx
SET BCP_Batchfile_path=D:\xxx\xxx
SET BCP_Achrivefile_path=D:\xxx\xxx\xxx
SET BCP_Logfile_path=D:\xxx\xxx\xxxLogfile.txt

REM===========================================================

REM Set fixed varibles for username, password and server
SET BCP_SERVER=abc
SET BCP_USERNAME=abc
SET BCP_PWD=abc
SET BCP_TABLE_NAME=abc..xxtablename

REM===========================================================

Echo Running BCP Batch file >> %BCP_Logfile_path%
Date /t >>%BCP_Logfile_path%
Time /t>>%BCP_Logfile_path%
Echo ==========================================================>>%BCP_Logfile_path%
REM change the directory using file path
CD %BCP_Batchfile_path%

REM produce the list of files starting with indiaBoData
DIR %BCP_Datafile_path%\abcde*.txt /o:d /b>fileslist.txt
Echo BCP Export filename.txt Finished >>%BCP_Logfile_path%

REM Import the data from filename.txt to the SQL FSICopes DB
For /f "tokens=1" %%g in (%BCP_Batchfile_path%\fileslist.txt) do (

bcp %BCP_TABLE_NAME% in %BCP_Datafile_path%\%%g -c -t "|" -S%BCP_SERVER% -U%BCP_USERNAME% -P%BCP_PWD%

IF EXIST %BCP_Datafile_path%\%%g (move %BCP_Datafile_path%\%%g %BCP_Achrivefile_path%) ELSE (goto :errMsg)

REM Run SP_MF_Move_India_Data which import the data to MF_India_Data_Test from MF_Move_India_Data
osql -S%BCP_SERVER% -U%BCP_USERNAME% -P%BCP_PWD% -q "exit(exec abcDB..sp_xxxstoredProcedureName)"

Echo BCP Export %%g Finished >>%BCP_Logfile_path%
)

Echo IndiaBCP File Finished.>>%BCP_Logfile_path%

goto setvariable

:errMsg
Echo Error %ERRORLEVEL% Raised!!>>%BCP_Logfile_path%

:setvariable
REM===========================================================
REM set the variables
SET BCP_SERVER=
SET BCP_USERNAME=
SET BCP_PWD=
SET BCP_Datafile_path=
SET BCP_Batchfile_path=
SET BCP_Achrivefile_path=
REM===========================================================
exit
@echo on


I just only shared and jotted my command line experience with BCP, SQL import and export utility from command prompt to SQL Server in this blog.
Only a few know..NOT MUCH..I beg you Plz not ask other command lines... HEeeeee hee :D :P.

No comments:

ကၽြန္မ သိပ္ျပီး ဘေလာ့ မေရးတတ္ပါဘူး။ ေရးလည္း မေရးဖူးပါဘူး။ အေၾကာင္းအရာေတြက သိပ္မေကာင္းေပ့မယ့္ အားလုံးကုိ ဗဟုသုတရေစမဲ့ အေၾကာင္းအရာေလးေတြ ျဖစ္ေအာင္ ေရးသားသြားဖုိ႕ စိတ္ကူး၊ ကုိယ့္ရဲ႕ေန႕ေလးေတြကုိ မွတ္တမ္းလည္း တင္ခ်င္တဲ့ စိတ္ကူးေလးလည္းပါပါတယ္။
 

blogger templates | Make Money Online