Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Proc to Rename a file

By Simon Facer,

The calls to this routine are simple, it's compiled to master but it could go anywhere, just change the prefix from sp_ !!!

The parameters are:

@Folder The folder where the file exists. REQUIRED.
@FileName The file to be renamed. REQUIRED.
@NewFileName The new file name. OPTIONAL - If not specified (NULL or empty / space) -
add a DateTime literal (_yyyymmddhhnnss) to the filename, as in filename.ext becomes filename_yyyymmddhhnnss.ext
@DateTimeType One of (not case sensitive): CREATED File Created DateTime, MODIFIED Modified DateTime, NULL / '' NULL or spaces. Value used to generate the DateTime literal for the default generated filename. OPTIONAL - If not specified (NULL or empty / space) - use GETDATE().
@Debug Switch to determine if debugging information should be output OPTIONAL - defaults to 0 (No).

An example of a call to this proc, to rename file 'D:\SQL Log\Full Backups.log' to 'D:\SQL Log\Full Backups_200805010600.log' (using the file creation date-time of 05/01/2008 06:00am)

EXECUTE .[sp_RenameFile]
@Folder = 'D:\SQL Log\'
,@FileName = 'Full Backups.log'
,@NewFileName = NULL
,@DateTimeType = 'Created'
,@Debug = 0

Total article views: 2776 | Views in the last 30 days: 2
 
Related Articles
FORUM

how to create folder at runtime using part of filename

how to create folder at runtime using part of filename

SCRIPT

Create A Folder With T-SQL

Pass a folder name to this SP and it will create the folder for you.

FORUM

Script to create datestamp folder causing issue

Script to create datestamp folder causing issue

FORUM

Error....While creating Folder through SQL

Error....While creating Folder through SQL

FORUM

Create, Rename, Delete file folder thru SQL

Create, Rename, Delete file folder thru SQL

Tags
miscellaneous    
sql server 2005    
sql server 7    
t-sql    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones