Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / General / Other SQL Server Topics / February 2007

Tip: Looking for answers? Try searching our database.

Reading a directory

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Utahduck@hotmail.com - 28 Feb 2007 00:40 GMT
I do a lot of file processing and I usually run a little script I copy
and paste to read directory information to see if a new file it there
and then process the file if it is.  So, I decided to wise up and make
a stored procedure to automate a lot of that.

The pivotal step in this is that i run a command that looks like:

CREATE TABLE #DIR (FileName varchar(100))

DECLARE @Cmd varchar(1050)
SET    @Cmd = 'DIR "' + @Path + CASE WHEN RIGHT(@Path, 1) = '\' THEN ''
ELSE '\' END + @WildCard + '"'

INSERT INTO #DIR
EXEC master..xp_CmdShell @Cmd

When I run the stored procedure I get back the files and folders in
there that match the wildcard and all is good!!!!

...Until I try to put that information into a table while calling that
stored procedure:

CREATE TABLE #Files (
    Path varchar(100),
    FileName varchar(100),
    PathAndFileName varchar(150),
    FileDateTime SmallDateTime,
    FileLength int,
    FileType Varchar(10))

INSERT INTO #Files
EXEC sp_GetFileNames @Path = '\\isoft2\ftp\Legacy\Billing\', @Wildcard
= '*.txt'

When I run this I get:

Server: Msg 8164, Level 16, State 1, Procedure sp_GetFileNames, Line
53
An INSERT EXEC statement cannot be nested.

Because I use an INSERT EXEC to with the results from the @Cmd.

Anybody have any ideas how I can get that information into a table?

I did try to just copy the data to c:\temp\dir.txt and then bulk
import it in.  But when it runs the @Cmd to create the file it comes
back with a NULL value and my stored procedure returns two sets of
values... which I can't do.

So, I would appreciate anybody who can help.

Thanks!

-utah
AlterEgo - 28 Feb 2007 01:02 GMT
utah,

You should paste your stored procedure. One thing, how are you getting from
a one column table (#Dir) to a multiple column table (#files) based upon
your insert? You are going to have to do some parsing to get all this info
into multiple columns.

-- Bill

>I do a lot of file processing and I usually run a little script I copy
> and paste to read directory information to see if a new file it there
[quoted text clipped - 50 lines]
>
> -utah
Russ Rose - 28 Feb 2007 05:18 GMT
What I have done in the past is create a global temp table (##Files) and
then in the called procedure(sp_GetFileNames) insert into the global temp
table directly.

>I do a lot of file processing and I usually run a little script I copy
> and paste to read directory information to see if a new file it there
[quoted text clipped - 50 lines]
>
> -utah
Jack Vamvas - 28 Feb 2007 10:16 GMT
Maybe I'm bot understanding your problem correctly ,, but if you did
CREATE TABLE #DIR (FileName varchar(100))

> DECLARE @Cmd varchar(1050)
> SET @Cmd = 'DIR "' + @Path + CASE WHEN RIGHT(@Path, 1) = '\' THEN ''
> ELSE '\' END + @WildCard + '"'
>
> INSERT INTO #DIR
> EXEC master..xp_CmdShell @Cmd

INSERT INTO myTABLE
SELECT filename FROM #DIR

would that not do the job?

Signature

Jack Vamvas
___________________________________
The latest IT jobs   - www.ITjobfeed.com
<a href="http://www.itjobfeed.com">UK IT Jobs</a>

>I do a lot of file processing and I usually run a little script I copy
> and paste to read directory information to see if a new file it there
[quoted text clipped - 50 lines]
>
> -utah
Erland Sommarskog - 28 Feb 2007 22:37 GMT
> INSERT INTO #Files
> EXEC sp_GetFileNames @Path = '\\isoft2\ftp\Legacy\Billing\', @Wildcard
>= '*.txt'

Note that the sp_ prefix is reserved for system procedures, and SQL Server
will first look for these in the master database. Do not use it for your
own code.

> When I run this I get:
>
[quoted text clipped - 5 lines]
>
> Anybody have any ideas how I can get that information into a table?

I have an article on my web site that discusses a couple of alternatives:
http://www.sommarskog.se/share_data.html.

Signature

Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.