1
REM Exclude file types for archiving
REM set EXCLUDE_FILES_TYPES=-x!*.rar -x!*.zip -x!*.7z
set EXCLUDE_FILES_TYPES=-xr!*.zip 

REM Delete files after archiving
set DELETE_AFTER_ARCHIVE=-sdel

REM Archive files only older then days
REM set ARCHIVE_FILE_OLDER_THEN=-to5d
set ARCHIVE_FILE_OLDER_THEN=-5

set EXTRA_PARAM=%EXCLUDE_FILES_TYPES% %DELETE_AFTER_ARCHIVE% %ARCHIVE_FILE_OLDER_THEN%


REM ------------------------------------------------------------------------------------


REM Path to WinRAR executable in Program Files
set path="C:\Program Files\7-Zip";%path%
REM $Env:Path = $Env:Path + ";C:\Program Files\7-Zip"

REM Set your WinRAR installation path for local testing
REM set path="C:\Program Files\7-Zip";%path%


REM Replace space in hour with zero if it's less than 10
SET hr=%time:~0,2%
IF %hr% lss 10 SET hr=0%hr:~1,1%

REM This sets the date like this: mm-dd-yr-hrminsecs1/100secs
Set TODAY=%date:~4,2%-%date:~7,2%-%date:~10,4%-%hr%%time:~3,2%%time:~6,2%%time:~9,2%


IF EXIST %EDI832_US_ARCHIVE_DIR% (
    cd %EDI832_US_ARCHIVE_DIR%
    7z a "%EDI832_US_FILE%_%TODAY%" %EXTRA_PARAM%
DavidPostill
  • 156,873

2 Answers2

1

How do I archive files older then 5 days with 7-Zip?

Unfortunately, 7-Zip currently doesn't have an option to filter files by age/date (as far as I am aware).

However, based on the link given in the comments by @DavidPostill suggesting the Windows forfiles utility, you may be able to use something similar to:

@REM Set the file list name used with 7-Zip
set FILE_LIST=file_list.txt

    if exist %EDI832_US_ARCHIVE_DIR% (

        cd %EDI832_US_ARCHIVE_DIR%

        @REM 7z a %EDI832_US_FILE%_%TODAY% %EXTRA_PARAM%

        @REM forfiles /s /m *.* /d -5 /c "cmd /v /c set newpath=@RELPATH && echo !newpath:.\=!" > file_list.txt
        forfiles /s /m *.* /d %ARCHIVE_FILE_OLDER_THEN% /c "cmd /v /c set newpath=@RELPATH && echo !newpath:.\=!" > %FILE_LIST%

        @REM 7z a %EDI832_US_FILE%_%TODAY% %EXTRA_PARAM% @file_list.txt
        7z a %EDI832_US_FILE%_%TODAY% %EXTRA_PARAM% @%FILE_LIST%

        @REM del file_list.txt
        @REM del %FILE_LIST%

        )

In this case, the forfiles example above should generate a list of files (> file_list.txt) older than 5 days with full paths to each file in the format:

"filename.ext" 
"subfolder\filename.ext"

which can then be passed to 7zip as ex. @file_list.txt.


forfiles Example Command

Breaking down the example forfiles command options:

  • /s means recurse subdirectories.

  • /m is the file mask (in this example, this includes all files and extensions via *.*).

  • /d sets the age of files to include/exclude (based on their last modified dates). The example above will include files older than 5 days (-5). The current day is included in this calculation, so files up to 4 days prior aren't considered (i.e. won't be added to this list).

  • /c specifies the command to run on each file. The command:

    cmd /v /c set newpath=@RELPATH && echo !newpath:.\=!
    

    works as follows:

    • cmd /v /c runs a new instance of cmd (the Windows command prompt) with delayed expansion enabled (/v) and closes that instance of cmd immediately after finishing the command (/c).

    • set newpath=@RELPATH creates a temporary variable called newpath and sets it to the value @RELPATH. @RELPATH is a relative path to a file created automatically by forfiles in the form ex. ".\filename.ext" (where .\ means the current folder).

    • echo !newpath:.\=! prints a modified version of newpath. The :.\=! portion attached to newpath is actually a DOS string variable manipulation which says to replace all instance of .\ with nothing (=). The ! signs are used in place of % to reference variables when delayed expansion is enabled (so ex. !newpath! instead of %newpath%).

    • && simply allows the two separate set and echo commands above to be placed/executed on one line.


Notes

  • newpath can be called anything you wish.

  • 7-Zip's -x and -sdel options should be honored by 7-Zip, even when using a file list.

  • The command to set/use a variable in one line was adapted from an answer to this Super User question.

Caveats

  • .\ needs to be stripped from the relative paths passed to 7-Zip in order to maintain a relative directory structure. Otherwise, leaving .\ in causes 7-Zip to "flatten" everything by placing all the the zipped files in the root of the archive (i.e. no subfolders).

  • When reading from a list generated by forfiles, some character differences may cause 7-Zip to skip files. For instance, using instead of ' in a file name may result in a "file not found" warning (and thus these files won't be added to a given archive).

  • Using cmd /c with forfiles adds a bit of overhead to processing each file (compared to running commands directly without cmd) but bypasses any potential bugs where a command might fail.

  • Using double quotes with /c "cmd /c [...]" is necessary to prevent forfiles from complaining about using /c twice.

  • The Super User question linked above notes that using cmd /c /v (rather than cmd /v /c) likely will not work.

  • The forfiles command given was tested on Windows 7. However, in the same Super User question already linked, one comment reported an issue with combining similar commands on Windows 10 (version 1607), apparently due to a trailing space.

  • In context, it seems unlikely the "trailing space" issue noted above would be relevant here, but if so, you could either try using "internal quotes" (as suggested in the linked question) or just use "cmd /c echo @RELPATH" in forfiles instead and use another command line utility to clean .\ (and any other undesirable characters) from each line in file_list.txt.

  • You can add files individually to an archive with forfiles (bypassing the need for ex. file_list.txt) but this is a much slower process (relatively speaking), as the archive will typically need to be decompressed/recompressed repeatedly for each file added.

Anaksunaman
  • 17,239
  • this is awesome .thanks for the detail explanation .let me see how i can use forfiles command. many thanks ..i really apperciate it – femalesoftwareengineer Apr 20 '20 at 12:56
  • 1
    Hi Man, this working perfectly and i have tested that .. The explanation pretty clear and very easy to understand.Your expertise and enthusiasm were both appreciated for guiding and helping me with great explanation – femalesoftwareengineer Apr 20 '20 at 13:08
  • Your welcome. I am glad this was helpful to you. =) – Anaksunaman Apr 20 '20 at 18:47
  • Hi @Anaksunaman Do i need delete the file after the archive , i have seen its comment out ? REM del file_list.txt REM del %FILE_LIST% – femalesoftwareengineer Apr 21 '20 at 05:17
  • "Do I need to delete the file after the archive?" - No, you don't. file_list.txt should be overwritten by forfiles each run (meaning there should be no problem with leaving file_list.txt in place). Those lines are intended as completely optional examples. Many scripts will clean up temporary files once completed. Using either of those lines is just a matter of preference (in case you may not want extra files lying around after you archive everything). Obviously, you only need one to delete e.g.file_list.txt but they both can be removed if you desire. – Anaksunaman Apr 21 '20 at 18:08
1

/d %ARCHIVE_FILE_OLDER_THEN%
/d -%ARCHIVE_FILE_OLDER_THEN%

  • Hello, please provide more information within your answers, it definitely shouldn't be just code/commands, unexplained in any way. – Destroy666 Jun 30 '23 at 21:34