February 21, 2008 at 2:18 am
Hi,
I want to move file from c:\test1\test1.txt to c:\test2\test1.txt using FilesystemObject. I am using script as below:
EXEC SP_OACREATE 'SCRIPTING.FILESYSTEMOBJECT', @OBJFSYS OUT
EXEC SP_OAMETHOD @OBJFSYS, 'MOVEFILE', @FILE_NAME1,@FILE_NAME2, 1
I don't want to use xp_cmdshell. Please let me know where my code is wrong.
Regards
February 21, 2008 at 2:40 am
If you insist on using the sp_OAxx procedures, I suggest that you read up on them in BOL very carefully. If one doesn't know how to use these properly, it's a sure way to evetually blow your server out of the water.
Care to elaborate why xp_cmdshell 'copy myfile...' isn't viable?
/Kenneth
February 21, 2008 at 2:48 am
Thanks kenneth,
I don't want to use xp_cmdshell due to security reasons.
I didn't get any syntax related to copying or moving file @ BOL. If anyone used earlier pls. suggests.
Regards
February 22, 2008 at 1:20 am
Ah, I didn't mean look in BOL for how to copy files using sp_OAxx procs explicitly, I ment for how to use the actual sp_OAxx procs in general. They are powerful in that they let you create OLE objects, and they'll gladly shoot your leg off if you don't do it 'right'. That's the price you pay for the 'do-it-yourself' opportunity.
Regarding security, there is an option to use xp_cmdshell with lower privileges than the default sysadmin. Though it's anyone's choice, you will probably have some security concerns regarding the security anyway. From what I can see in BOL, sp_OACreate for example, requires sysadmin to execute.
..before anyone asks, no I've never used the sp_OAxx procs (more than some experimenting long time ago), simply because I regard them as 'dangerous' and there's always been other options around. I just want to make the point clear the using sp_OAxx does indeed require that the programmer has an absolute understanding of what he/she is doing with them...
/Kenneth
February 22, 2008 at 4:00 am
I agree, using COM in server environment is tricky. The least can happen when something goes wrong is stealing memory - after some time - day, week or month, serwer will grind to halt.
If the only thing to do is to copy the file, create an user without login, give it proper credentials and wrap the copy operation into a stored proc with EXECUTE AS in its definition. This way you will be on the safe side.
Piotr
...and your only reply is slΓ inte mhath
February 25, 2008 at 5:39 am
Hi kenneth,
No its does not require sysadmin rights to run these proc. You can specifically assign access to all 5 sp_oa .... proc. I am not getting any hint anywhere. As per BOL, the code below should work, but its not working.
Anyway i have got a way out. pls. check the code as below ..... if any one find any thing wrong in it pls. let me know......
DECLARE @oFS INT
DECLARE @oFol INT
DECLARE @oFC INT
DECLARE @oFil INT
DECLARE @Path VARCHAR(255)
DECLARE @Path1 VARCHAR(255)
DECLARE @FileName VARCHAR(255)
DECLARE @NumFiles INT
DECLARE @RetCode INT
DECLARE @ErrObject INT
DECLARE @Description VARCHAR(255)
DECLARE @Count INT
EXEC @RetCode = sp_OACreate 'Scripting.FileSystemObject', @oFS OUTPUT
SET @Path = 'D:\TEST\Test1\REN071219001.dat'
SET @Path1 = 'D:\TEST\Test2\REN071219001.dat'
EXEC @RetCode = sp_OAMethod @oFS, 'GetFile', @oFol OUTPUT, @Path
select @oFol,@RetCode
EXEC @RetCode = sp_OAMethod @oFol, 'MoveFile' , @Path,@Path1
select @RetCode
Thanks
Dhananjay
February 26, 2008 at 2:40 pm
hi there,
i'm not familiar with this kind of coding but in WMI you set the from file in a object variable (like oFrom)
and say oFrom.move ("c:\somewhere\file.ext")
so i think the information which you provide in the move statement could not work.
WMI sample:
Set fso = CreateObject("Scripting.FileSystemObject")
set oFrom = fso.getfile("c:\file.ext")
oFrom.copy("d:\file.ext")
i hope this will help you
*M*
February 27, 2008 at 3:10 am
Unfortunately, I'm not much help either when it comes to OLE programming.
(It's simply not turned on on my servers) π
For this type of task, I use:
exec @err = master..xp_cmdshell 'move 'D:\TEST\Test1\REN071219001.dat D:\TEST\Test2\REN071219001.dat'
..and that would be the end of it.
Security concerns around xp_cmdshell is a valid point, but so is letting users create arbitrary COM objects as well, if not even more. There are ways to use xp_cmdshell with a proxy account where you can limit permissions, I'm not certain in what context a sp_OACreate object would run.
/Kenneth
September 19, 2008 at 3:47 pm
Unfortunately, my web hosting vendor does not allow xp_cmdshell or sp_OACreate. π
I was looking at these options to move a file as well as means to export table data to a text file.
Could someone point me in the direction of another idea?
Thanks.
October 31, 2013 at 12:54 pm
I use these in several stored procs. Looks like you are missing a null.
Your code:
EXEC @RetCode = sp_OAMethod @oFol, 'MoveFile' , @Path,@Path1
Try adding a null.
EXEC @RetCode = sp_OAMethod @oFol, 'MoveFile' , NULL, @Path,@Path1
October 31, 2013 at 5:15 pm
Dhananjay-440114 (2/21/2008)
I don't want to use xp_cmdshell due to security reasons.
Heh... here we go again. π
--Jeff Moden
Change is inevitable... Change for the better is not.
October 31, 2013 at 5:20 pm
Kenneth Wilhelmsson (2/27/2008)
Security concerns around xp_cmdshell is a valid point...
I've found that most of the security concerns about xp_CmdShell are mostly based on emotion rather than anything practical. Turning it off provides no real layer of security and only hinders those that need to use it. The ONLY security concern I have about is those that allow non-SA users to use it directly. It can and should be all done through stored procedures.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 31, 2013 at 5:21 pm
The Code in my thread works fine... By the way its a long back story..:)
October 31, 2013 at 5:24 pm
Isle.Of.Mull (9/19/2008)
Unfortunately, my web hosting vendor does not allow xp_cmdshell or sp_OACreate. πI was looking at these options to move a file as well as means to export table data to a text file.
Could someone point me in the direction of another idea?
Thanks.
That's a real shame considering the frightful lack of security that a lot of web hosting vendors display that make any concerns about xp_CmdShell pale in comparison.
Use a batch file to run a stored procedure from SQLCmd and redirect the output to a file. Or, use Powershell.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 31, 2013 at 5:25 pm
Dhananjay-440114 (10/31/2013)
The Code in my thread works fine... By the way its a long back story..:)
We have the time. Fire away. π
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply