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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 how to list all the file names in a particular directory in Stored Procedure.

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2003-07-14 : 07:23:27
john writes "hi,

Can anyone help me out !!

how to list all the file names in a particular directory in Stored Procedure.

I need to do this in Stored Procedure ...Pls provide me some solution...

hope to hear from u..

Thanks in advance
John"

Andraax
Aged Yak Warrior

790 Posts

Posted - 2003-07-14 : 07:35:55
Hi John!

You can always use xp_cmdshell if you have the appropriate permissions:

insert #res
exec xp_cmdhell 'dir c:\bla'



Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-07-14 : 09:15:51
The following interogates a directory, stores the results in a work table, then parse the work table in to a table the you can easily apply SQL to:

Good Luck



--DDL

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Ledger_Folder_Parsed]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Ledger_Folder_Parsed]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ledger_folder]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[ledger_folder]
GO

CREATE TABLE [dbo].[Ledger_Folder_Parsed] (
[Create_Time] [datetime] NULL ,
[File_Size] [int] NULL ,
[File_Name] [varchar] (255) NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[ledger_folder] (
[dir_output] [varchar] (255) NULL
) ON [PRIMARY]
GO

-- In procedure code

Delete From Ledger_Folder

Insert Into Ledger_Folder exec master..xp_cmdshell 'Dir d:\Data\Tax\SmartStreamExtracts\*.*'

SELECT @Result_Count = @@ROWCOUNT, @error_out = @@error

If @Error_Out <> 0
BEGIN
Select @Error_Loc = 5
Select @Error_Type = 50001
GOTO Load_Ledger_Init_sp_Error
END

Delete From Ledger_Folder_Parsed

SELECT @Result_Count = @@ROWCOUNT, @error_out = @@error

If @Error_Out <> 0
BEGIN
Select @Error_Loc = 6
Select @Error_Type = 50001
GOTO Load_Ledger_Init_sp_Error
END

Insert Into Ledger_Folder_Parsed (Create_Time, File_Size, File_Name )
Select Convert(datetime,Substring(dir_output,1,8)
+ ' '
+ (Substring(dir_output,11,5)
+ Case When Substring(dir_output,16,1) = 'a' Then ' AM' Else ' PM' End)) As Create_Time
, Convert(Int,LTrim(RTrim(Replace(Substring(dir_output,17,22),',','')))) As File_Size
, Substring(dir_output,40,(Len(dir_output)-39)) As File_Name
From Ledger_Folder
Where Substring(dir_output,1,1) <> ' '
And (Substring(dir_output,1,1) <> ' '
And Substring(dir_output,25,5) <> '<DIR>')




Brett

8-)
Go to Top of Page
   

- Advertisement -