View Full Version : find 0 bytes file in a folder


Mak
09-14-2006, 10:36 AM
Hi,

In part of a batch, I need to evaluate if two (maybe three) text files with
known names exist in the current folder and if they are, check they are not
empty.
The first part is easy, but I got stuck on the second.

Ideally, I'd like batch to stop checking and do something as soon as the
first 0 bytes file found, but this isn't critical, there won't be many files
in this folder, I can live with evaluating every single file.

I have isolated second part into this:
@echo off
setlocal
set crap=
for %%i in (*) do (
set crap=%%~zi
call :check
)
goto end

:check
if %crap% EQU 0 (
echo found
pause
)
goto:eof

:end
endlocal & goto:eof

The above works, but I have the feelings it can be done differently, better.
For some reason I'm in mental block with this task, hence asking for help.
Oh, and I'd like to avoid using enabledelayedexpansion (if possible),
external files and utilities. OSes this batch needs to work on: W2k, XP and
W2k3.

billious
09-14-2006, 01:15 PM
"Mak" <nospam@nospam.com> wrote in message
news:OS6m7E%231GHA.968@TK2MSFTNGP03.phx.gbl...
> Hi,
>
> In part of a batch, I need to evaluate if two (maybe three) text files
> with known names exist in the current folder and if they are, check they
> are not empty.
> The first part is easy, but I got stuck on the second.
>
> Ideally, I'd like batch to stop checking and do something as soon as the
> first 0 bytes file found, but this isn't critical, there won't be many
> files in this folder, I can live with evaluating every single file.
>
> I have isolated second part into this:
> @echo off
> setlocal
> set crap=
> for %%i in (*) do (
> set crap=%%~zi
> call :check
> )
> goto end
>
> :check
> if %crap% EQU 0 (
> echo found
> pause
> )
> goto:eof
>
> :end
> endlocal & goto:eof
>
> The above works, but I have the feelings it can be done differently,
> better. For some reason I'm in mental block with this task, hence asking
> for help. Oh, and I'd like to avoid using enabledelayedexpansion (if
> possible), external files and utilities. OSes this batch needs to work on:
> W2k, XP and W2k3.

----- batch begins -------
[1]@echo off
[2]for %%i in (*) do if %%~zi==0 set yzb=%%i&goto found
[3]echo no zero-byte files found
[4]goto :eof
[5]:found
[6]echo %yzb% is a zero-byte file
------ batch ends --------

Lines start [number] - any lines not starting [number] have been wrapped
and should be rejoined. The [number] that starts the line should be removed

The label :eof is defined in NT+ to be end-of-file but MUST be expressed
as :eof

alt.msdos.batch.nt for NT+ batch techniques.....

Mak
09-14-2006, 01:55 PM
"billious" <billious_1954@hotmail.com> wrote in message
news:450946ab$0$24287$a82e2bb9@reader.athenanews.com...
> ----- batch begins -------
> [1]@echo off
> [2]for %%i in (*) do if %%~zi==0 set yzb=%%i&goto found
> [3]echo no zero-byte files found
> [4]goto :eof
> [5]:found
> [6]echo %yzb% is a zero-byte file
> ------ batch ends --------

This is good, thank you.

> The label :eof is defined in NT+ to be end-of-file but MUST be expressed
> as :eof

You've lost me here. Why are you saying this?

billious
09-14-2006, 02:34 PM
"Mak" <nospam@nospam.com> wrote in message
news:uUaNX0$1GHA.324@TK2MSFTNGP05.phx.gbl...
> "billious" <billious_1954@hotmail.com> wrote in message
> news:450946ab$0$24287$a82e2bb9@reader.athenanews.com...
>> ----- batch begins -------
>> [1]@echo off
>> [2]for %%i in (*) do if %%~zi==0 set yzb=%%i&goto found
>> [3]echo no zero-byte files found
>> [4]goto :eof
>> [5]:found
>> [6]echo %yzb% is a zero-byte file
>> ------ batch ends --------
>
> This is good, thank you.
>
>> The label :eof is defined in NT+ to be end-of-file but MUST be expressed
>> as :eof
>
> You've lost me here. Why are you saying this?

I use a script to generate a reply skeleton, using my tested batchcode to
add the [number] to the text.

Since [4] contains ":eof", the batch detected it and spat out the
appropriate line. Comes of having too many complaints that "it doesn't work"
because the colon was omitted or "but you didn't put in the label 'eof.'"
Consequently, it's become SOP (at least in alt.msdos.batch.nt) to include
all the waffle. Bit like the obligatory warning on chocolate peanut bars
"Warning: contains peanuts."

Mak
09-14-2006, 02:52 PM
"billious" <billious_1954@hotmail.com> wrote in message
news:45095953$0$24271$a82e2bb9@reader.athenanews.com...
>>> The label :eof is defined in NT+ to be end-of-file but MUST be expressed
>>> as :eof
>>
>> You've lost me here. Why are you saying this?
>
> I use a script to generate a reply skeleton, using my tested batchcode to
> add the [number] to the text.
>
> Since [4] contains ":eof", the batch detected it and spat out the
> appropriate line. Comes of having too many complaints that "it doesn't
> work" because the colon was omitted or "but you didn't put in the label
> 'eof.'" Consequently, it's become SOP (at least in alt.msdos.batch.nt) to
> include all the waffle. Bit like the obligatory warning on chocolate
> peanut bars "Warning: contains peanuts."

oh, and I've even started to think I've made some trivial mistake in my
batch and you're pointing it out, note to self: use Occam's razor.
Thanks again.