Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top