Search code examples
windowsfor-loopbatch-filecmdphp-ziparchive

batch script to identify folders and zip & move them


I am a total newbie at windows scripting having spent over 13 yrs scripting in Unix shell. I need help in finding a way to find a folder last modified about 30 days back and then zip the folder and move it to another "Archive" folder. Folder structure is like the following :

D:\AuditFiles\2013.05
D:\AuditFiles\2013.06
D:\AuditFiles\Archive

So aim is to move 2013.05 into Archive directory after zipping it up.

I've tried forfiles.exe which doesnt allow me to skip the folder Archive and I've tried dir /ad but that doesnt allow me to specific last modified days been +30.


Solution

  • This is one method to get the oldest folder, excluding Archive, which seems to be what you need.

    @echo off
    pushd "D:\AuditFiles\"
    for /f "delims=" %%a in ('dir /b /ad /o-d') do (
    if /i not "%%a"=="Archive" set "folder=%%a"
    )
    echo oldest folder is "%folder%"
    popd