YWATFA
YWATFA

Reputation: 91

How to get Folder age (The Number of Days from creation or modification date)

Under a spesific path need to find all folders that have the name "MyData". Inside every "Mydata" need to get the age of every folder (days). Then I will perform a set of commands if the age is greater than N days. Till now I already found all the folders "MyData" and their last modification date, the script is:

for /f "delims=" %%a in ('dir /b /s /a:d "C:\Mypath\" ^|findstr /e /i "\Mydata"') do (
    for /f "delims=" %%k in ('dir /b /s /a:d "%%~a\"') do (
            echo %%k
    for /d %%a in ("%%k") do echo Modified date: %%~ta
    ) 
)

from here need to compare the modified date with the current date to get the age in days. Please your help thanks! Comment: Please avoid using forfiles My machine has WIN-XP and forfiles doesn't exist and didn't work even after dowloading it to C:\windows\system32. Thanks!

Upvotes: 2

Views: 1295

Answers (2)

npocmaka
npocmaka

Reputation: 57252

Here's a jscript/bat hybrid that can be used for more common cases when files should be filtered by date/time (you can save it with whatever name you want). To show folders created 5 days ago use :

FileTimeFilterJS.bat "." -dd -5 -filetime created -show dirs

to show directories modified 20 days ago use:

FileTimeFilterJS.bat "." -dd -20 -filetime modified -show dirs

Upvotes: 1

rojo
rojo

Reputation: 24466

@npocmaka asked me to provide a solution to this question. Challenge: accepted!

The simple answer is to use forfiles. Running WinXP is no big hindrance to forfiles. It's not too difficult to find forfiles.exe and copy it into System32 without the world ending. Note: The Win2K res kit version of forfiles has a bit different syntax -- the switches using dashes instead of slashes, and different spacing for arguments. Just enter forfiles with no arguments for the help screen. For posterity, if that link to forfiles disappears a year from now, try searching open directories.

Anyway, avoiding using forfiles and robocopy really cripples your options. Those are the only two native commands of which I'm aware capable of performing date math. If those commands are not available, then you'll have to turn either to Windows Script Host or PowerShell for date math. Here's a batch / JScript hybrid example. (Save this as a .bat file.)

@if (@CodeSection==@Batch) @then

@echo off
setlocal

set "age=5"
pushd "C:\MyPath"

for /d %%a in (*MyData*) do (
    for /d %%I in ("%%~a\*") do (
        call :getAge modified created accessed "%%~fI"
        setlocal enabledelayedexpansion

        echo %%~nxI was created !created! days ago.
        echo %%~nxI was modified !modified! days ago.
        echo %%~nxI was last accessed !accessed! days ago.

        if "!created!" gtr "%age%" (
            rem // do stuff to "%%~fI" if folder created > 5 days ago
        ) else if "!modified!" gtr "%age%" (
            rem // do something else to "%%~fI" if folder modified > 5 days ago
        )
        endlocal
    )
)

popd
rem // end main runtime
goto :EOF

:getAge <modified_var> <created_var> <accessed_var> <file|folder>
setlocal
for /f %%I in ('cscript /nologo /e:JScript "%~f0" "%~4"') do set "%%I"
endlocal & set "%~1=%modified%" & set "%~2=%created%" & set "%~3=%accessed%"
goto :EOF

rem // end batch portion / begin JScript
@end

var fso = new ActiveXObject("scripting.filesystemobject"),
    arg = WSH.Arguments(0),
    file = fso.FileExists(arg) ? fso.GetFile(arg) : fso.GetFolder(arg);

var props = {
    modified: new Date() - file.DateLastModified,
    created: new Date() - file.DateCreated,
    accessed: new Date() - file.DateLastAccessed
}

for (var i in props) {
    // age in days
    props[i] = Math.floor(props[i] / 1000 / 60 / 60 / 24);
    WSH.Echo(i + '=' + props[i]);
}

Upvotes: 3

Related Questions