Hello,
I have a group of files in a folder. I want to rename each file in the folder to something different.
This is a process that will run each day and the file names will always be different. I want my user to run a SQL script to accomplish this without any necessary input from the user.
I am assuming a cursor is the best way to go with the master..xp_cmdshell? If you have a better option, please let me know what you think.
In the meantime, I have written this:
However I am getting an error:
Line 13: Incorrect syntax near '+'
Any idea what I am missing? If I change the 'exec master..xp_cmdsell' to 'print' it prints out the rename scripts just fine.
Is there an issue running the xp_cmdshell in a cursor?
I have a group of files in a folder. I want to rename each file in the folder to something different.
This is a process that will run each day and the file names will always be different. I want my user to run a SQL script to accomplish this without any necessary input from the user.
I am assuming a cursor is the best way to go with the master..xp_cmdshell? If you have a better option, please let me know what you think.
In the meantime, I have written this:
Code:
set nocount on
go
declare RenameFiles scroll cursor
for select distinct filename, newfilename from loadimages
where filename is not null
declare @filename varchar(200)
declare @newfilename varchar(200)
open RenameFiles
fetch next from RenameFiles into @filename, @newfilename
while (@@fetch_status =0)
begin
exec master..xp_cmdshell 'rename "\\ServerName\ShareName\FolderName\unzipped\' + @filename + '" ' + @newfilename
fetch next from RenameFiles into @filename, @newfilename
end
deallocate RenameFiles
However I am getting an error:
Line 13: Incorrect syntax near '+'
Any idea what I am missing? If I change the 'exec master..xp_cmdsell' to 'print' it prints out the rename scripts just fine.
Is there an issue running the xp_cmdshell in a cursor?