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).
Related
I'm trying to run the script below, but it returns null. When I run the DOS command, it generates the file normally.
DECLARE #str VARCHAR(1000)
SET #str = 'bcp "Select * FROM WDG.dbo.Facilidade" queryout "w:\xyzTable.txt" -S "WDG-NOTE24\MSSQLWDG" -T -c -t ; '
EXEC xp_cmdshell #str
GO
I need to return a separate txt file for ';' with query data
Tanks
Given your error in your comments i will follow this link and as i described earlier its an access problem to your folder.
Remember it should be given to your Sql service account user and not your self
BCP unable to open BCP host access
I managed to make it work, I found the command below that tests if it has access to the directory.
For some reason did not accept the old path, so I created another one on another disk and it worked.
EXEC master..xp_cmdshell 'DIR C:\sql'
Very thank's for help.
I have a problem, at the end of my import file, it comes with the text '--END--', and when the bulk insert is going to render, it displays an unexpected end message.
I can put some parameter so that when it finds the text it finishes the import.
declare #sql varchar(max)
set #sql = 'BULK INSERT Temp_Facilite FROM ''' + ##FullPath + '''WITH (FIRSTROW = 2,CODEPAGE = ''RAW'',FIELDTERMINATOR = '';'',ROWTERMINATOR = ''0x0A'',MAXERRORS = 3, KEEPNULLS );'
exec (#sql)
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'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 +'"'
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"
How do we transfer the content of a string variable to a file on a SP? I need to do these:
Create an empty text file from an SP
Push the content of a variable (whose len is 25487) to the newly created file
The variable size is Varchar(Max), and this is the code I am trying to make work (but it ain't [Sad] ) ---
Declare #cmd sysname
Declare #ReqContent Varchar(max)
SET #cmd = 'echo ' + #ReqContent + ' > C:\DD4TRD.txt'
EXEC master..xp_cmdshell #cmd ,NO_OUTPUT
Thanks,
Sayan
Check out this sproc from Reading and Writing Files in SQL Server using T-SQL.