使用批处理脚本检测丢失的文件 [英] Detecting missing file using batch script

查看:63
本文介绍了使用批处理脚本检测丢失的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写批处理脚本以从Windows上的文件列表中检测丢失的文件.给定文件名的格式为"day_month_date_hh_mm_00_yyyy.enf",并且文件的时间将不同,我必须确定是否缺少特定时间的文件. 我写下了以下内容,以找出给定年份和月份的天数.

I am trying to write a batch script to detect for missing files from a list of files on Windows. Given the format of the file names to be "day_month_date_hh_mm_00_yyyy.enf", and the hours of the files will be different, I have to identify if there's a file of a particular hour is missing. I have written down the following to find out the number of days in a given year and month.

set /p m="Enter month: "
set /p y="Enter year: "

REM call :DaysOfMonth %y% %m%
setlocal DisableDelayedExpansion
set /a "yy = %y%, mm = 100%m% %% 100"
set /a "n = 30 + !(((mm & 9) + 6) %% 7) + !(mm ^ 2) * (!(yy %% 4) - !(yy %% 100) + !(yy %% 400) - 2)"
set maxDay=%n%
echo Days Of Month: %maxDay%

但是,当我运行循环以检查丢失的文件时,我不知道如何生成日期.我已经在网上进行了一些搜索,并结合了一些代码,这些代码肯定无法正常工作,因为我还是一批新手.以下是我需要坚持的脚本部分,需要帮助.

However, I have no idea how to generate the Day of a date when I run the loop to check for missing files. I have search abit on the net and combined a few codes which will definitely not work as I am still very new to batch. Below is the part of the script where I am stuck at and need help on.

for /L %%d in (1,1,%maxDay%) do (
    for /L %%f in (0,1,%max%) do (
    if %%f < 10 (
        set hour=0%%f
    ) else (
        set hour=%%f
    )
        set "num=%%Day_%m%_%%d_%hour%_00_00_%y%"    
        if not exist "num.enf" (
            echo num.enf
            set /A "cnt=!cnt!+1"
        )
    )
)
NUMBER MISSING: !cnt!

完整脚本如下:

@echo off

set max=24
set cnt=0

set /p m="Enter month: "
set /p y="Enter year: "

setlocal DisableDelayedExpansion
set /a "yy = %y%, mm = 100%m% %% 100"
set /a "n = 30 + !(((mm & 9) + 6) %% 7) + !(mm ^ 2) * (!(yy %% 4) - !(yy %% 100) + !(yy %% 400) - 2)"
set maxDay=%n%
echo Days Of Month: %maxDay%

setlocal ENABLEDELAYEDEXPANSION

pause

for /L %%d in (1,1,%maxDay%) do (
    for /L %%f in (0,1,%max%) do (
    if %%f < 10 (
        set hour=0%%f
    ) else (
        set hour=%%f
    )
        set "num=%%Day_%m%_%%d_%hour%_00_00_%y%"    
        if not exist "num.enf" (
            echo num.enf
            set /A "cnt=!cnt!+1"
        )
    )
)
NUMBER MISSING: !cnt!

endlocal &exit /b %n%

请帮助我告诉我如何纠正脚本中的错误以及如何在循环中生成指定日期的日期以识别丢失的文件.预先感谢!

Please help advise me how do I rectify the errors in the script and how to generate the day of the specified date in the loop to identify missing file. Thanks in advance!

推荐答案

好,所以我刚刚对您的代码进行了一些修改,以使其正常工作.这不是最快的,因为我们通过运行powershell命令来获取每个文件的日期,但是它将完成此工作.

ok, so I just made some amendments to your code to get this to work. It is not the fastest as we get the date for each file by running a powershell command, but it will do the job.

我敢肯定,我可以写得更快更好一些,但是我需要花些时间.

I am sure I can write something a little better and quicker, but I would need to find some time.

@echo off
set max=24
set cnt=0
set /p m="Enter month: "
set /p y="Enter year: "

set /a "yy = %y%, mm = 100%m% %% 100"
set /a "n = 30 + !(((mm & 9) + 6) %% 7) + !(mm ^ 2) * (!(yy %% 4) - !(yy %% 100) + !(yy %% 400) - 2)"
set maxDay=%n%
echo Days Of Month: %maxDay%

setlocal enabledelayedexpansion

pause

for /L %%d in (1,1,%maxDay%) do (
      if %%d leq 9 set "day=0%%d"
      if %%d gtr 9 set "day=%%d"
    for /L %%f in (0,1,%max%) do (
      if %%f leq 9 set hour=0%%f
      if %%f gtr 9 set hour=%%f
      for /f "tokens=1*" %%i in ('PowerShell -Command "& {Get-Date "%m%/!day!/%y%" -format "ddd_MMMM_dd"}"') do (
        if not exist "%%i_!hour!_??_%y%.enf" (
           echo Missing: %%i_!hour!_00_%y%.enf
       )
      )
    )
)
NUMBER MISSING: !cnt!
exit /b %n%

注意

我正在测试:

if not exist "%%i_!hour!_??_%y%.enf" (..

??将进行任何分钟的测试.那么文件是否存在为:

The ?? will test for any minute. So whether a file exists as:

Wed_January_01_05_00_2020.enfWed_January_01_05_13_2020.enf

它将在特定时间检测文件.如果这不是您打算执行的操作,则只需恢复到00,但是存在文件到达01的风险.

It will detect a file for the specific hour.. If that is not what you intend to do, then just revert back to 00 but there is a risk of a file arriving at 01 instead.

这篇关于使用批处理脚本检测丢失的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆