Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 delete dir with t-SQL

Author  Topic 

scrap
Yak Posting Veteran

57 Posts

Posted - 2005-02-28 : 11:51:46
Hi, how can I delete files and directories with T-SQL

Thank you

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2005-02-28 : 12:30:27
This will remove directory:
declare @rmdircmd nvarchar(280);
set @rmdircmd = N'RMDIR C:\blah\blah\ /S /Q'
exec master.dbo.xp_cmdshell @rmdircmd, no_output

Hopefully it will get You started if you know the dos commands.

rockmoose
Go to Top of Page

scrap
Yak Posting Veteran

57 Posts

Posted - 2005-02-28 : 15:28:59
Thank you very much.
It was very helpful
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2005-02-28 : 15:35:58
You're welcome.
Post back if there is anything else...

rockmoose
Go to Top of Page

scrap
Yak Posting Veteran

57 Posts

Posted - 2005-03-01 : 14:32:30
I forgot, and about the files, is it still the rmdir command?
Thanks
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2005-03-01 : 15:00:31
from the command prompt type "del /?" and You will see the syntax for the DEL command.
DEL is used for deleting files.

rockmoose
Go to Top of Page

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2005-03-01 : 15:01:36
Well, that depends. Are the files in the directory? If they're in another directory, you will need to use the DEL command. Otherwise, the RMDIR will work. You'll have to use the /S option though.

MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page

scrap
Yak Posting Veteran

57 Posts

Posted - 2005-03-01 : 15:19:27
Ouch, this is fast replying. It must be the forum with the most active members!

Thank you Derrick
Go to Top of Page

scrap
Yak Posting Veteran

57 Posts

Posted - 2005-03-01 : 15:21:43
...and moose!
Go to Top of Page

scrap
Yak Posting Veteran

57 Posts

Posted - 2005-03-01 : 15:28:17
Finally, here's what i did:
declare @rmdircmd nvarchar(280);
declare @rmdircmd1 nvarchar(280);
set @rmdircmd = N'del \\cgcapp\Report\presence.dbf /s'
set @rmdircmd = N'del \\cgcapp\Report\presence.rdf /s'
exec master.dbo.xp_cmdshell @rmdircmd, no_output
Go to Top of Page

scrap
Yak Posting Veteran

57 Posts

Posted - 2005-03-01 : 15:44:41
Actually, it looks more like:
declare @rmdircmd nvarchar(280);
declare @rmdircmd1 nvarchar(280);
set @rmdircmd = N'del \\cgcapp\Report\presence.dbf /q'
set @rmdircmd1 = N'del \\cgcapp\Report\presence.rdf /q'
exec master.dbo.xp_cmdshell @rmdircmd, no_output
exec master.dbo.xp_cmdshell @rmdircmd1, no_output

Thank you all!!
Go to Top of Page

SqlNewbieMe
Starting Member

2 Posts

Posted - 2014-01-09 : 14:13:33
Hi, is there a way to remove directories older than x days using T-sql

Thanks
Go to Top of Page

SqlNewbieMe
Starting Member

2 Posts

Posted - 2014-01-09 : 14:15:46
Hi, is there a way to remove directories older than x days using T-sql. The above tsql works even on an UNC path only issue is to remove Directories older than x days..

Thanks
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2014-01-09 : 15:26:41
use the FORFILES command.

here is an example.
Go to Top of Page
   

- Advertisement -