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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
PAB
Posts: 139
Joined: 12 Aug 2019 13:57

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

#1 Post by PAB » 19 Aug 2019 13:37

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.
Last edited by PAB on 22 May 2020 09:28, edited 1 time in total.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

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

#2 Post by aGerman » 19 Aug 2019 14:57

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

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

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

#3 Post by PAB » 19 Aug 2019 15:02

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.

Post Reply