一個SQL Server數據庫文件批量重命名的代碼示例
作者:tohen
本文我們主要介紹了SQL Server數據庫對文件進行批量重命名的代碼示例,希望通過這個代碼示例,能夠給您帶來一些幫助。
當我們需要重命名多個文件時,如果能夠批量重命名文件,就能夠大大提高工作效率。那么SQL Server數據庫如何對文件進行批量重命名呢?本文我們通過一個代碼示例來介紹這個過程,過程如下:
1. 新建自定義函數:從***一個特定字符開始截取字符串
- create function fn_GetLastCharIndex(@Chars varchar(1000),@Char varchar(10))
- returns varchar(1000)
- begin
- declare @i int
- set @i=CharIndex(@Char,@Chars)
- while(@i>0)
- begin
- set @Chars=Substring(@Chars,@i+1,len(@Chars))
- set @i=CharIndex(@Char,@Chars)
- end
- return @Chars
- end
2. 獲取文件名列表
- if exists(select * from tempdb..sysobjects where id=object_id('tempdb..#temp'))
- drop table #temp
- create table #temp(filenames varchar(1000))
- Insert into #temp(filenames)
- Exec master..xp_cmdshell 'dir F:\灌籃高手-全國大賽全彩版'
3. 重命名
- declare @FileName varchar(1000),
- @NewFileName varchar(1000),
- @s varchar(1000)
- declare cur_1 cursor for
- select dbo.fn_GetLastCharIndex(filenames,' ') FileName from #temp where Right(filenames,4) in ('.jpg') and filename
- s like '%slamdunk%' order by 1
- open cur_1
- fetch cur_1 into @FileName
- while(@@fetch_status=0)
- begin
- set @NewFileName = Replace(@FileName,dbo.fn_GetLastCharIndex(@FileName,'-'),Right('00'+dbo.fn_GetLastCharIndex(@FileName,'-'),7))
- set @s='ren F:\灌籃高手-全國大賽全彩版\' + @FileName + ' ' + @NewFileName
- Exec master..xp_cmdshell @s,no_output
- fetch cur_1 into @FileName
- end
- deallocate cur_1
上述了代碼就描述了SQL Server數據庫對文件的批量重命名的過程,希望能夠帶給您一些收獲。本文就介紹到這里,謝謝大家的瀏覽!
【編輯推薦】
責任編輯:趙鵬
來源:
博客園