SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


TSQL-Get Windows Folders Files in table with file parameters


TSQL-Get Windows Folders Files in table with file parameters

Author
Message
khurramsaifi
khurramsaifi
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 4
Comments posted to this topic are about the item TSQL-Get Windows Folders Files in table with file parameters
JON RICE
JON RICE
SSC-Enthusiastic
SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)

Group: General Forum Members
Points: 111 Visits: 35
Getting error: Msg 197, Level 15, State 1, Line 19
EXECUTE cannot be used as a source when inserting into a table variable.
Msg 170, Level 15, State 1, Line 27
Line 27: Incorrect syntax near 'try'.
Msg 170, Level 15, State 1, Line 36
Line 36: Incorrect syntax near 'try'.
Msg 156, Level 15, State 1, Line 40
Incorrect syntax near the keyword 'END'.
WILLIAM MITCHELL
WILLIAM MITCHELL
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2128 Visits: 3062
On the first attempt I received the message:

SQL Server blocked access to procedure 'sys.xp_cmdshell' of component 'xp_cmdshell' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'xp_cmdshell' by using sp_configure. For more information about enabling 'xp_cmdshell', see "Surface Area Configuration" in SQL Server Books Online.

I turned that feature on & it worked fine.
Michael Newell
Michael Newell
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2112 Visits: 708
This is a very useful script. However, there is really no need to use a cursor here. The code can be simplified just by deleting rows which do not contain a date within the first 10 characters and then processing the remaining set.


DECLARE @FilePath varchar(200);

SET @FilePath = 'dir C:\temp\*.pdf /-C '

--EXEC master.dbo.xp_cmdshell 'dir /?'
--EXEC master.dbo.xp_cmdshell 'dir c:\*.txt /-C '

DECLARE @Files TABLE (FileName varchar(200), FileSize varchar (20), DateCreated datetime);
DECLARE @Dir TABLE (FileInfo varchar(1000));

INSERT INTO @Dir
EXEC master.dbo.xp_cmdshell @FilePath;

DELETE FROM @Dir WHERE ISDATE(SUBSTRING(FileInfo, 1, 21)) = 0;

INSERT INTO @Files (DateCreated, FileSize, FileName)
SELECT
DateCreated = CONVERT(datetime, SUBSTRING(FileInfo, 1, 21)),
FileSize = SUBSTRING(LTRIM(SUBSTRING(FileInfo, 21, LEN(FileInfo))), 0,
CHARINDEX(' ', LTRIM(SUBSTRING(FileInfo, 21, LEN(FileInfo))))),
FileName = SUBSTRING(LTRIM(SUBSTRING(FileInfo, 21, LEN(FileInfo))),
CHARINDEX(' ', LTRIM(SUBSTRING(FileInfo, 21, LEN(FileInfo)))),
LEN(LTRIM(SUBSTRING(FileInfo, 21, LEN(FileInfo)))))
FROM @Dir;

SELECT * FROM @Files;

Jeffery Williams
Jeffery Williams
SSC-Addicted
SSC-Addicted (423 reputation)SSC-Addicted (423 reputation)SSC-Addicted (423 reputation)SSC-Addicted (423 reputation)SSC-Addicted (423 reputation)SSC-Addicted (423 reputation)SSC-Addicted (423 reputation)SSC-Addicted (423 reputation)

Group: General Forum Members
Points: 423 Visits: 913
I would not use a CURSOR here, well anywhere really.... What I would do if you needed to iterate through a logic then I would use a TABLE VARIABLE.

There is however no need for iteration as stated here.

Good little script however.

<hr noshade size='1' width='250' color='#BBC8E5'>


Regards,

Jeffery Williams
http://www.linkedin.com/in/jwilliamsoh
Iwas Bornready
Iwas Bornready
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13860 Visits: 885
Thanks for the script.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search