Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: .bat file to delete certain files  (Read 3902 times)

0 Members and 1 Guest are viewing this topic.

iwoodland

    Topic Starter


    Newbie

    • Experience: Experienced
    • OS: Windows 7
    .bat file to delete certain files
    « on: January 03, 2017, 10:53:04 AM »
    Hi all - just wondering if someone has some coding i could use for a batch file in windows


    I have a back up folder and files automatically backup with a date and time at the end. Over time this becomes a long list. Can someone share some coding for a batch file that once there is more than 2 of a specific file name [taking into account the file name will have successive dates and times] and then delete them leaving only the 2 latest files of each of the different files in that folder. Only needs to work when i run the batch.

    e.g. keep The 2 latest
    Apple 2017-01-01 21.30.xls
    Apple 2017-01-01 21.59.xls
    Apple 2017-01-02 21.10.xls
    Apple 2017-01-03 21.150.xls

    and keep the 2 latest
    Water 2017-01-01 21.30.mdb
    Water 2017-01-01 21.59.mdb
    Water 2017-01-02 21.10.mdb
    Water 2017-01-03 21.15.mdb

    and so on....


    Many thanks in anticipation


    Ian

    I have this coding - but it deletes all but the last 4 files in total - so it's not quite there yet:
    pushd "C:\Users\ian.woodland\Documents\irw Bin" && ( for /f "skip=4 eol=: delims=" %%F in ('dir /b /o-d /tw /a-d') do del "%%F")

    DaveLembke



      Sage
    • Thanked: 662
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: .bat file to delete certain files
    « Reply #1 on: January 03, 2017, 01:31:07 PM »
    I myself have taken the easy route with this myself. I shuffle backup files down a path to eventual deletion. Example Being Folder 1, Folder 2, and Folder 3.

    Folder 1 is populated with the latest backup
    Folder 2 is populated with backup that is up to 2 days old
    Folder 3 is the waste bin where all files that end up here get deleted

    example being:
    Code: [Select]
    echo off
    cls
    @echo. Shuffling Backups and Deleting the 3rd day
    @echo.
    erase c:\backups\folder3\*.* /q
    xcopy c:\backups\folder2\*.* c:\backups\folder3\*.*
    erase c:\backups\folder2\*.* /q
    xcopy c:\backups\folder1\*.* c:\backups\folder2\*.*
    erase c:\backups\folder1\*.* /q
    @echo.
    @echo. Backup Shuffle Complete
    pause

    You will want to run this shuffle batch prior to your actual backup of latest data to folder1. This way all data is shuffled down making folder1 empty waiting the latest data after each run of this. *Note: Use of xcopy and wildcards is because I use this myself for multiple file types.

    If you dont want it to shuffle as a wildcard anything contained within, you can shuffle just the mdb and xls files.

    Warning: It is assumed when running this batch that no other important data is at the backup location such as Folder1 as for if the application or program is located in the same location as the backups such as with my example at Folder 1, it will rip the data from its home location and pass it down stream to Folder 2. This one line here is very dangerous if for example your program or application resided at the same location as your backups. If this is the case then the backup location needs to be changed to protect from moving the program from where it should remain. erase c:\backups\folder1\*.* /q

    Code: [Select]
    echo off
    cls
    @echo. Shuffling Backups and Deleting the 3rd day
    @echo.
    erase c:\backups\folder3\*.* /q
    xcopy c:\backups\folder2\*.mdb c:\backups\folder3\*.mdb
    xcopy c:\backups\folder2\*.xls c:\backups\folder3\*.xls
    erase c:\backups\folder2\*.* /q
    xcopy c:\backups\folder1\*.mdb c:\backups\folder2\*.mdb
    xcopy c:\backups\folder1\*.xls c:\backups\folder2\*.xls
    erase c:\backups\folder1\*.* /q
    @echo.
    @echo. Backup Shuffle Complete
    pause

    *Last Important Note... You actually have 3 days of backup before the batch is run again. The waste cleanup of Folder 3 doesnt happen until the next run of this batch. So after you have run this and data is moved down from folder 2 to 3 ... 1 to 2 and then you run your backup for that day into the was empty Folder 1. You now have Folder 1, 2, and 3 with backup files. Until this batch is run in which Folder 3 is wiped clean and only 2 days move from folder to folder.

    Reason why I did not use the MOVE command and decided to use XCOPY is because if this process was disrupted say a blackout. The MOVE command is less graceful to disruption whereas I have been told that XCOPY is graceful to interruption because its making a copy of the data to another destination vs moving the data bit for bit which if interrupted spells disaster. Maybe one of the batch gurus here can confirm that rumor. If using the MOVE command alone, this could be shortened as for the only erase would need to happen at Folder 3 and the MOVE command would move data between Folders 2 to 3 and 1 to 2.

    iwoodland

      Topic Starter


      Newbie

      • Experience: Experienced
      • OS: Windows 7
      Re: .bat file to delete certain files
      « Reply #2 on: January 03, 2017, 02:50:13 PM »
      hi Dave - thanks so much for taking the time to reply. Since i posted earlier, i decided that there was an easier way. I split into folders my backups and then have several batch files that delete all but 2 of the latest files from each folder. All the batch files run from just one single batch file that i'll get windows to run.

      Much appreciate your suggestion and i'll keep it if my way doesn't work out

      All the best

      Ian

      DaveLembke



        Sage
      • Thanked: 662
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: .bat file to delete certain files
      « Reply #3 on: January 04, 2017, 10:10:35 AM »
      Hello Ian,

      Great to hear you got it to work. As per you stated using multiple batch files. Just wanted to mention that most of the time batches can be combined that is that you can merge batched instructions so that say you have 4 batches combined into 1, whatever needs to run first is at the top of the batch file, then the next batch to run you can copy paste that info from that batch file below the other batch files instructions, and do the same for adding contents from all other batches into that first back file. Order of Operation/Execution is what is most important. Batches are like a linear checklist of go down the line and do this and do that with ability to jump around with goto statements usually used only for when specific instructions are met or to start the batch over from scratch in an endless loop until CTRL+C or exiting the window and FOR loop instructions with parsing and other tasks.

      But there are other times as well when you want functions/instructions to not run all the time as part of following other processes and for that keeping them separate makes sense.  :)