本文介绍了如何获得文件夹期限(天数从创建或修改日期数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个spesific路径需要找到具有名称MyData的所有文件夹。每内幕MYDATA需要得到的每一个文件夹(天数)的年龄。
这时如果年龄比N天更大,我将执行一组命令。
到现在我已经找到的所有文件夹迈德特和他们的最后修改日期的脚本是:

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
    ) 
)

从这里需要比较修改日期与当前日期以获得日龄。请你的帮助的感谢!
注释:请避免使用 FORFILES 我的机器有WIN-XP和 FORFILES 不存在和没将不起作用,即使之后你下载到C:\\ Windows \\ System32下。谢谢!

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!

推荐答案

@npocmaka要求我提供了一个解决这个问题。挑战:接受

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

简单的答案是使用 FORFILES 。运行WinXP是没有什么大的障碍 FORFILES 。这不是太困难的并将其复制到System32下没有世界期末。的注:Win2K系统资源 FORFILES 的套件版本有一点不同的语法 - 使用破折号,而不是斜杠的交换机和不同间距的参数。只需输入 FORFILES 不带参数的帮助画面。对于后人,如果链接 FORFILES 消失一年后的今天,<一个href=\"https://www.google.com/search?hl=en&lr=&q=intitle%3Aindex.of+%22forfiles.exe%22+-htm+-html+-php+-asp+%22Last%20Modified%22&btnG=Search&gws_rd=ssl\"相对=nofollow>尝试搜索开放目录。

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.

总之,使用 FORFILES 的Robocopy 真正削弱你的选择回避。这些都是它的,我能够执行日期数学意识到只有两个原生的命令。如果这些命令不可用,那么你就必须把无论是Windows脚本宿主或PowerShell的日期数学。这里有一个批次/ JScript的混合例子。 (另存为.bat文件)。

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]);
}

这篇关于如何获得文件夹期限(天数从创建或修改日期数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 07:48