Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can I check in dos if a file has a size of 0

1,523 views
Skip to first unread message

Walter Zackery

unread,
Jan 22, 1999, 3:00:00 AM1/22/99
to
There are (at least) 2 ways to do this in Dos or Win 9x:

copy filename.ext newfile.ext
if not exist newfile.ext echo filename.ext is a 0-byte file.

The above works because the copy command copies, then deletes the copied file
if it's a 0-byte file.

The other method is:

dir filename.ext | find/i " 0 bytes"
if not errorlevel 1 echo filename.ext is a 0-byte file.

Note that there must be at least one space before the 0 in the expression " 0
bytes".


Tom Lavedas

unread,
Jan 22, 1999, 3:00:00 AM1/22/99
to Walter Zackery

A third method:

find/v "" filename.ext | find "[1]" > nul
if errorlevel 1 echo Zero length file

A fourth method (DOS and Win 9x only):

fc filename.exe nul.{{{{/lb1|find "***** NUL.{{{{">nul
if errorlevel 1 echo Zero length file.

BTW, you failed to mention that your first method deletes the file in
the OS's named.

Tom Lavedas
-----------
http://www.pressroom.com/~tglbatch/

Walter Zackery

unread,
Jan 22, 1999, 3:00:00 AM1/22/99
to
Tom Lavedas wrote in article

: BTW, you failed to mention that your first method deletes the file in
: the OS's named.
:

I'm not sure what you mean by the last statement. I clearly stated that the
copy command copies, then deletes the copied file. You're not suggesting that
the copy command deletes the original file, are you?


paminof

unread,
Jan 22, 1999, 3:00:00 AM1/22/99
to

FIND /C /V "" < [Path]FileName

A zero-length file returns "0", if the file is a directory you get
"Access denied", if it doesn't exist "File not found". The rest of
the time you will get a file's line-count, which is better than a
zero-length file any day. :)

Tom Lavedas

unread,
Jan 22, 1999, 3:00:00 AM1/22/99
to Walter Zackery

Oops. I appologize. I was thinking of a little trick I use to test
flag files, when I want to find non-zero length versions. The trick
deletes the flag file when it is zero length, something like this ...

copy /b flag.flg+,, . > nul
if exist flag.flg echo Non-zero length

BTW, that leads to a fifth way to test, something like ...

copy filename.ext nul | find " 0 file" > nul
if not errorlevel 1 echo Zero length file.

Larry Weiss

unread,
Jan 22, 1999, 3:00:00 AM1/22/99
to
Walter Zackery wrote:
> dir filename.ext | find/i " 0 bytes"
> if not errorlevel 1 echo filename.ext is a 0-byte file.
> Note that there must be at least one space before the
> 0 in the expression " 0 > bytes".

This technique will not work (in general) under Windows 95.
Consider using it when two files exist in the current directory.
One is named "foo" and the other is named "foo.bar".
Lets say "foo" is a zero length file and "foo.bar" is not.
Then "dir foo" will produce output similar to:

====================================================================
C:\foobar>dir foo

Volume in drive C is MS-DOS_6
Volume Serial Number is 2424-0D12
Directory of C:\foobar

FOO BAR 5 01-22-99 5:41p foo.bat
FOO 0 01-22-99 5:41p FOO
2 file(s) 5 bytes
0 dir(s) 575,340,544 bytes free
====================================================================

Even though "foo" is zero length, no " 0 bytes" appears anywhere
in the output of "dir foo".

- Larry Weiss

Walter Zackery

unread,
Jan 23, 1999, 3:00:00 AM1/23/99
to
I was aware of that; that's why I was careful to use filename.ext instead of
filename. :-) I'm glad that you mentioned it, because it forced me to look hard
at the DIR command and come up with a better test, which is:

dir/a/-p filename.ext | find " 0 " | find/i/v " bytes"


Larry Weiss

unread,
Jan 23, 1999, 3:00:00 AM1/23/99
to
Walter Zackery wrote:
> ... a better test, which is:

> dir/a/-p filename.ext | find " 0 " | find/i/v " bytes"

What does

dir/a/-p

do? I can't seem to parse it mentally this morning against
the "help" given with "dir /?" under Windows 95.
Especially the "/-p" part.

- Larry Weiss

Tom Lavedas

unread,
Jan 23, 1999, 3:00:00 AM1/23/99
to Larry Weiss

The /A invokes a listing of ALL files, irrespective of attribute, i.e.
hidden, directories, etc. The /-P nullifies any /P specification (pause
at page boundaries) that may be included in a DIRCMD environment
variable. It's a belt and suspenders implimentation. Sure to work in
all environments, or at least that's the hope. A good approach
considering the diversity of the audience. You can never tell what
someone is going to do with your advice (and then write telling you that
it made their system hang).

Larry Weiss

unread,
Jan 23, 1999, 3:00:00 AM1/23/99
to
Tom Lavedas wrote:
> Larry Weiss wrote:
> > What does dir/a/-p do? ...

> The /A invokes a listing of ALL files, irrespective of attribute,
> i.e. hidden, directories, etc. The /-P nullifies any /P
> specification (pause at page boundaries) that may be included in
> a DIRCMD environment variable. It's a belt and suspenders
> implimentation. Sure to work in all environments, or at least
> that's the hope.

That sparse "help" text in Windows 95 versions of the commands
is really grating on me! I guess all batch files might prudently
save/set/restore $DIRCMD just to be sure. Having O'Reilly's
"Windows 95 in a Nutshell" has been a real time-saver for me,
but even they don't cover all the possibilities.

- Larry Weiss

Al Aab

unread,
Jan 24, 1999, 3:00:00 AM1/24/99
to

simtel/msdos

batchutl/
grade12 16303 960603 set errorlevel depending on file size, w/asm
--
=-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
al aab, seders moderator sed u soon
it is not zat we do not see the s o l u t i o n
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+

John Savage

unread,
Jan 28, 1999, 3:00:00 AM1/28/99
to
Larry Weiss <l...@airmail.net> writes:
>Walter Zackery wrote:
>> dir filename.ext | find/i " 0 bytes"
>> if not errorlevel 1 echo filename.ext is a 0-byte file.
>
>This technique will not work (in general) under Windows 95.

>C:\foobar>dir foo


>
> Volume in drive C is MS-DOS_6
> Volume Serial Number is 2424-0D12
> Directory of C:\foobar
>
>FOO BAR 5 01-22-99 5:41p foo.bat
>FOO 0 01-22-99 5:41p FOO
> 2 file(s) 5 bytes
> 0 dir(s) 575,340,544 bytes free


The same goes for DOS, but the solution here is to terminate the
filename with a dot, e.g., dir foo.
Working in the 8.3 filename environment, the remedy is to amend
the snippet from Walter to provide a dot after the dir argument:

dir %1. | find/i " 0 bytes"


if not errorlevel 1 echo filename.ext is a 0-byte file.

This guarantees a single file, regardless of the presence of an extension
when a superfluous dot is ignored.

You will have to test this out on OS with long filenames; I haven't.
--
John Savage (for email, replace "ks" with "k" and delete "n")


0 new messages