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


Scripting help - Need a query to find filenames and filepaths under a certain directory


Scripting help - Need a query to find filenames and filepaths under a certain directory

Author
Message
tan110
tan110
Old Hand
Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)

Group: General Forum Members
Points: 342 Visits: 712
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
Lowell
Lowell
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29064 Visits: 39984
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

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

john.arnott
john.arnott
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2036 Visits: 3059
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.
tan110
tan110
Old Hand
Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)

Group: General Forum Members
Points: 342 Visits: 712
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.
john.arnott
john.arnott
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2036 Visits: 3059
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?
tan110
tan110
Old Hand
Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)

Group: General Forum Members
Points: 342 Visits: 712
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.
Lowell
Lowell
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29064 Visits: 39984
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

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Lowell
Lowell
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29064 Visits: 39984
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

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

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