Database File Path to Directory

  • Hi All,

    Can any one tell me how can i get info for database files location. as i know that if i use

    select * from sys.files. it's give me the acutally phiysical location of the file but i just need the path only.

    what sys.files give me :

    C:\data\x.mdf.

    but i m only looking for

    C:\data\

    Thanks and looking forward.

    -MALIK

  • Try this out

    select Left(filename, len(filename)-charindex('\',reverse(filename))+1) from [sysfiles]

    HTH

  • thanks. it's working now

  • welcome 🙂

  • if u dont' mind can i ask one more question.

    is there any code or script that can be used to create a copy of Database with new name and location.

    i dont' know the way i m doing is professional. what i do, i just take the back of the database which i want to use to create a new database and then restore as new name and locations.

    if you have better idea , please let me know. Acutally i m looking for script or procuder that can do this.

    Thanks and looking forward.

    -MALIK

  • whatever you are doing is correct, this is manual way of restoring the database with new name. Also you can write a stored procedure with two input parameters as DBName and location and inside the procedure just write the restore DB script.

Viewing 6 posts - 1 through 5 (of 5 total)

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