0

I have enabled Xp_cmdshell extended stored procedure successfully.

I have typed the command

EXEC xp_cmdshell 'del C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\Backup\testdelete\CallRecordings\2020\05\05\Call'

and despite the file path is correct it indicates

enter image description here

Can anyone help why that doesn't work ?

jarlh
  • 42,561
  • 8
  • 45
  • 63
  • First: why would you enable `xp_cmdshell`? Don't do that. Anything you think you want to use `xp_cmdshell` for should probably be getting handled by an external process that connects to SQL Server. – AlwaysLearning Nov 09 '21 at 09:29
  • Second: What would `del C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\Backup\testdelete\CallRecordings\2020\05\05\Call` do at a Command Prompt? Try to delete `C:\Program`? Try instead `del "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\Backup\testdelete\CallRecordings\2020\05\05\Call"` with quotes (`"`). – AlwaysLearning Nov 09 '21 at 09:30
  • i used " " and now it just indicates the path C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\Backup\testdelete\CallRecordings\2020\05\05\Call\*, Are you sure (Y/N)? – Andreas Rigas Nov 09 '21 at 09:36
  • HOW can i select Y to delete the file ? – Andreas Rigas Nov 09 '21 at 09:36
  • This is yet another reason not to use `xp_cmdshell`: commands that prompt for input will block `xp_cmdshell` and never terminate. How would you do this from a Command Prompt? `del /f /q "C:\Path To Your\File"` – AlwaysLearning Nov 09 '21 at 09:44
  • So you say xp_cmdshell cannot work. The fact is that afterwards i want to connect it to an sql job and delete files according to date. So i need to start from somewhere and i found xp_cmdshell command. – Andreas Rigas Nov 09 '21 at 09:48
  • i have also used the command EXECUTE master.dbo.xp_delete_file 0, 'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\Backup\testdelete\CallRecordings\2020\05\05\Call','.wav' but it just returns int value 0 and the file remains intact – Andreas Rigas Nov 09 '21 at 09:52
  • i found it here https://stackoverflow.com/questions/69885214/information-about-master-dbo-xp-delete-file/69894515#69894515 – Andreas Rigas Nov 09 '21 at 10:10
  • Why would you do this from TSQL anyway? TSQL is not a generalized scripting language, use Powershell, Python or similar – Charlieface Nov 09 '21 at 10:24
  • my superior told to find a way to do it like that. – Andreas Rigas Nov 09 '21 at 10:30

0 Answers0