成都网站建设设计

将想法与焦点和您一起共享

一个SQLServer数据库文件批量重命名的代码示例

当我们需要重命名多个文件时,如果能够批量重命名文件,就能够大大提高工作效率。那么SQL Server数据库如何对文件进行批量重命名呢?本文我们通过一个代码示例来介绍这个过程,过程如下:

1. 新建自定义函数:从***一个特定字符开始截取字符串

 
 
 
  1. create function fn_GetLastCharIndex(@Chars varchar(1000),@Char varchar(10))
  2. returns varchar(1000)
  3. begin
  4. declare @i int
  5. set @i=CharIndex(@Char,@Chars)
  6. while(@i>0)
  7. begin
  8. set @Chars=Substring(@Chars,@i+1,len(@Chars))
  9. set @i=CharIndex(@Char,@Chars)
  10. end
  11. return @Chars
  12. end

2. 获取文件名列表

 
 
 
  1. if exists(select * from tempdb..sysobjects where id=object_id('tempdb..#temp'))
  2. drop table #temp
  3. create table #temp(filenames varchar(1000))
  4. Insert into #temp(filenames)
  5. Exec master..xp_cmdshell 'dir F:\灌篮高手-全国大赛全彩版'

3. 重命名

 
 
 
  1. declare @FileName varchar(1000),
  2. @NewFileName varchar(1000),
  3. @s varchar(1000)
  4. declare cur_1 cursor for
  5. select dbo.fn_GetLastCharIndex(filenames,' ') FileName from #temp where Right(filenames,4) in ('.jpg') and filename
  6. s like '%slamdunk%' order by 1
  7. open cur_1
  8. fetch cur_1 into @FileName
  9. while(@@fetch_status=0)
  10. begin
  11. set @NewFileName = Replace(@FileName,dbo.fn_GetLastCharIndex(@FileName,'-'),Right('00'+dbo.fn_GetLastCharIndex(@FileName,'-'),7))
  12. set @s='ren F:\灌篮高手-全国大赛全彩版\' + @FileName + ' ' + @NewFileName    
  13. Exec master..xp_cmdshell @s,no_output
  14. fetch cur_1 into @FileName
  15. end
  16. deallocate cur_1

上述了代码就描述了SQL Server数据库对文件的批量重命名的过程,希望能够带给您一些收获。本文就介绍到这里,谢谢大家的浏览!


网页标题:一个SQLServer数据库文件批量重命名的代码示例
链接URL:https://chengdu.cdxwcx.cn/article/cooojop.html