Page 1 of 1

[SOLVED] Add output message if file is empty [0 KB].

Posted: 19 Aug 2019 13:37
by PAB
Good evening,

I have this piece of code . . .

Code: Select all

for %%R in ("%userprofile%\Desktop\x.txt") do if %%~zR equ 0 del "%userprofile%\Desktop\x.txt"
. . . which works perfectly. What I can't figure out is how to include a message that says "NO ERRORS FOUND, deleting file." I have tried several IF...ELSE statements but can't quite get it right. If the file has data in then obviously there isn't a problem and the batch file displays the message "The file was created successfully". So, something like . . .

If the .txt file has data then output the message "The file was created successfully"
If the .txt file doesn't have data then output the message "NO ERRORS FOUND, deleting file.".

I was going to post the things I have tried but there has been quite a few!!!

Thanks in advance.

Re: Add output message if file is empty [0 KB].

Posted: 19 Aug 2019 14:57
by aGerman
Probably this way.

Code: Select all

for %%R in ("%userprofile%\Desktop\x.txt") do if %%~zR equ 0 (
  del "%userprofile%\Desktop\x.txt"
  echo NO ERRORS FOUND, deleting file.
) else echo The file was created successfully.
Steffen

Re: Add output message if file is empty [0 KB].

Posted: 19 Aug 2019 15:02
by PAB
Brilliant aGerman, your script works perfectly!

Just before you posted I came up with . . .

Code: Select all

for %%R in ("%userprofile%\Desktop\x.txt") do if %%~zR NEQ 0 echo The file was created successfully!
for %%R in ("%userprofile%\Desktop\x.txt") do if %%~zR EQU 0 echo NO PROBLEMS FOUND, deleting the desktop file . . . & del "%userprofile%\Desktop\x.txt"
Not very nice or elegant I know!

Thank you so much, it is appreciated.