Rename files on disk with t-sql

后端 未结 3 973
南笙
南笙 2021-01-16 04:15

I am using T-SQL

I have a few excel files located here: C:\\MyFiles\\

I want to remove all the apostrophes in the file names in that directory.<

3条回答
  •  囚心锁ツ
    2021-01-16 04:33

    First you need to enable xp_cmdshell

    EXEC sp_configure 'show advanced options', 1
    GO
    RECONFIGURE
    GO
    EXEC sp_configure 'xp_cmdshell', 1
    GO
    RECONFIGURE
    GO
    

    Then you can use sp_cmdshell to retrieve the files names from the directory

    Declare @Directory TABLE (Files Varchar(MAX))
    Declare @File TABLE (Name varchar(MAX))
    INSERT INTO @Directory
    EXEC XP_CMDSHELL 'DIR "D:"'
    Insert into @File
    Select reverse(LEFT(reverse(Files),charindex(' ' ,reverse(Files)))) from @Directory
    Select * from  @FILE
    

    Now you get the file names in the table variable @FILE and use function like replace or your own custom function to replace apostrophes with the exact filename

提交回复
热议问题