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
 SQL Server Administration (2005)
 Query Instance folder

Author  Topic 

obsoletedude
Starting Member

31 Posts

Posted - 2008-06-25 : 14:59:19
Is there a script that can determine the folder location for a particular instance. I know that this can be done simply by looking at the properties of the instance, but I am trying to automate a procedure, and need to be able to query the folder path to a user specified instance. Any ideas?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-25 : 15:00:56
Yes, which folder path are you interested? For backups, data files, or log files?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tfountain
Constraint Violating Yak Guru

491 Posts

Posted - 2008-06-25 : 16:44:03
Assuming you mean data and log files, this snippet will help you on that (note, I did not come up with this myself - I borrowed it from http://www.mssqltips.com/tip.asp?tip=1426). This probably gives you a bit more than what you asked for. I actually wrapped this into a stored proc so can execute it from a remote server and store the results on that remove server for usage and trending analysis.

DECLARE @DBInfo TABLE  
(
ServerName SYSNAME,
DatabaseName SYSNAME,
LogicalFileName SYSNAME,
PhysicalFileName NVARCHAR(520),
/* [Size], [SpaceUsed] are identified as number of 8 KB pages */
Size INT,
SpaceUsed INT,
SpaceFree AS (Size - SpaceUsed),
SizeMB AS (Size / 128.0),
SpaceUsedMB AS (SpaceUsed / 128.0),
SpaceFreeMB AS ((Size - SpaceUsed) / 128.0),
SpaceUsedPct AS (100 - (((Size / 128.0) - (SpaceUsed / 128.0)) / (Size / 128.0))),
SpaceFreePct AS (((Size / 128.0) - (SpaceUsed / 128.0)) / (Size / 128.0)),
Status SYSNAME,
Updateability SYSNAME,
RecoveryModel SYSNAME,
PollDate DATETIME
);

DECLARE @PollDate NVARCHAR(23);
DECLARE @command VARCHAR(5000);

SET @PollDate = CONVERT(NVARCHAR(23), GETDATE(), 121);

SELECT @command =
'
USE ;
SELECT @@SERVERNAME AS ServerName,
''?'' AS DatabaseName,
sysfiles.name AS LogicalFileName,
sysfiles.filename AS PhysicalFileName,
sysfiles.size,
FILEPROPERTY(sysfiles.name, ' + '''' + 'SpaceUsed' + '''' + ' ),
CONVERT(sysname,DatabasePropertyEx(''?'', ''Status'')) AS Status,
CONVERT(sysname,DatabasePropertyEx(''?'', ''Updateability'')) AS Updateability,
CONVERT(sysname,DatabasePropertyEx(''?'', ''Recovery'')) AS RecoveryMode,
''' + @PollDate + '''
FROM dbo.sysfiles
';

INSERT INTO @DBInfo
(
ServerName,
DatabaseName,
LogicalFileName,
PhysicalFileName,
Size,
SpaceUsed,
Status,
Updateability,
RecoveryModel,
PollDate
)
EXEC sp_MSForEachDB @command

SELECT
*
FROM @DBInfo AS DI
ORDER BY ServerName, DatabaseName, DI.LogicalFileName
Go to Top of Page

obsoletedude
Starting Member

31 Posts

Posted - 2008-06-27 : 14:24:05
thank you tfountain. That is exactly what I needed.
Go to Top of Page

tfountain
Constraint Violating Yak Guru

491 Posts

Posted - 2008-06-27 : 16:36:23
quote:
Originally posted by obsoletedude

thank you tfountain. That is exactly what I needed.



Actually, this can be even more simplified (thanks to advice from Tara on another thread) by querying the sys.master_files table. This removes the need for running dynamically. However any part of that query that uses a function that must execute in the context of the database will not work (ie FILEPROPERTY).

So: SELECT * FROM sys.master_files ORDER BY name;
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-27 : 16:42:48
Run FILEPROPERTY using sp_executesql that way it runs in the context of the correct database.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tfountain
Constraint Violating Yak Guru

491 Posts

Posted - 2008-06-27 : 16:59:08
quote:
Originally posted by tkizer

Run FILEPROPERTY using sp_executesql that way it runs in the context of the correct database.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog




I personally liked the approach of not using dynamic SQL and hitting sys.master_files better IMO. I'm not sure how to approach your suggestion. Anyway, I ended up just using sys.database_files instead inside the loop in the above example. But for the OP, just querying sys.master_files should give him the information he needed.
Go to Top of Page
   

- Advertisement -