Computing.Net > Forums > Disk Operating System > Bat file to find a 0 byte text file

Bat file to find a 0 byte text file

Reply to Message Icon

Original Message
Name: Mary
Date: March 9, 2003 at 07:41:00 Pacific
Subject: Bat file to find a 0 byte text file
OS: Dos
CPU/Ram: 450/ 256
Comment:

Please help, I'm a newbie.
I need to create a batch file to locate and determine if a text file is 0 bytes.
If you could please reply with code and an explanation, that would be great.
Thanks in advance,
Mary


Report Offensive Message For Removal


Response Number 1
Name: Secret_Doom
Date: March 9, 2003 at 11:18:03 Pacific
Reply: (edit)

Mary wrote:
> I need to create a batch file to locate
> and determine if a text file is 0 bytes.

You mean you want to find (track) a file which is 0 byte long, or you already have a certain filename and you wanna check if it's 0 byte long or not?

And by the way, you reported your OS to be DOS. Are you sure about that? Just because you're using the command interpreter, but is under a Windows, it doesn't mean you're on DOS, not at all. So, if you're under Windows, report its version (95, 98, NT, 2000, XP, etc). If you're really on DOS, do also report its version (5.0, 6.0, 6.22, etc).

-- Leonardo Pignataro - Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br


Report Offensive Follow Up For Removal

Response Number 2
Name: Secret_Doom
Date: March 10, 2003 at 05:13:35 Pacific
Reply: (edit)

Leonardo,

Thanks for the reply. I'm using Win 98.
I want create a bat file that will find "all" text files that are 0 bytes in the C:\directory and all sub directories.

So any file with a ".txt" extenstion,
will be found and those files will show as a 0 byte file on the screen when the batch is run. Thanks in advance,
Mary


Report Offensive Follow Up For Removal

Response Number 3
Name: Secret_Doom
Date: March 10, 2003 at 11:13:57 Pacific
Reply: (edit)

It's much clearer now, Mary. So, the following batch script will look for 0 byte files named %TGTFILE% (wildcards supported, e.g.: *.txt) on the directory %TGTDIR% and on its subdirs. The found filenames will be displayed on the screen.

:: Batch script to track 0 byte files
@echo off
if "%1"=="GoTo" goto %2

:: Set target directory and filename to look for 0-byte
:: files. The subdirectories of TGTDIR will be searched
:: as well. Wildcards may be used on the filename. Do
:: * NOT * use executable extentions on the filename.
set TGTDIR=C:\AnyFolder
set TGTFILE=*.txt

echo e100 BA 18 01 B9 01 00 BB 00 00 B4 3F CD 21 09 C0 74> %temp%.\zb.com
echo e110 02 CD 20 B8 01 4C CD 21>> %temp%.\zb.com
for %%? in (rcx 18 w q) do echo %%?>> %temp%.\zb.com
type %temp%.\zb.com |DEBUG %temp%.\zb.com > nul
echo @prompt call %0 GoTo process;> %temp%.\t1.bat
XCOPY/l/n/s "%TGTDIR%\%TGTFILE%" \ |FIND/v " ">> %temp%.\t1.bat
echo @exit>> %temp%.\t1.bat
ctty nul
%comspec% /c %temp%.\t1.bat > %temp%.\t2.bat
ctty con
call %temp%.\t2.bat
echo.
for %%? in (t1.bat t2.bat zb.com) do del %temp%.\%%?
for %%? in (TGTDIR TGTFILE) do set %%?=
goto eof

:process
%temp%.\zb.com < %TGTDIR%\%3
if errorlevel=1 echo %TGTDIR%\%3

:eof

That batch script is compatible with Win9x only. Tell me if it worked as intended. The 8.3 file system is used when displaying the 0 byte files on the screen.

BTW, what the heck happenned on this site? Your post appeared with my nick as poster...

-- Leonardo Pignataro - Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br

___________________________________________________________________________


Report Offensive Follow Up For Removal

Response Number 4
Name: Leonardo
Date: March 10, 2003 at 19:18:05 Pacific
Reply: (edit)

Leonardo,

Thanks for the info. I understand most of it, but can you tell me what the \zb.com part is for?
I will check it out at a later time..

Sorry, I have no clue what happened on the web site.

Thanks so much for your help.

Mary


Report Offensive Follow Up For Removal

Response Number 5
Name: Secret_Doom
Date: March 11, 2003 at 14:53:38 Pacific
Reply: (edit)

Geez, now your name appeared as "Leonardo", with my email, like in the other post. However, I don't even have that name registered with my email! You do realize you ought to enter your OWN name and email (email not necessary) on the reply form, don't you? Anyway, let's just keep signing the posts on the end, ok?

The batch is mainly divided into two parts:

1. Make a list of all the .txt files to be checked and transform it so it works as a batch file to call a subroutine on the main batch script, passing each filename one by one;

2. On the subroutine, check if each of the filenames passed is 0-byte, and if so, display it.

I'll only explain the part you said you didn't understand. ZB.COM is an assembler program created, used and futher deleted by the batch script itself.

It will read input from STDIN. If there is any input, it returns errorlevel 0. Otherwise, if there's no input at all (that's what happens if STDIN is a 0-byte file), it returns errorlevel 1. There are other ways to check if a file is 0-byte or not. Here's the one I like most:

copy file.ext nul |FIND " 0 " > nul
if not errorlevel=1 echo file IS 0-byte!
if errorlevel=1 echo file IS NOT 0-byte!

That works because the COPY cmd, on DOS/Win9x, doesn't copy 0-byte files. It displays a message like " 0 File(s) copied" if you try it.

Note 1: The returned error message may change because of different OS languages, but the " 0 " part is fix, and so reliable.
Note 2: That method cannot be used on NT systems because the cmd's behavior is different.

So, why did I use ZB.COM, which takes the work of 4 lines to be created, some more work to delete it and makes the source so strange? That's because ASM programs are _very_ fast. Using it instead of that COPY method will make the process faster. That speed advantage on a single check isn't that important, and that's why I generally use the COPY method. However, if we're talking about several files to be checked, the time saved increases with each file that is checked using this faster method.

The commented source for that program follows:

nnnn:0100 MOV     DX,0118 ;}
nnnn:0103 MOV     CX,0001 ;}
nnnn:0106 MOV     BX,0000 ;} read the 1st character from STDIN
nnnn:0109 MOV     AH,3F   ;}
nnnn:010B INT     21      ;}
nnnn:010D OR      AX,AX   ; did we read any data at all?
nnnn:010F JZ      0113    ; if not, skip next command
nnnn:0111 INT     20      ; 'default exit' (errorlevel=0)
nnnn:0113 MOV     AX,4C01 ; set errorlevel to 1
nnnn:0116 INT     21      ; 'custom exit'

Those quoted terms aren't official, I just invented them to make the comments easier to understand.

I'm the author of that ASM program (wooow, what a program... Almost the complexity of an OS hehe...)

-- Leonardo Pignataro - Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Bat file to find a 0 byte text file

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge