批处理脚本在30天以内的目录中显示文件名?

时间:2014-11-11 11:07:53

标签: batch-file

@echo off
set dir_path="<path>"

rem *********set min age of files and folders to be  displayed**********
set max_days= 30

rem *********display FILE names from %dir_path%*********

dir %dir_path% -m *.* -d -%max_days% /-p /o:gn > "C:\Users\Desktop\DirContents.txt"

exit


**************************************************************************************************

在上面的脚本中,我可以显示特定目录中的整个文件和文件夹列表。但不是超过30天的文件或文件夹。请帮忙

3 个答案:

答案 0 :(得分:3)

forfiles /d -30 /c "cmd /c echo @path is older than 30 days."

答案 1 :(得分:1)

robocopy %dir_path% %dir_path% /L /V /MINAGE:30 /NJH /NJS /LOG:C:\Users\Desktop\DirContents.txt

这将仅在%dir_path%上运行robocopy。

/L only outputs the output of the command but doesn't move anything
/V enables verbose mode, which allows the filenames to be printed
/MINAGE:30 only returns files and folders older than 30 days
/NJH prevents the job header from being printed
/NJS prevents the job summary from being printed
/LOG:C:\Users\Desktop\DirContents.txt outputs the result to a text file

答案 2 :(得分:0)

使用jscript / bat hybrid的另一种方法(应保存为.bat):

@if (@X)==(@Y) @end /* jscript comment

@echo off
    cscript //E:JScript //nologo "%~f0"
exit /b
******************    end comment  */

var objFSO  = new ActiveXObject("Scripting.FileSystemObject");

//var objStartFolder = "C:\Scripts";


var curr_ms = (new Date()).getTime();
var _30days_to_ms=1000*60*60*24*30;

ShowSubFolders(".");

function ShowSubFolders (folder) {
    var objFolder = objFSO.GetFolder(folder);
    var colFiles = objFolder.Files;
    var colFolders = objFolder.SubFolders;

    for(var objEnum = new Enumerator(colFiles); !objEnum.atEnd(); objEnum.moveNext()) {
       strFileName = objEnum.item();

       var fileDate_ms =(new Date(strFileName.DateCreated)).getTime();
       if (curr_ms - fileDate_ms > _30days_to_ms ) {
         WScript.Echo(strFileName);
       }


    }

    for(var objEnum = new Enumerator(colFolders); !objEnum.atEnd(); objEnum.moveNext()) {
        ShowSubFolders(objEnum.item());
    }
}

使用此功能,您可以使用更详细的测量值(如秒,分钟,周......)过滤文件。