Author |
Topic |
X002548
Not Just a Number
15586 Posts |
Posted - 2003-10-24 : 15:35:30
|
I tried thismaster..xp_cmdshell 'DELETE d:\Data\Tax\ETRS\Archive\Oct__1_2003__8_52AM\*.*' but it doesn't work because it expects a response...are you sure you want...Then I tried..DECLARE @cmd varchar(8000)SELECT @cmd = 'DELETE d:\Data\Tax\ETRS\Archive\Oct__1_2003__8_52AM\*.*' + CHAR(10)+CHAR(13) + 'Y'EXEC master..xp_cmdshell @cmd In a failed attempt at being clever...How can you clean out all files in a directory...Or more to the point, how do you use xp_cmdshell when a response is expected?Any help appreciated.Brett8-) |
|
jhermiz
3564 Posts |
Posted - 2003-10-24 : 15:58:25
|
why are you using xp_cmdshell to do this?why dont you clean out a directory through the console ? |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2003-10-24 : 15:58:52
|
Good question Brett. I have nothing I can offer except a weak attempt at humor: "You need to add little martini drinking men to the code to respond "Y"." Coincidently, I've run into a problem using xp_cmdshell myself.See you later.Sam |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2003-10-24 : 16:00:34
|
quote: why are you using xp_cmdshell to do this?why dont you clean out a directory through the console ?
Clean the folder himself using the console? How many reasons do you want?Sam |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2003-10-24 : 16:01:13
|
You could use the FileScripting object and sp_oa commands.... |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2003-10-24 : 16:05:23
|
Would you post an example using the filescripting object? |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2003-10-24 : 16:17:24
|
You need to add /Q to your delete statement. Just run del /? in a cmd window and you'll see the options. /Q allows the delete statement to run in quiest mode.Tara |
|
|
Granick
Starting Member
46 Posts |
Posted - 2003-10-24 : 16:19:21
|
Assuming you are just doing a command line wild card delete, you should be able to pass a /Q, which is quiet mode. If there are read-only files in the directory, then you might need to add /F as well.Shannon |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-10-24 : 16:27:48
|
try a /Q.oops - thought I read through all the responses.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
iapetus
Starting Member
15 Posts |
Posted - 2003-10-24 : 16:39:13
|
Another posibility is to not use a CmdExec but use SQLAgent or DTS to execute an ActiveX script. This would let you use the Scripting.FileSystemObject. Here is a sample:Dim objFSODim objFolderDim objFile Set objFSO = CreateObject("Scripting.FileSystemObject")Set objFolder = objFSO.GetFolder("d:\Data\Tax\ETRS\Archive\Oct__1_2003__8_52AM") For Each objFile In objFolder.Files objFile.Delete TrueNextSet objFile = NothingSet objFolder = NothingSet objFSO = NothingGood luck. |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-10-27 : 09:01:45
|
[code]master..xp_cmdshell 'DEL /Q d:\Data\Tax\ETRS\Archive\Oct__1_2003__8_52AM\*.* '[/code]WHAT A SCRUBThanks Tara/Nigel....Wow...talk about being lazy....Brett8-) |
|
|
MakeYourDaddyProud
184 Posts |
Posted - 2003-10-27 : 09:35:54
|
iapetus, it looks as though your method could be more easily done via the sp_OACreate method, since it's COM object and can easily be wrapped up into general purpose stored procedure code.My quids worth.Daniel Small MIAPwww.danielsmall.com IT Factoring |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-10-27 : 10:45:39
|
Thanks everyone....I prefer a backend solution...This is predicated on the fact that an archiving scenario is estableshed making directories based on getdate() and replacing the spaces with underbars.../*master..xp_cmdshell 'MD d:\Data\Tax\ETRS\Archive\Oct_24_1960__8_52AM'master..xp_cmdshell 'Dir d:\Data\Tax\ETRS\Archive\*.'master..xp_cmdshell 'Copy C:\AutoExec.bat d:\Data\Tax\ETRS\Archive\Oct_24_1960__8_52AM'master..xp_cmdshell 'Dir d:\Data\Tax\ETRS\Archive\Oct_24_1960__8_52AM\*.*'EXEC isp_Dir_Cleanup 'd:\Data\Tax\ETRS\Archive', 100master..xp_cmdshell 'Dir d:\Data\Tax\ETRS\Archive\*.'*/if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[isp_Dir_Cleanup]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)drop procedure [dbo].[isp_Dir_Cleanup]GOCREATE PROC isp_Dir_Cleanup @Filepath varchar(8000) , @ArchiveCutoff intASDeclare @error_out int, @Result_Count int, @Error_Message varchar(255), @Error_Type int, @Error_Loc int, @Comp_Code int , @Cmd varchar(8000), @Comp_msg varchar(7000), @Dir_Name varchar(8000)SET NOCOUNT ON BEGIN TRAN CREATE TABLE #Folder_Parsed ( Create_Time datetime NULL , File_Size int NULL , File_Name varchar (255) NULL ) If @Error_Out <> 0 BEGIN Select @Error_Loc = 1 Select @Error_Type = 50001 GOTO isp_Dir_Cleanup_Error END CREATE TABLE #Folder ( dir_output varchar (255) NULL ) If @Error_Out <> 0 BEGIN Select @Error_Loc = 2 Select @Error_Type = 50001 GOTO isp_Dir_Cleanup_Error ENDCOMMIT TRAN BEGIN TRAN SELECT @Cmd = 'Dir ' + @Filepath + '\*.' INSERT INTO #Folder EXEC master..xp_cmdshell @Cmd SELECT @Result_Count = @@ROWCOUNT, @error_out = @@error If @Error_Out <> 0 BEGIN Select @Error_Loc = 1 Select @Error_Type = 50001 GOTO isp_Dir_Cleanup_Error END Delete From #Folder_Parsed SELECT @Result_Count = @@ROWCOUNT, @error_out = @@error If @Error_Out <> 0 BEGIN Select @Error_Loc = 2 Select @Error_Type = 50001 GOTO isp_Dir_Cleanup_Error END DECLARE Dir CURSOR FOR SELECT Substring(dir_output,40,(Len(dir_output)-39)) As Dir_Name FROM #Folder WHERE ISDATE(REPLACE(Substring(dir_output,40,11),'_',' '))=1 AND DATEDIFF(ww,CONVERT(datetime, REPLACE(Substring(dir_output,40,11),'_',' ')),GetDate()) > @ArchiveCutoff AND Substring(dir_output,25,5) = '<DIR>' OPEN DIR FETCH NEXT FROM Dir INTO @Dir_Name WHILE @@FETCH_STATUS = 0 BEGIN SELECT @cmd = 'DEL /Q ' + @FilePath + '\' + @Dir_Name + '\*.*' SELECT @cmd EXEC master..xp_cmdshell @cmd SELECT @cmd = 'RD ' + @FilePath + '\' + @Dir_Name SELECT @cmd EXEC master..xp_cmdshell @cmd FETCH NEXT FROM Dir INTO @Dir_Name ENDCOMMIT TRAN BEGIN TRANDROP TABLE #FolderDROP TABLE #Folder_ParsedCOMMIT TRANSELECT @Comp_Code = 0isp_Dir_Cleanup_Exit:SET NOCOUNT OFFRETURN @Comp_Codeisp_Dir_Cleanup_Error:Rollback TRANSelect @Comp_Code = -1, @Comp_msg = @Error_MessageIf @Error_Type = 50001 BEGIN Select @error_message = (Select 'Location: ' + RTRIM(Convert(char(3),@Error_Loc)) + ' @@ERROR: ' + RTRIM(Convert(char(6),error)) + ' Severity: ' + RTRIM(Convert(char(3),severity)) + ' Message: ' + RTRIM(description) From master..sysmessages Where error = @error_out) ENDIf @Error_Type = 50002 BEGIN Select @Error_Message = 'Location: ' + RTRIM(Convert(char(3),@Error_Loc)) + ' Severity: UserLevel ' + ' Message: ' + RTRIM(@Error_Message) ENDRAISERROR @Error_Type @Error_MessageGOTO isp_Dir_Cleanup_ExitGO Brett8-) |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-10-27 : 12:15:01
|
I think you should test what effect the transactions have on that.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-10-27 : 12:51:51
|
quote: Originally posted by nr I think you should test what effect the transactions have on that.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy.
OK..I tested it before...albiet with 1 directory, and it seemed to work fine...Sounds like I should check it out and set up a trace...Are you saying isolating the DDL from the DML is a bad thing?Actually...now that I think about it...if I commit the creation and then fail, I'm screwed...Is that it?Brett8-) |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-10-27 : 19:55:40
|
Just that a transaction around all the xp_cmdshells won't do anything as they won't roll back.If you were logging what you executed then the transactions would cause a problem but here doesn't look like they serve any purpose.And why put a transaction round the creation of temp tables.Nothing wrong with it but doesn't seem much point - must admit I rely insert errors to detect creation failures.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2003-10-29 : 19:49:53
|
quote: Originally posted by SamC Would you post an example using the filescripting object?
/Q is clearly the solution here but for posterity's sakedeclare @cmd varchar(100), @Path varchar(200) , @FileName varchar(255)select @Path = 'C:\temp\' declare @objFSO intdeclare @i intdeclare @File varchar(1000)select @cmd = 'dir /B ' + @Pathcreate table #dir(files varchar(1000))insert #dir exec master..xp_cmdshell @cmdwhile exists (select files from #dir where files is not null)begin select @filename = max(files) from #dir select @File = @Path + @fileName exec sp_OACreate 'Scripting.FileSystemObject', @objFSO out exec sp_OAMethod @objFSO,'DeleteFile', null, @File exec sp_OADestroy @objFSO delete from #dir where files = @filenameenddrop table #dir |
|
|
|