Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Use "forfiles" to get today's files... (windows)
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Use "forfiles" to get today's files... (windows)

mehargagsmehargags Member
edited March 2016 in Help

Hi All,
I tried so many times but the syntax is so confusing I can't really get to understand forfiles feature on windows. My MS Sql 2008 dumps DB backup to folder D:/DB-Back

I need to :

1) copy "todays" files to another drive X:/Backup

2) Delete files older than 10 Days from "today"

which means every day it copies one set and deletes an older one, maintaing a set of total 10 Days. Can you please help me write this small script that I can put in scheduled task to run everyday ?

Thanks

Comments

  • Can't you simply use Windows Scheduler to schedule a copy D:\DB-Back\todaysfilename.db X:\Backup\todaysfilename.db (I'm sure that's not the right syntax as you probably have to pass it a path, but conceptually might be right.

    Forfiles example
    http://stackoverflow.com/questions/51054/batch-file-to-delete-files-older-than-n-days

  • mikhomikho Member, Host Rep
    edited March 2016

    FORFILES /p "C:\SQL_Backup" /s /m *.BAK /d -5 /c "CMD /C del /Q @FILE"

    Deletes *.BAK files older then 5 days in the C:\SQL_Backup folder.
    Use that to create your copy command. Or take a look at robocopy .....

    I use the above command in a batch file that I always use when installing MS SQL Express.

  • Hi @Mikho,
    Works Beautifully :)
    Can you double check my code to complete my scenario:

    1) Copy Today's files from C:\SQL_Backup to X:\Backup (preserving time stamps)
    robocopy "C:\SQL_Backup" "X:\Backup" /s /maxage:1

    2) Delete files older than 10 days from X:\Backup
    FORFILES /p "X:\Backup" /s /m *.BAK /d -15 /c "CMD /C del /Q @FILE"

    Now X:\Backup is synced with cloud backup storage so I always keeps last 15 days of backup here and on cloud. Any suggestions are duly welcome!

  • mikhomikho Member, Host Rep

    @mehargags

    Looks ok, only a few comments;

    • robocopy copies everything in that folder, while forfiles only deletes *.BAK files. You might wanna check that there are no other file types in that directory since they wont be deleted.

    X:, is that a mapped network drive? If you plan on running this using task Scheduler it might be a problem using mapped drives, better to use UNC paths in that case.

    You mention both 10 and 15 days, the forfiles command in your post deletes *.BAK files older then 15 days.

  • mehargagsmehargags Member
    edited March 2016

    @mikho said:

    • robocopy copies everything in that folder, while forfiles only deletes *.BAK files.

    Well there are only .BAK files in there...still corrected it to '*' for forfile! Thanks.

    X:, is that a mapped network drive?

    No its a Physical Hard drive on my dedi... we are good!

    You mention both 10 and 15 days

    Yup settled down with 15 days of backup now!!! :)

    Your help is unprecedented. Have been (intermittently) mugging my head up with forfiles shity syntax for months.

    Thanks for the inputs...as always!

Sign In or Register to comment.