Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
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: 2795 | Views in the last 30 days: 1
 
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