SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 xp_cmshell get Subdirectory name
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ph1long
Starting Member

16 Posts

Posted - 01/09/2013 :  16:03:11  Show Profile  Reply with Quote
Hi,

I can get all of the filenames but am not sure how to also get the subfolder's names. Please help.

truncate table Reports 
go

IF OBJECT_ID('tempdb..#temp') IS NOT NULL
    DROP TABLE #temp

CREATE TABLE #temp(
[files] [nvarchar](50) NULL
--[Warrants] [nvarchar](50) NULL,
--	[Statement] [nvarchar](50) NULL,
--	[Property Tax] [nvarchar](50) NULL
) ON [PRIMARY]
go

INSERT INTO #temp
EXEC xp_cmdshell 'dir D:\Reports /b'

;WITH cte AS
(
	SELECT
		CASE 
			WHEN files LIKE '%stmt.pdf' THEN 'stmt'
			WHEN files LIKE '%tax.pdf' THEN 'tax'
			WHEN files LIKE '%warr.xls' THEN 'warr'
		END
		AS FileType,
		REPLACE(REPLACE(REPLACE(files,'stmt.pdf',''),'tax.pdf',''),'warr.xls','') AS FILENAME,
		files
	FROM
		#temp
)
INSERT INTO Reports
([Statement],[Property Tax],[Warrants], [FileName])
SELECT
	stmt, tax, warr, FileName
FROM
	cte
PIVOT(MAX(files) FOR filetype IN ([stmt],[tax],[warr]))p

Jeff Moden
Aged Yak Warrior

USA
649 Posts

Posted - 01/09/2013 :  19:43:45  Show Profile  Reply with Quote
Add /s to the command.

--Jeff Moden
RBAR is pronounced "ree-bar" and is a "Modenism" for "Row By Agonizing Row".

First step towards the paradigm shift of writing Set Based code:
"Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

When writing schedules, keep the following in mind:
"If you want it real bad, that's the way you'll likely get it."
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000