Famela Canico
Famela Canico

Reputation: 31

ELSE condition not executed when checking if file exists

I have a batch script which checks the following about a file:
1. If file exists
2. If file is empty
3. if file is up to date (comparing with current date)

I'm able to get results for all conditions whenever I run the batch and a file is present in the folder I'm using. However, it exits when I remove the file then run the script to test condition #1.

I noticed that the ELSE statement for the condition which checks if file exists is not being executed. Below is the code:

@echo off  
setlocal   
set strpath="C:\SAMPLE.txt"  
set $strErrorMessage="No Error"  

set datenow=%DATE:~4,13%  
FOR %%A IN (%strpath%) DO set filelastmodifieddate=%%~tA  
FOR %%A IN (%strpath%) DO set filesize=%%~zA  

set filelastmodifieddate=%filelastmodifieddate:~0,10%  

IF EXIST %strpath% (  
   IF %filesize% NEQ 0 (  
      IF %filelastmodifieddate% EQU %datenow% (rem do something  
      ) ELSE (SET strErrorMessage=FILE IS NOT UDPATED)  
   ) ELSE (SET strErrorMessage=ZERO BYTE FILE)  
) ELSE (SET strErrorMessage=FILE DOES NOT EXIST)  

echo %strErrorMessage%

Upvotes: 0

Views: 396

Answers (3)

jsxt
jsxt

Reputation: 1135

The nested conditions are not quite convenient to read and understand the logic even with indentations. If you rethink conditions you will be able to simplify the logic as below:

set "strErrorMessage=NO ERROR"

for %%a in ( %strpath% ) do if not exist "%%~a" (
    SET "strErrorMessage=FILE DOES NOT EXIST"
) else if %%~za equ 0 (
    SET "strErrorMessage=ZERO BYTE FILE"
) else if %%~ta neq %datenow% (
    SET "strErrorMessage=FILE IS NOT UDPATED"
)

Upvotes: 0

michael_heath
michael_heath

Reputation: 5372

@echo off
setlocal
set "strpath=C:\SAMPLE.txt"
set "strErrorMessage=No Error"

set "datenow=%DATE:~4,13%"

if not exist "%strpath%" (
    set "strErrorMessage=FILE DOES NOT EXIST"
    goto :end
)

for %%A in ("%strpath%") do set "filelastmodifieddate=%%~tA"
for %%A in ("%strpath%") do set "filesize=%%~zA"

set "filelastmodifieddate=%filelastmodifieddate:~0,10%"

if %filesize% EQU 0 (
    set "strErrorMessage=ZERO BYTE FILE"
) else if %filelastmodifieddate% NEQ %datenow% (
    set "strErrorMessage=FILE IS NOT UDPATED"
)

:end
echo(%strErrorMessage%

Seems illogical to do the for loops or anything else if the file does not exist. So if the file exist, do the for loops to get the timestamp and size of the file, else set FILE DOES NOT EXIST and goto :end.

Now, the rest of the code can continue if the file exist and be regarded as valid. If you do consider not checking first if the file exist, then you could run into syntax errors with if comparisons etc. with undefined variables.

Upvotes: 0

user7818749
user7818749

Reputation:

Replace entire if statement block with this. It is a slightly simpler, more readable way to do what you want:

if not exist %strpath% (
     set "strErrorMessage=File does not exist!"
     goto :end
  )
if %filesize% equ 0 (
     set "strErrorMessage=Zero Byte file!"
     goto :end
  ) 
if %filelastmodifieddate% neq %datenow% (
     set "strErrorMessage=File not updated!"
     goto :end
  )
rem If we get to this pint, we have met all the requirements and we DO SOMETHING here
set "strErrorMessage=Success!"

:end
echo %strErrorMessage%

Upvotes: 1

Related Questions