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".
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/
: 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?
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. :)
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.
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
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
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).
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
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
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+
>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")