Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Scripting help - Need a query to find filenames and filepaths under a certain directory Expand / Collapse
Author
Message
Posted Friday, October 23, 2009 3:47 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, October 16, 2014 10:08 PM
Points: 148, Visits: 642
Scripting help - Need a query to find filenames and filepaths under a certain directory.

I need the results to be something like this:


filepath filename
c:\program files\blah\blah helpme.txt
c:\program files\blah
Post #808186
Posted Friday, October 23, 2009 4:33 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:47 AM
Points: 12,905, Visits: 32,168
tan110 (10/23/2009)
Scripting help - Need a query to find filenames and filepaths under a certain directory.

I need the results to be something like this:


filepath filename
c:\program files\blah\blah helpme.txt
c:\program files\blah

not enough information.
do you have a table with both file and filepath? do you want to use something like xp_cmdshell to search the servers harddrive? what have you tried so far?


Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #808200
Posted Saturday, October 24, 2009 3:48 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, August 28, 2014 8:53 PM
Points: 1,388, Visits: 3,039
Another bit of info that will help you get a useful response would be why you need the file list to begin with. The first thing that popped into my head on seeing your question was that you could use a ForEach loop in SSIS. Not that SSIS is your solution, but that a narrower definition of your problem will get you closer to a workable answer.
Post #808340
Posted Tuesday, October 27, 2009 12:40 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, October 16, 2014 10:08 PM
Points: 148, Visits: 642
Heres the table:

CREATE TABLE [dbo].[tblSaveFilePathAndName](
[filePath] [varchar](1000) NULL,
[fileName] [varchar](200) NULL
)

Purpose: We have thousands of files that we need to sort through and find, and quickly identify where those files are located.
Post #809522
Posted Tuesday, October 27, 2009 2:03 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, August 28, 2014 8:53 PM
Points: 1,388, Visits: 3,039
I don't understand how this is a problem. Is there more to what you need than just a list of filepaths where a filename is found? What do you need beyond a simple query for the two columns where the filename matches your target?
Post #809568
Posted Tuesday, October 27, 2009 2:09 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, October 16, 2014 10:08 PM
Points: 148, Visits: 642
Basically, all I want to do is insert all the files and filepaths into the table for a given path, For instance C:\program files\. This could be thousands of files.
Post #809572
Posted Tuesday, October 27, 2009 3:42 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:47 AM
Points: 12,905, Visits: 32,168
here's something to get you started. Assuming you've enabled xp_cmdshell, and the SQL service is using credentials that can see the program files folder, this command would give you a detailed list of all files and sub-folders.

go to the command prompt and do dir /? to see other options, like if you want the /b for brief results with no sizes/dates. review the results, it should be obvious how you can use substring() to get the data elements out of it.

note this command took more than 35 minutes to run on my laptops 5400rpm hard drive to return 324,570 rows of results. it expanded my tempdb by a little more than 30 meg.

you need to make sure this is a one time thing, and not something you do regularly.

CREATE TABLE #Results (txtResult varchar(2000) NULL)
INSERT INTO #Results (txtResult)
EXEC master.dbo.xp_cmdshell 'DIR "C:\PROGRAM FILES\*.*" /s'
SELECT * FROM #Results



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #809626
Posted Tuesday, October 27, 2009 4:07 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:47 AM
Points: 12,905, Visits: 32,168
this was an order of magnitude faster: 8 minutes to create a text file to disk, then 20 seconds to bulk insert into a table

using the /b command, it was only 1 minute 11 secs to create the file to disk.
CREATE TABLE #Results (txtResult varchar(2000) NULL)
INSERT INTO #Results (txtResult)
EXEC master.dbo.xp_cmdshell 'DIR "C:\PROGRAM FILES\*.*" /b s >C:\contents.txt'

BULK INSERT #Results FROM 'C:\contents.txt'
WITH (
DATAFILETYPE = 'char',
FIELDTERMINATOR = ',',
ROWTERMINATOR = '',
FIRSTROW = 1
)

SELECT * FROM #Results



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #809643
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse