How can I use FOR FILES to delete old backup files from a remote server

Home Forums Scripting General Scripting How can I use FOR FILES to delete old backup files from a remote server

This topic contains 5 replies, has 4 voices, and was last updated by Avatar jman642 4 years, 1 month ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • Avatar
    sql_lover
    Member
    #164958

    I do have the need of running a TSQL job from a remote server in order to delete old backup files. The command runs fine when running via command prompt, same remote server. But it fails when it’s being executed as an added step inside a TSQL job.

    Here is the screenshot of the TSQL job and step:

    PushD.png

    The job runs and gives no errors but it does not delete any file. Why?

    Avatar
    wullieb1
    Moderator
    #245084

    Re: How can I use FOR FILES to delete old backup files from a remote server

    Try changing the command to this:

    forfiles /s /m *DIFF*.sqb /d -1 /c “cmd /c del /q @patch”

    Avatar
    Ossian
    Moderator
    #190359

    Re: How can I use FOR FILES to delete old backup files from a remote server

    Should that be “@path” ?

    Avatar
    wullieb1
    Moderator
    #245091

    Re: How can I use FOR FILES to delete old backup files from a remote server

    Yep. Sorry my bad.

    Avatar
    wullieb1
    Moderator
    #245092

    Re: How can I use FOR FILES to delete old backup files from a remote server

    Ok had a little play around with this and this works for me

    pushd “ChangeThisToMeetYourRequirments” &&(forfiles /S /M *.txt /D -0 /C “cmd /c del /s /q @path”) & popd[/CODE]

    It has been tested on a share with text files created today, hence the /D -0

    This has all been done in cmd from my local system.

    I created a job on our SQL server and again it worked perfectly for me.

    I did notice however that your command is spread over a few lines. Min isn’t. Is that possibly an issue?[CODE]pushd “ChangeThisToMeetYourRequirments” &&(forfiles /S /M *.txt /D -0 /C “cmd /c del /s /q @path”) & popd[/CODE]

    It has been tested on a share with text files created today, hence the /D -0

    This has all been done in cmd from my local system.

    I created a job on our SQL server and again it worked perfectly for me.

    I did notice however that your command is spread over a few lines. Min isn’t. Is that possibly an issue?

    Avatar
    jman642
    Member
    #389131

    Sorry for the late reply, lol…

    This was my solution, which works like a charm:

    PushD “\server1.xx.comShareshare_server1” &&(“forfiles.exe” /S /M “*DIFF*.sqb” /d -1 /c “cmd /c del @file”) & PopD
    It runs on my SQL server via TSQL job against the backup server.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.