1

I will have a variable that will be equal to anything from 1-7, which will represent 1 day old and so on. I need to find a file that is the same age in days as that variable.

For example

set "age=4" // code to find the file OR folder that is 4 days old

It's worth noting that I need to get the filename of this file

1
  • Nothing, I'm unsure how to do this. Could find any good resources that have a similar example or usage Dec 15, 2014 at 19:36

3 Answers 3

1

Here, try this:

@echo off
setlocal

set age=4
set /a maxage=age+1

:: For files that are %age% days old or older, set a variable
for /f "delims=" %%I in ('forfiles /d -%age%') do (
    set "fileArr[%%~snxI]=%%~fI"
)

:: for files that are %age% + 1 days old or older, unset variable
for /f "delims=" %%I in ('forfiles /d -%maxage%') do (
    set "fileArr[%%~snxI]="
)

:: Get values of all remaining fileArr variables
for /f "tokens=1,* delims==" %%I in ('set fileArr') do echo(%%J

As you can see from the comments, first the script gets all files %age% days old or older, and sets a variable. To make the variable name unique and to be able to index it later, the variable name contains the short filename, simulating an associative array. Next, it unsets those variables which are %age% + 1 days old or older, then outputs what's left.

No painful date math is needed. forfiles handles the computation. If you needed age-by-minutes computation, admittedly that gets a little more complicated; but forfiles handles math by day just fine.

1

I found a much easier soloution:

ROBOCOPY "D:\file-backups\%ThisUser_Username%\Server"^
         "%ThisService_WorkingDirectory%@DayZ_Epoch_Server\Addons"^
         "dayz_server_*.pbo"^
         /minage:%ThisService_filerestoreage%^
         /maxage:%ThisService_filerestoreage%
1
  • Nice one! I edited your code as a code block and added line continuation and indents to better show the various arguments. Your original was a bit ambiguous because quotes were not used. I believe I preserved your intent, but double check.
    – dbenham
    Dec 15, 2014 at 21:32
0

In batch, as soon as you get the date of modification which corresponds to the age, (thus %DATE% - %AGE%), you can use the ~t modifier in a parameter expansion to get the age of the file.

Note that if you have PowerShell on your system (and you really should because it's preinstalled since Win7, cf comments) you can use it to do the date calculation easily.

Hence, a script that does what you want (with the parameter being %age% which you can replace with a command-line parameter for example) :

set age=4
for /f "delims=|" %%i in ('powershell.exe -Command "Get-Date  -Format dd/MM/yyyy -Date ([DateTime]::Today.AddDays(-%age%))"') do set age_date=%%i

echo Files modified %age% days ago (the %age_date%) are :

setlocal ENABLEDELAYEDEXPANSION

for /f "delims=|" %%f in ('dir /b') do (
    set file_date=%%~tf
    if "!file_date:~0,10!"=="%age_date%" (echo %%f)
)

For a pure-batch script to get the date to replace the powershell call (say for legacy versions), you could slightly modifiy the code from here : http://www.powercram.com/2010/07/get-yesterdays-date-in-ms-dos-batch.html , even though that would get very ugly.


In contrast, unix-like systems have a tool for this, see man find, which is more user-friendly.

find /path/to/parent/directory -daystart -atime $age 
  • atime : File was last accessed n*24 hours ago
  • $age without leading + or - : find exactly age of $age*24 hours (as opposed to less or equal, or superior or equal)
  • daystart : Measure times (for -amin, -atime, -cmin, -ctime, -mmin, and -mtime) from the beginning of today rather than from 24 hours ago

You can use mtime instead of atime for modified date instead of creation date.

1
  • All versions of Windows since 7 have Powershell preinstalled. Dec 15, 2014 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.