@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]);
}
.net
?for
andif
commands.Robocopy
, can you use that? If so, check out the/MinAge
switch.