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.
| Author |
Topic |
|
rvs.suresh@hotmail.com
Starting Member
12 Posts |
Posted - 2009-03-11 : 07:40:59
|
| • how I can delete specific files using Stored Procedures.Two Machines CO1 and CO2.One (CO1) is Sql 2005 runing. Another (CO2) is Coding and files storing.I want runing Stored procedure that time delete CO2 system files. How can it possible.I am using following procedure declare @ImagePath varchar(100) DECLARE @cmd varchar(1000) select @ImagePath= PhotoPath from UserPhotoAlbumInfo WHERE UserAlbumInfoId = @UserAlbumInfoId SET @cmd = 'del "\\192.183.1.19\e$\webprojects\My\Files\Images\UserPhotoAlbum\' + @ImagePath +'"' EXEC master..xp_cmdshell @cmdThis procedure is not working. because,that CO2 admin username,password is i know. but where its given??? |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
darkdusky
Aged Yak Warrior
591 Posts |
Posted - 2009-03-11 : 08:47:33
|
| enabling xp_cmdshell and giving file access are both possible security weaknesses - especially when combined.You could achieve your aim quite easily using a vbs script file running under windows scheduler - it could run on a central server pointing to remote location, or run locally in target machine. E.g. using FSO you can access a folder's filename, datemodified, deletefile, copyfile etc. This way you can use Windows security to restrict access, it's faster, more secure, less complicated and puts no strain on SQL Server. |
 |
|
|
rvs.suresh@hotmail.com
Starting Member
12 Posts |
Posted - 2009-03-11 : 09:33:29
|
How can i do this methodPls. help me.quote: Originally posted by darkdusky enabling xp_cmdshell and giving file access are both possible security weaknesses - especially when combined.You could achieve your aim quite easily using a vbs script file running under windows scheduler - it could run on a central server pointing to remote location, or run locally in target machine. E.g. using FSO you can access a folder's filename, datemodified, deletefile, copyfile etc. This way you can use Windows security to restrict access, it's faster, more secure, less complicated and puts no strain on SQL Server.
|
 |
|
|
rvs.suresh@hotmail.com
Starting Member
12 Posts |
|
|
darkdusky
Aged Yak Warrior
591 Posts |
Posted - 2009-03-12 : 07:43:45
|
| You can create a vbscript file to query table using DMO and FileSystemObject to delete files etc.Save this as "delfiles.vbs" - any name ending with .vbs will do. It can be scheduled using windows scheduler. 'Start of script Option Explicit'************************************************************'USE DMO to retrieve value from table '*************************************************************' any used constants from the type library must be explicitly declaredConst SQLDMODataFile_TabDelimitedChar = 2Const SQLDMOBCPDataFile_Char = 1Dim oServer ' the SQL Server objectDim oDatabase ' the target database to useDim strTableSQL Dim num, strRowResult Dim QueryResult'Put SQL query here:strTableSQL = "SELECT top 1 * from sys.all_columns"Set oServer = CreateObject("SQLDMO.SQLServer")oServer.LoginSecure = True 'Set instance nameoServer.Connect "MySQLInstanceName" 'set DBSet oDatabase = oServer.Databases("MyDatabaseName")Set QueryResult = oServer.ExecuteWithResults(strTableSQL)'This can be used to build a list from different rows or columns For num = 1 To QueryResult.Rows strRowResult = QueryResult.GetColumnString(num, 1) 'Msgbox( strRowResult ) NextoServer.DisConnect '*****************************************************'Finished DMO'Use result of query to delete files'***************************************************** Dim FolderPath Dim LastModifiedDate Dim ErrorFound Dim msg , tempstring 'location of file to be deleted - adds result from query to path FolderPath = "C:\temp\test1\" & strRowResult Dim FSO Dim FolderFile , ThisFileName On Error Resume Next Set FSO = CreateObject("Scripting.FileSystemObject").GetFolder(FolderPath) Set fsoc = CreateObject("Scripting.FileSystemObject") For Each FolderFile In FSO.Files With FolderFile ThisFileName = .Name LastModifiedDate =DateValue(.DateLastModified) 'Msgbox(ThisFileName & " " & LastModifiedDate ) 'Check each file's properties: 'If need to check file dates or extension e.g.".doc" etc ' If (LastModifiedDate < now()-5) AND right(ThisFileName,4) = ".doc" Then 'fsoc.deletefile FolderPath & "\" & ThisFileName, TRUE ' End If 'If need to delete everything in the folder fsoc.deletefile FolderPath & "\*.*" , TRUE End With Next Set FSO = Nothing Set FolderFile = Nothing Set FolderSubFolder = Nothing Set fsoc = Nothing |
 |
|
|
|
|
|
|
|