代码之家  ›  专栏  ›  技术社区  ›  justaguy

要检查目录中新文件的批处理文件

  •  -1
  • justaguy  · 技术社区  · 7 年前

    在下面 batch 文件,我试图检查一个目录,其中将只包含文本文件(没有子目录),为新的文件添加。下面的脚本执行,但始终显示 New file detected . 最后,我将把它添加到startup菜单中,以便在登录时检查目录。也许有更好的方法,但我不太熟悉 batchfiles . 谢谢:)。

    批量

    @echo off
    :START
    cls
    set /a Old = 0
    set /a New = 0
    echo Checking for new annotated files...
    for /f "tokens=*" %%P IN ('dir "path/to/directory" /A /b') do (set /a Old += 1)
    set Old
    echo Checking for new files..
    for /f "tokens=*" %%P IN ('dir "path/to/directory" /A /b') do (set /a New += 1)
    set New
    goto COMPARE
    
    :COMPARE
    if %New% gtr %Old% goto NEWF (
    goto NEWF
    )
    else (
    goto OLDF
    )
    
    :NEWF
    echo New File Detected.
    echo.
    pause
    
    :OLDF
    echo Nothing New.
    echo.
    pause
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   John Kens    7 年前

    由于您正在查看目录中是否创建了任何新文件,因此可以使用 xcopy 检查之前的文件 x 日期。因为你没有在你的帖子上发布日期或时间,所以我假设它是当前日期。

    你不需要检查比给定日期年轻的文件,因为你不需要比较它们。您只需检查作为“New”返回的文件是否小于1。这可以通过以下方法完成:

    if %New% gtr 0 (goto NEWF) else (goto OLDF)
    

    如果您希望以后搜索子董事,可以使用 /S 切换 xcopy公司 /L /S /D: .

    对于pause语句,如果脚本要继续(因为它没有方向),您可能会遇到问题。要修复这一点,可以简单地使用脚本退出脚本。 goto :eof . 使用 echo( 而不是 echo.

    @ECHO OFF
    set /a New = 0
    
    :: Gather & edit the date for xcopy.
    SET CurrentDate=%date%
    SET CurrentDate=%CurrentDate:/=-%
    SET CurrentDate=%CurrentDate:* =% 
    
    :: Check for files created today.
    for /f "delims=" %%i in ('xcopy "path/to/directory" /L /I /D:%CurrentDate%') do (set /a New+=1)
    set /a New-=1
    
    goto COMPARE
    
    :COMPARE
    if %New% gtr 0 (goto NEWF) else (goto OLDF)
    
    :NEWF
    echo New File Detected.
    echo(
    pause
    goto :eof
    
    :OLDF
    echo Nothing New.
    echo(
    pause
    goto :eof
    
        2
  •  1
  •   SachaDee    7 年前

    将值存储在old.txt文件中。如果存在此文件,则在内部获取值并循环TrAL.T.TXT文件并测试该值。这里是新的代码:

    @echo off&cls
    setlocal enabledelayedexpansion
    
    :: Default value
    
    set "New=0"
    set "old=0"
    
    :: If exist old.txt get the real value 
    
    if exist old.txt set /p Old=<old.txt
    
    
    echo Checking for new annotated files...
    
    ::Count of the txt files
    
    for /f %%$ IN ('dir *.txt') do set /a New+=1
    
    :: Update the value of the old.txt for the next check
    
    echo !New!>old.txt
    
    :: Testing the 2 values
    
    if !New! gtr %Old% (goto NEWF) else (goto OLDF)
    
    :NEWF
    echo New File Detected.
    echo.
    pause
    exit/b
    
    :OLDF
    echo Nothing New.
    echo.
    pause
    exit/b
    
    推荐文章