2

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!

6
  • Do you have installed .net ?
    – npocmaka
    Commented Feb 8, 2015 at 12:41
  • @npocmaka - NO. I avoid installing any thing on my machine because It is sensitive and old and it runs an operating system to an important unique production tool. So this problem should be solved by regular for and if commands.
    – YWATFA
    Commented Feb 8, 2015 at 13:25
  • You are boxing yourself in a bit here - what about Robocopy, can you use that? If so, check out the /MinAge switch. Commented Feb 8, 2015 at 13:57
  • For all others searching for a solution read first How to delete all directories (and contents) older than N days without usage of forfiles? also asked by YWATFA.
    – Mofi
    Commented Feb 8, 2015 at 20:11
  • Did you a simple Google search for File age in days? A lot of usable tips, e.g. at DosTips
    – JosefZ
    Commented Feb 9, 2015 at 8:01

2 Answers 2

3

@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]);
}
1
  • nice.answer is more detailed than the question.
    – npocmaka
    Commented Feb 10, 2015 at 4:13
1

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

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.