Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
dr223
Constraint Violating Yak Guru
444 Posts |
Posted - 2011-07-19 : 07:45:20
|
| Hi, I have a folder (G:\Test) which has 3 csv files (Test1.csv, Test2.csv, Test3.csv) on the server. On my database (GTech) I have a table called Tbl.Filename, it has two fields FilenameID and Filename. FilenameID is the primary key and also auto increment. Now I want to be able to read the contents of the 3 csv files in the folder Test and copy the filenames to Tbl.Filaname. So that my table opens us FilenameID Filename1 Test12 Test23 Test3Any ideas? Thanks |
|
|
dr223
Constraint Violating Yak Guru
444 Posts |
Posted - 2011-07-19 : 08:27:31
|
| Ok, this worked for me...DECLARE @cmd nvarchar(200)SET @cmd = 'dir /b G:\Test\*.csv'INSERT INTO dbo.TblFilenameList (Filename)EXEC xp_cmdshell @cmdSELECT * FROM dbo.TblFilenameListResults ID Filename1 Test1.csv2 Test2.csv3 Test3.csv4 NULLNow, I want to update the code to check if the filename exists in Tblfilenamelist if so,then ignore it and copy the next filename...My G:\Test has an extra file Test4.csv...Running the same script gives the result..ID Filename1 Test1.csv2 Test2.csv3 Test3.csv4 Test4.csv5 NULL6 Test1.csv7 Test2.csv8 Test3.csv9 Test4.csv10 NULLI wanted the result to be; ID Filename1 Test1.csv2 Test2.csv3 Test3.csv4 Test4.csv5 NULLN/B - TblFilenamelist has duplicates tooAny ideas .. many thanks |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2011-07-19 : 08:57:24
|
| DECLARE @cmd nvarchar(200)SET @cmd = 'dir /b G:\Test\*.csv'INSERT INTO dbo.TblFilenameList (Filename)EXEC xp_cmdshell @cmdSELECT distinct row_number() over(order by FileName) as ID, FileName FROM dbo.TblFilenameListMadhivananFailing to plan is Planning to fail |
 |
|
|
dr223
Constraint Violating Yak Guru
444 Posts |
Posted - 2011-07-19 : 09:07:07
|
| It didn't work!!Results, similar ID Filename1 Test1.csv2 Test2.csv3 Test3.csv4 Test4.csv5 NULL6 Test1.csv7 Test2.csv8 Test3.csv9 Test4.csv10 NULLI want to append tblFilename, so for the above example the table should ONLY have 4 recordsID Filename1 Test1.csv2 Test2.csv3 Test3.csv4 Test4.csvNo matter how many times I run the script, the table should have the same records until when a new file is added in G:\Test, then it will be appended. For example, I add another file Test5.csv.. Result will change to; ID Filename1 Test1.csv2 Test2.csv3 Test3.csv4 Test4.csv5 Test5.csvPlease ignore the select statement it is NOT necessary, my objective is to append tblFilenamelist...Any help please |
 |
|
|
dr223
Constraint Violating Yak Guru
444 Posts |
Posted - 2011-07-19 : 10:07:59
|
| Tried this code and the output result is fine displayed on the screen, however, Tblfilenamelist is not appended with new files.. WhyDECLARE @cmd nvarchar(200)SET @cmd = 'dir /b G:\Test\*.csv'INSERT INTO dbo.TblFilenameList (Filename)SELECT Filename from dbo.TblFilenameList WHERE not exists (select filename from dbo.TblFilenameList)EXEC xp_cmdshell @cmdMany thanks |
 |
|
|
|
|
|
|
|