0

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.

Pavan Kumar
  • 463
  • 2
  • 10
  • 18
  • "when I can actually see the file" - *Where* can you see it? The server doesn't necessarily have the same drive mappings as you do; it may not know anything about `F:\` at all. – Ken White Nov 11 '13 at 07:24
  • @KenWhite : I can see the file in server location only. I an able to delete other files of others types which are in the same location. – Pavan Kumar Nov 11 '13 at 07:44

3 Answers3

1

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.

jpw
  • 44,361
  • 6
  • 66
  • 86
1

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.

bjnr
  • 3,353
  • 1
  • 18
  • 32
1

--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"