1

我正在使用 T-SQL

我在这里有一些 excel 文件:C:\MyFiles\

我想删除该目录中文件名中的所有撇号。

现在要删除撇号,可以使用这样的代码。

update MyTable
set FileName= replace(FileName, '''', '')

如果我在数据库中拥有所有文件名,那么使用上面的代码很容易。但我需要更新位于磁盘上的文件名。

我将如何使用 T-SQL 执行此操作?

它必须是 T-SQL,因为我需要将它添加到存储过程中的现有代码中。

4

3 回答 3

4
SET NOCOUNT ON;

 CREATE TABLE #FileList
    (
     FileID INT IDENTITY(1, 1)
    ,Line VARCHAR(512)
    )  

 CREATE TABLE #temp
    (
     isFileThere BIT
    ,isDirectory BIT
    ,parentDirExists BIT
    )

 DECLARE @Command VARCHAR(1024)
 ,  @RowCount INT
 ,  @counter INT
 ,  @FileName VARCHAR(1024)
 ,  @FileExists BIT
 SET @Command = 'dir C:\MyFiles\  /A-D  /B'

 PRINT @Command
 INSERT #FileList
        EXEC master.dbo.xp_cmdshell @Command

 DELETE FROM #FileList
 WHERE  Line IS NULL

 SELECT @RowCount = COUNT(*)
 FROM   [#FileList]

 SET @counter = 1

 WHILE ( @counter <= @RowCount ) 
    BEGIN
        SELECT  @FileName = [Line]
        FROM    [#FileList]
        WHERE   [FileID] = @counter 

        SET @Command = 'C:\MyFiles\' + @FileName + ''

 PRINT @Command

        INSERT  [#temp]
                EXEC master.dbo.xp_fileExist @Command

        SELECT  @FileExists = [isFileThere]
        FROM    [#temp] 


        IF @FileExists = 1
            AND CHARINDEX('''', @FileName) > 0 
            SET @Command = 'REN "C:\MyFiles\' + @FileName + '" "'
                + REPLACE(@FileName, '''', '') + '"'
        ELSE 
            SET @Command = ''

        SET @counter = @counter + 1

        PRINT @Command

        IF LEN(@Command) > 0 
            EXEC master.dbo.xp_cmdshell @Command

    END 

 DROP TABLE #FileList

 DROP TABLE [#temp]
于 2012-06-29T10:34:07.253 回答
2

首先你需要启用 xp_cmdshell

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

然后您可以使用 sp_cmdshell 从目录中检索文件名

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

现在,您在表变量 @FILE 中获取文件名,并使用 replace 之类的函数或您自己的自定义函数将撇号替换为确切的文件名

于 2012-06-28T11:26:24.867 回答
0

试试这个它会为你工作

1)如下所述创建一个sp

CREATE PROCEDURE dbo.ListPathsXML
@FileSpec VARCHAR(2000),
@order VARCHAR (80) = '/O-D',--sort by date time oldest first
@xmlFileList XML OUTPUT

AS
DECLARE @myfiles TABLE (MyID INT IDENTITY(1,1) PRIMARY KEY, FullPath VARCHAR(2000))
DECLARE @CommandLine VARCHAR(4000)
IF @order IS NOT NULL -- abort if the order is silly
   BEGIN
   SELECT @CommandLine =LEFT('dir "' + @FileSpec + '" /A-D /B /S '+@order,4000)
   INSERT INTO @MyFiles (FullPath)
       EXECUTE xp_cmdshell @CommandLine
   DELETE FROM @MyFiles WHERE fullpath IS NULL
       OR fullpath = 'File Not Found'
   END
SET @xmlFileList = (SELECT fullpath FROM @MyFiles
                         FOR
                          XML PATH('thefile'),
                              ROOT('thefiles'),
                              TYPE)

2)然后给出要替换文件名的目录名称

DECLARE @LotsOfText NVARCHAR(MAX),
       @ii INT,
       @iiMax INT,
       @File VARCHAR(2000),
       @Command NVARCHAR(4000)
DECLARE @files TABLE (MyID INT IDENTITY(1,1) PRIMARY KEY, [Path] VARCHAR(2000))

DECLARE @FileList XML
EXECUTE ListPathsXML 'D:\QAconfig\',
    DEFAULT , @XMLFileList = @FileList OUTPUT

INSERT INTO @files(path)
   SELECT   x.thefile.value('fullpath[1]', 'varchar(2000)') AS [path]
        FROM    @FileList.nodes('//thefiles/thefile') AS x ( thefile )
--don't look at the current errorlog!
SELECT @ii=1, @iiMax=MAX(MyID) FROM @Files
WHILE @ii<=@iiMax
   BEGIN
   SELECT @File= [path] FROM @files WHERE MyID=@ii
   print @File
   SELECT @command='EXEC master..xp_cmdshell' + '''MOVE '+ Replace(@FILE,'''','''''') + ' ' +REPLACE(@FILE,'''','') +''''
   print @command
   EXECUTE sp_ExecuteSQL @command--, N'@lotsOfText nvarchar(max) output ',@lotsoftext output 
   SELECT @ii=@ii+1
   END
于 2012-06-28T12:30:00.463 回答