SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


delete files before 8PM


delete files before 8PM

Author
Message
Zeal-DBA
Zeal-DBA
Old Hand
Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)

Group: General Forum Members
Points: 337 Visits: 1125
hello experts,
i have some error in using "sp_delete_file" Stored procedure. so looking some other option or script to delete files before 8 PM. daily. if some one can design such script please help.
Bhuvnesh
Bhuvnesh
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5188 Visits: 4076
Zeal-DBA (10/7/2013)
hello experts,
i have some error in using "sp_delete_file" Stored procedure. so looking some other option or script to delete files before 8 PM. daily. if some one can design such script please help.


YOu can use maintenance plan wizard
OR
By command shell too see link http://www.sevenforums.com/tutorials/55728-file-delete-command-prompt.html

-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
Zeal-DBA
Zeal-DBA
Old Hand
Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)

Group: General Forum Members
Points: 337 Visits: 1125
hi,
maintainenace plan itself uses internally same SP "XP_delete_file", i tried using XP_cmdshell 'forefile...

but it does not facilitate to provide time or hours... only days can be defined.
how to define time or 5 hour before in DEL\Forfiles commands
Bhuvnesh
Bhuvnesh
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5188 Visits: 4076
Zeal-DBA (10/7/2013)
but it does not facilitate to provide time or hours... only days can be defined.
how to define time or 5 hour before in DEL\Forfiles commands
i am not comfotable in command shell on your requirement but yes this can be achieve easilt in Tsql by using " DATEPART function or waitfor delay

-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
schleep
schleep
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1008 Visits: 1326
Look up the powershell commands get-childitem and remove-item, and you should be well on your way.

You may even find a script to do what you want.

Get back to us if you need more help once you've tried it yourself.



Zeal-DBA
Zeal-DBA
Old Hand
Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)

Group: General Forum Members
Points: 337 Visits: 1125
actually i am quite new on this powershell scripting, it seems quite complex on this stage. so i have got following script and modified in my ways replaced hours in place of days. when i executed the same it gave me some } error.

DECLARE @pscmd varchar(1000)
DECLARE @targetpath varchar(8000)
DECLARE @olddays int
DECLARE @extension varchar(5)
DECLARE @cmdstring varchar(1000)
--assigning value to parameters, you can customize as per your need
SET @targetpath = 'D:\Dtest\'
SET @olddays = -6 --pass the Hours with negative values
SET @extension = 'txt'
SET @pscmd = '"& '+
'Get-ChildItem ' + Quotename(@targetpath,'''') + ' | '+
'where {$_.lastWriteTime -lt ((Get-Date).AddHours('+ CAST(@olddays as varchar) +'))
-and ($_.Extension -match ' + QUOTENAME(@extension,'''')+ ') } | ' +
'Remove-Item -force " '
SET @cmdstring = ' ""powershell.exe" '+ @pscmd
exec master..xp_cmdshell @cmdstring
schleep
schleep
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1008 Visits: 1326
What's the error you're getting?



Zeal-DBA
Zeal-DBA
Old Hand
Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)

Group: General Forum Members
Points: 337 Visits: 1125
i printed the Tsql fron @cmddtring variable which is actually getting executed at last, kindly find the printed or actual executed text of @cmdstring below, i have not added any double quote just pasting same what i am getting.

""powershell.exe" "& Get-ChildItem 'D:\Dtest\' | where {$_.lastWriteTime -lt ((Get-Date).AddHours(-6))
-and ($_.Extension -match 'txt') } | Remove-Item -force "



below is the error :

Missing closing '}' in statement block.
At line:1 char:83
+ & Get-ChildItem 'D:\Dtest\' | where {$_.lastWriteTime -lt ((Get-Date).AddHour
s(-6)) <<<<
NULL
schleep
schleep
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1008 Visits: 1326
You were very close: this needs to be in brackets:

($_.lastWriteTime -lt ((Get-Date).AddHours(-6)))



ALZDBA
ALZDBA
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12051 Visits: 8923
cls
$Path = 'c:\temp\'
$RefTs = ([datetime](Get-Date -Format 'yyyy-MM-dd')).AddHours(20) # 8PM
$RefTs
# select and delete files having last write time before RefTs
get-childitem -Path $Path -Filter '*.txt' -Recurse | Where-Object { $_.PSIsContainer -eq $false -and $_.lastwritetimeutc -lt $RefTs } | Remove-Item -Force


Johan


Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere w00t

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


press F1 for solution, press shift+F1 for urgent solution :-D


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me Alien but most of the time this is me Hehe
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search