|
Question : Renaming a file using cmdShell
|
|
Hi,
I have this code I wrote to rename a file. The first parameter is the file name and the second is the new name. When I run it, it tells me "The Syntax of the command is incorrect" and "NULL".
CREATE PROCEDURE dbo.RenFile(@ip_FName varchar(25), @ip_NewName varchar(25)) AS
DECLARE @cmdTxt varchar(50) set @cmdTxt = 'ren ' set @cmdTxt = @cmdTxt + @ip_FName set @cmdTxt = @cmdTxt + ' ' set @cmdTxt = @cmdTxt + @ip_NewName
exec master..xp_cmdshell @cmdTxt --'ren c:\test.txt c:\somethingelse.txt'
set @cmdTxt = 'e:\websites\MDS\docs\cat1\' set @cmdTxt = @cmdTxt + @ip_NewName
update Documents set DocLoc = @cmdTxt where DocLoc = (select DocLoc from Documents where DocLoc = @ip_FName) GO
JP
|
|
Answer : Renaming a file using cmdShell
|
|
One suggestion: make the @cmdTxt bigger in declare since your inputs could exceed the length of it and you can test the result from executing the command as the following:
CREATE PROCEDURE dbo.RenFile(@ip_FName varchar(25), @ip_NewName varchar(25)) AS DECLARE @result int, @cmdTxt varchar(250)
set @cmdTxt = 'ren ' set @cmdTxt = @cmdTxt + @ip_FName set @cmdTxt = @cmdTxt + ' ' set @cmdTxt = @cmdTxt + @ip_NewName
@result = exec master..xp_cmdshell @cmdTxt --'ren c:\test.txt c:\somethingelse.txt'
IF (@result = 0) PRINT 'Success' ELSE PRINT 'Failure'
|
|
|
|