Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8798059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:59:30+00:00 2026-06-13T23:59:30+00:00

Is there a way to find out if SQL Server databases are attached from

  • 0

Is there a way to find out if SQL Server databases are attached from a directory with many subfolders apart from going to properties of each .mdf and checking if it is attached?

Is there a script that can check folders/subfolders an write to file to accomplish this?

THanks all.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:59:31+00:00Added an answer on June 13, 2026 at 11:59 pm

    When you need to read the file system from T-SQL, I find that CLRs have the most flexibility. They give you full access to the system.io namespace as well as support for table valued functions.

    Have a look at the SQL Server CLR IO Utility. There is a full solution here which enables filesystem functionality that is not easily available with the frowned upon xp_cmdshell.

    The table valued function, SQLIO_fnGetFiles, can be used to join to the sys.master_files table and return files which are no longer attached to a database. In this example, the CLR is deployed to a database named CLRS:

    USE master
    
    SELECT fs.PATH FileSystemPath
         , FileIsAttached = CASE WHEN d.name IS NULL THEN 'YES' ELSE 'NO' END
         , d.name DatabaseName
         , mf.name FileLogicalName
         , mf.physical_name DatabaseFilePath
         , FileType = CASE mf.type_desc WHEN 'ROWS' THEN 'DATA' ELSE mf.type_desc END
    
    FROM CLRS.dbo.SQLIO_fnGetFiles('L:\MSSQL\','*df',1) fs
    LEFT OUTER JOIN sys.master_files mf ON fs.PATH = mf.physical_name
    LEFT OUTER JOIN sys.databases d ON d.database_id = mf.database_id
    

    There are also other methods (powershell, .net, ssis,…) to accomplish this. My experience is that once you deploy the IO Utility solution, you’ll find many other time saving uses for it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In SQL Server 2005, is there a way for a trigger to find out
Is there a way to find out the size of a SQL Server database
Is there any easy way to find out what T-SQL is being generated by
Is there a SQL way to find out which columns in my schema are
Is there another way to find out the status of services on AppEngine other
Is there a way to find out which Javascript function is fired by click
Is there a way to find out programmatically what TestCategory is running in a
Is there any way to find out or store which app is accessing an
Is there a way to find out where a Python object was instantiated the
Is there a way to find out if a light is enabled in GLSL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.