I'm trying to delete a excel file using xp_cmdshell, but is not working... I check the other questions of here but I can't solve my issue, I activated the advanced options of SQL SERVER to allow this sintax, but it show's me a error message like this:
No se encuentra C:\xampp\htdocs\Web_Seguimiento\Body\Upload\Files\Carga
No se encuentra C:\WINDOWS\system32\Masiva
NULL
My code:
---- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO
declare #sqlquery varchar(255)
declare #name varchar(255)
set #name = 'Carga Masiva Usuarios General.xlsx'
set #sqlquery= 'Del C:\xampp\htdocs\Web_Seguimiento\Body\Upload\Files\'+#name +''
exec master..xp_cmdshell #sqlquery
Can anybody tell me what I do wrong, or how can I solve this issue...???
Try putting quotes around the file name, like this:
set #sqlquery= 'Del "C:\xampp\htdocs\Web_Seguimiento\Body\Upload\Files\'+#name +'"'
Related
I have been using the following sql script to move file from one location to another .
EXEC
master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE
EXEC
master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE
DECLARE #cmd nvarchar(500), #folderName varchar(100),#move varchar(100),#destinationpath varchar(50)
SET #folderName = 'Newfolder_' + REPLACE(CONVERT(varchar(10), GETDATE(), 101), '/', '')
SET #cmd = 'mkdir C:\FileDestination\' + #folderName
EXEC master..xp_cmdshell #cmd --- this will create folder(newfolder_mmddyyy)
set #destinationpath='C:\FileDestination\'+#folderName
set #move ='move c:\filesource\* '+ #destinationpath
exec master.dbo.xp_cmdshell #move ---this will move files to newly created folder
But it's showing the error as follows
The system cannot find the file specified.
Can any one let me know the solution for this. Thanks Alot for your help !
If "filesource" and "FileDestination" are example folder locations, then it could be that the variable for the move command is too small. Try making it the same as #cmd, i.e. #move nvarchar(500)
It seems to be the access issue on the filesource folder, try giving full access to everyone on the folder.
I tried the exact code on my system and it works fine for me (moved test file).
I'm trying to extract images from a restored database. The script below ostensibly runs fine: meaning it extracts the images and saves the number of files I expect to see (and the size I would expect to see) into a network folder as .pdf files. However, when I try to open the files this is the error message I receive:
Adobe Reader could not open 'filename.pdf' because it is either not a supported file type or because the file
has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).
I have run this script before in other environments (2 to be exact) and it's worked fine.
I have verified that the actual images are good. You can access the files via the front end.
It doesn't appear that the values are encrypted. Although I'm not completely sure.
The Data Type natively is: image
I've been using SQL for a while but would consider myself a noob, whereas, I'm basically self-taught -so please go easy.
Any guidance / suggestions / insight will be very much welcome.
Thank you.
--========================================================================
USE <<ENTER_DATABASENAME>>
GO
sp_configure 'show advanced options', 1
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1
GO
RECONFIGURE
GO
DECLARE #FILEID VARCHAR(12),
#IMAGE VARBINARY(MAX),
#OBJSTREAM INT,
#FILENAME VARCHAR(MAX),
#BASEDIR VARCHAR(MAX)
SET #BASEDIR = '<<ENTER_NETWORKPATH>>'
PRINT 'BASEDIR: ' + #BASEDIR
DECLARE IMGEXTRACT CURSOR FAST_FORWARD FOR
select <<ENTER_COLUMNNAME01>>, <<ENTER_COLUMNNAME02>>
from <<ENTER_TABLENAME>>
OPEN IMGEXTRACT
FETCH NEXT FROM IMGEXTRACT INTO #FILEID, #IMAGE
WHILE ##FETCH_STATUS = 0
BEGIN
SET #FILENAME = #BASEDIR + '\' + #FILEID + '.pdf'
PRINT #FILENAME
EXEC sp_OACreate 'ADODB.Stream', #OBJSTREAM OUTPUT
EXEC sp_OASetProperty #OBJSTREAM, 'Type', 1
EXEC sp_OAMethod #OBJSTREAM, 'Open'
EXEC sp_OAMethod #OBJSTREAM, 'Write', NULL, #IMAGE
EXEC sp_OAMethod #OBJSTREAM, 'SaveToFile', NULL, #FILENAME, 2
EXEC sp_OAMethod #OBJSTREAM, 'Close'
EXEC sp_OADestroy #OBJSTREAM
FETCH NEXT FROM IMGEXTRACT INTO #FILEID, #IMAGE
END
CLOSE IMGEXTRACT
DEALLOCATE IMGEXTRACT
GO
--========================================================================
The following code is used to call a web service from SQL Server:
Declare #Object as Int;
Declare #ResponseText as VARCHAR(MAX);
Exec sp_OACreate 'MSXML2.XMLHTTP', #Object OUT;
Exec sp_OAMethod #Object, 'open', NULL, 'GET','http://localhost/Service1.asmx/HelloWorld?name=xyz', false;
Exec sp_OAMethod #Object, 'send';
EXEC sp_OAGetErrorInfo #Object
Create table #tmp(dt varchar(max))
insert into #tmp
exec sp_OAGetProperty #Object, 'ResponseText' --,#strLine OUtPUT
Select dt from #tmp -- single column/single row.
Drop Table #tmp -- clean up
Exec sp_OADestroy #Object;
But I am getting below error:
0x800C0005 msxml3.dll System error: -2146697211
Any pointers ...?
There are two (2) ways to fix Msxml3 Dll 0x800c0005 System Error Error:
Advanced Computer User Solution (manual update):
1) Start your computer and log on as an administrator.
2) Click the Start button then select All Programs, Accessories, System Tools, and then click System Restore.
3) In the new window, select "Restore my computer to an earlier time" option and then click Next.
4) Select the most recent system restore point from the "On this list, click a restore point" list, and then click Next.
5) Click Next on the confirmation window.
6) Restarts the computer when the restoration is finished.
Novice Computer User Solution (completely automated):
1) Download (Msxml3 Dll 0x800c0005 System Error) repair utility.
2) Install program and click Scan button.
3) Click the Fix Errors button when scan is completed.
4) Restart your computer.
More details
I use char(13) and char(10) for break text in sql server.
I want when copy text filed content top the notepad , text in break.
I use below code to break line :
declare #result varchar(30)='this is some'+ char(13)+char(10)+'text'
but when I copy text to notepad all text show in a line .
how can I do this ?
I searched in google , but cant find any similar answer.
it have to work:
just try this one, (using only one of char(13) or char(10) also gives same output)
declare #result varchar(30)='this is some'+char(13)+char(10)+'text'
print #result
Output:(copied exact output from my window)
this is some
text
but for getting the result to notepad(windows) don't copy/past from the displayed grid within management studio use xp_cmdshell as below:
master..xp_cmdshell 'bcp "SELECT ''this is some''+char(13)+char(10)+''text''" queryout C:\myText.txt -t, -c -Slocalhost -T'
exact content:
this is some
text
Note: you may need to enable xp_cmdshell:
EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
EXEC sp_configure 'xp_cmdshell', 1
GO
RECONFIGURE
GO
I have a Zip file created and I am unable to delete it using the below command.
xp_cmdshell 'rm "F:\EXIS\Reports\Individual.zip"'
It gives an error saying File not found, when I can actually see the file.
I tried using xp_cmdshell 'del "F:\EXIS\Reports\Individual.zip"'
But, this asks for a confirmation, which I actually cannot input.
Please suggest if anything,
Thanks.
The message is more generic in the sense the file is not found with the current credentials of SQL Server process while accessing the indicated location.
I suspect it is a problem of rights, so please assure the SQL Server proecess has rights to delete file in that location. An alternative suggestion is to perform a "dir" on that location.
Try executing delin silent mode like:
xp_cmdshell 'del /Q "F:\EXIS\Reports\Individual.zip"'
And also: if SQL Server is running on a different machine the path must of course be valid for that machine.
--change server configuration like :
--Script to enable the XP_CMDSHELL
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1 GO
-- To update the currently configured value for advanced options.
RECONFIGURE GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1 GO
-- To update the currently configured value for this feature.
RECONFIGURE GO;
DECLARE #BackUpPath varchar(255) ,#sqlQuery varchar(255)
Set #BackUpPath='D:\All DB BackUp\TestDB.bak'
IF dbo.fn_FileExists(#BackUpPath)=1
BEGIN
SET #sqlQuery='DEL /Q "' + #BackUpPath + '"';
PRINT #sqlQuery
EXEC master..xp_cmdshell #sqlQuery
IF dbo.fn_FileExists(#BackUpPath)=0
BEGIN
PRINT 'File Deleted'
END
ELSE
BEGIN
PRINT 'File not Deleted'
END
END
IF #BackUpPath contains space , you must type your path like "your
path"