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.

    I need the results to be something like this:

    filepath filename

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

    c:\program files\blah

  • 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!

  • 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.

  • 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.

  • 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?

  • 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.

  • 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!

  • 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!

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply