1

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!

7
  • 2
    please show an actual example of day_month_date_hh_mm_00_yyyy.enf is it fri_July_17_10_39_00_2020.enf? Commented Jul 17, 2020 at 8:35
  • Hi, yes. So, if there are a few files on the same day, they will appear like fri_july_17_10_00_00_2020.enf fri_july_17_11_01_00_2020.enf fri_july_17_12_00_00_2020.enf . . . Commented Jul 17, 2020 at 9:29
  • and you only check files for the day? Commented Jul 17, 2020 at 9:30
  • No. I will need to check the files for the whole month. Also, one file is being generated every hour, hence, total there should be 24 files per day. So, the script will have to print out the missing file names after doing a check on all the files of that month. Commented Jul 17, 2020 at 9:36
  • ok, so use powershell to determine the name easily Get-Date "15/07/2020 00:00" -format "ddd" Commented Jul 17, 2020 at 9:39

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Gerhard for your time and detailed explanation to help with my learning. The Powershell command really does the job! However, since you mentioned that there is a way to make the script run faster and better, can I trouble you to advise on it if you have the time to? Thank you!
Yes, I can do that. I have to rewrite everything though, so I need to find some time. I will see what I can do over the weekend, between tasks, but I cannot promise anything for the weekend.
Thanks once again Gerhard, understand that it will be taking up some time as you have to rewrite everything. Please take your time, I can still wait as the current script can still do the job, just that it's taking a bit of time. Thank you so much for the help! :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.