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 2005 Forums
 Transact-SQL (2005)
 xp_cmshell get Subdirectory name

Author  Topic 

ph1long
Starting Member

16 Posts

Posted - 2013-01-09 : 16:03:11
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

652 Posts

Posted - 2013-01-09 : 19:43:45
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
   

- Advertisement -