Detecting missing file using batch script

余生长醉 提交于 2020-12-06 15:48:29

问题


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!

The full script is as per below:

@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!


回答1:


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%

Note

I am testing:

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.enf or Wed_January_01_05_13_2020.enf

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.



来源:https://stackoverflow.com/questions/62949168/detecting-missing-file-using-batch-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!