delete files before 8PM

  • 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.

  • 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;-)

  • 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

  • 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;-)

  • 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.

  • 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

  • What's the error you're getting?

  • 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

  • You were very close: this needs to be in brackets:

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

  • 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

    Learn to play, play to learn !

    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[/url]

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

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

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

  • last time i have excecuted it by removing one (") quote, thought its a multiple " quote issue. and script is executing since yesterday it was not even getting killed, and in task manager i found powershell,exe is runing, should i stop tht exe before executing this modified script?

    and please confirm will this script delete files recursively (means in the given path as well as in subdirectories), if not than what to edit so that script will start removing files in subdirectoris as well..

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply