SQLServer過濾檔案名稱特殊字元(利用SQLServer修改檔案物理位址名稱),特殊字元sqlserver

來源:互聯網
上載者:User

SQLServer過濾檔案名稱特殊字元(利用SQLServer修改檔案物理位址名稱),特殊字元sqlserver

declare @cmd nvarchar(2000)    declare @messageid nvarchar(200)    declare @serialid nvarchar(200)    declare @oldFileName nvarchar(500)    declare @fileName nvarchar(500)     declare @filePath nvarchar(4000)    declare @oldfilePath nvarchar(4000)  --將路徑D:改成D$    declare @newFilePath nvarchar(4000)    declare ccur cursor for select d.MessageId,m.serialid,m.AttachmentName,m.AttachmentPath from d left join r on d.MessageID=r.MessageID left  join DataAttachment m on m.MessageID=d.MessageID where  d.messageid='0.abe3b3d58161ccb3de7ab63754bb3f15'  --通過資料庫尋找所需要的附件             open ccur             fetch ccur into @messageid,@serialid,@fileName,@filePath             while @@fetch_status=0                 begin                    --判斷附件檔案名稱中是否含有特殊字元                     if CHARINDEX('+',@filename)>0 or CHARINDEX('$',@filename)>0 or CHARINDEX('&',@filename)>0  --含有+、$、&                    begin                                          set @oldfileName=@filename                       set @filename=replace(@filename,'+','')                       set @filename=replace(@filename,'$','')                       set @filename=replace(@filename,'&','')    --過濾這些特殊字元,替換後新的檔案名稱                       set @newFilePath=dbo.GetDirectoryPath(@filePath) + '\' + @filename  --新路徑名(<span style="font-family: Arial, Helvetica, sans-serif;">dbo.GetDirectoryPath方法為根據路徑擷取檔案目錄</span><span style="font-family: Arial, Helvetica, sans-serif;">)</span>                       --舊路徑名更改成磁碟驅動                       set @oldfilePath=replace(@filePath,'D:','D$')                            set @oldfilePath='\\192.168.21.36\' + @oldfilePath   --附件在遠程伺服器下面,如果不是遠程,不用加磁碟路徑                                              -- exec master.dbo.xp_cmdshell 'net use \\<span style="font-family: Arial, Helvetica, sans-serif;">192.168.21.36</span><span style="font-family: Arial, Helvetica, sans-serif;">\d$ "123456" /user:"192.168.21.36\administrator"'</span>                       --exec  master.dbo.xp_cmdshell   'ren   \\<span style="font-family: Arial, Helvetica, sans-serif;">192.168.21.36</span><span style="font-family: Arial, Helvetica, sans-serif;">\d$\Book1.xls,Book2.xls'</span>                       set @cmd='ren '  + @oldfilePath + ',' + @filename + ''    --拼湊需要自行的命令                       --修改檔案物理位置名稱(串連遠程伺服器及其需要修改檔案名稱所在的盤符)                       exec master.dbo.xp_cmdshell 'net use \\192.168.21.36\d$ "123456" /user:"192.168.21.36\administrator"'                       exec  master.dbo.xp_cmdshell  @cmd  --@cmd的長度必須設定具體資料,不能設定為nvarchar(max),會報“過程需要類型為 'varchar' 的參數 'command_string'”錯誤                       --插入檔案名稱修改日誌表                       insert into DataFileNameUpdateLog(messageid,oldFileName,newFileName,oldFilePath,newFilePath)                       values(@messageid,@oldfileName,@filename,@filePath,@newFilePath)                       --根據serialid更新新檔案名稱和新路徑(serialid為唯一性)                       update A set AttachmentName=@filename,AttachmentPath=@newFilePath where serialid=@serialid                       --給我插入訊息提醒                       declare @content nvarchar(max)                       set @content='集團來文附件名含有特殊符號,已過濾,請注意查看。serialid:' + @serialid + ',messageid:' + @messageid +                       ' ,原檔案名稱:' + @oldfileName + ',新檔案名稱:' + @filename + '(系統自動發送)'                       exec aa.dbo.[aaRemind] 0,'',11182,@content                    end                                    fetch ccur into @messageid,@serialid,@fileName,@filePath                              end                             close ccur                deallocate ccur  end


根據路徑擷取檔案目錄dbo.GetDirectoryPath方法

-- =============================================  -- Author:        Paul Griffin  -- Create date:   18 January 2015  -- Description:   Returns the path without the file name  --                from a full path:  --                    D:\Temp\Resources\Images\My.Picture.jpg  --                ==> D:\Temp\Resources\Images  -- =============================================  CREATE FUNCTION [dbo].[GetDirectoryPath]  (      @Path NVARCHAR(MAX)  )  RETURNS NVARCHAR(MAX)  AS  BEGIN      DECLARE @FileName NVARCHAR(MAX)      DECLARE @ReversedPath NVARCHAR(MAX)      DECLARE @PathLength INT         SET @ReversedPath = REVERSE(@Path)      SELECT @PathLength = CHARINDEX('\', @ReversedPath)      SELECT @FileName = LEFT(@Path, LEN(@Path) - @PathLength)      RETURN @FileName  END  


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.