1.

Solve : identify & delete zero byte files from a directory?

Answer»

Hello. If anyone can help me with this issue:

I'm trying to create a batch that will delete any files from a network drive that are Zero bytes.

Notes:
1. This doesn't run from a batch file, but runs from a command prompt (with the single %)
2. It appears that the ~z doesn't work because it deletes ALL the files from the directory.

Script:
FOR %%F IN (UNC Path) DO IF %%~zF equ 0 DEL %%F

Any help would be great.

Thanks... GTHello.
Some questions.
Why would you want to do this? If a file has a least 1 byte, would it be somehow better or more useful?
If you do not like a zero byte file, Why did you make them?
If you did not make them, who did? And Why?

My point is that a zero byte fie make have an important reason for being in a directory.
Let me suggest you first make a list of all the zero byte files before you delete them. You might change your mind.
Reference:
https://en.wikipedia.org/wiki/Zero_byte_file
QUOTE

Zero byte file
From Wikipedia, the free encyclopedia
A zero byte file or zero length file is a computer file containing no data; that is, it has a length or size of zero bytes.
It goes on to say:
Quote
n some cases, zero byte files may be USED to convey information like file metadata (for example, its filename may contain an instruction to a user viewing a directory listing such as documents-have-been-moved-to-partition-D.txt, etc); or to put in a directory to ensure that it is nonempty, since some tools such as backup and revision control software may ignore the empty directories.
The filename itself can be a for of metadata.
Quote
metadata
a set of data that describes and gives information about other data.
Because you are asking it too look at the directory and not any of the files in the directory. If the directory is empty it will be zerobytes and delete the directory.

From the cmd prompt. Double the percent signs if you want to run this in a batch file.

Code: [Select]H:\zerobyte>dir /a-d
Volume in drive H is DATA
Volume Serial Number is D2F3-49FA

Directory of H:\zerobyte

07/22/2016 01:08 PM 1 onebyte.txt
07/22/2016 01:08 PM 2 twobyte.txt
07/22/2016 01:05 PM 0 zerobyte1.txt
07/22/2016 01:05 PM 0 zerobyte2.txt
07/22/2016 01:05 PM 0 zerobyte3.txt
5 File(s) 3 bytes
0 Dir(s) 124,020,506,624 bytes free

H:\zerobyte>for %G in (\\SERVER\SHARE\Squashman\zerobyte\*) do @if %~zG equ 0 @echo delete %G
delete \\SERVER\SHARE\Squashman\zerobyte\zerobyte1.txt
delete \\SERVER\SHARE\Squashman\zerobyte\zerobyte2.txt
delete \\SERVER\SHARE\Squashman\zerobyte\zerobyte3.txt

H:\zerobyte>There's a script that runs on another system that places an error file into an ERROR_Logs folder. Many times, a zero byte file is generated. (I'm working on figuring out why this process is doing this.) I also have a UC4 monitor on this folder, that sends a notification if any files are in this folder for more than 1 hour. (another area where I'm trying to exclude zero byte files. In the mean time, I thought it would be easier to use a batch to delete any zero byte files, until the other STUFF can be sorted out.

Hope this answers your question.

Quote from: Geek-9pm on July 22, 2016, 09:59:40 AM
Hello.
Some questions.
Why would you want to do this? If a file has a least 1 byte, would it be somehow better or more useful?
If you do not like a zero byte file, Why did you make them?
If you did not make them, who did? And Why?

My point is that a zero byte fie make have an important reason for being in a directory.
Let me suggest you first make a list of all the zero byte files before you delete them. You might change your mind.
Reference:
https://en.wikipedia.org/wiki/Zero_byte_fileIt goes on to say:The filename itself can be a for of metadata.
Quote from: Geek-9pm on July 22, 2016, 09:59:40 AM
Why would you want to do this? If a file has a least 1 byte, would it be somehow better or more useful?
If you do not like a zero byte file, Why did you make them?
If you did not make them, who did? And Why?

Geek, the task of deleting zero byte files is the very clear question and it's also clear that the OP has a reason to do so.
Quote
I'm trying to create a batch that will delete any files from a network drive that are Zero bytes.

I chose to interpret that as every zero byte file on the drive.

I added the recursive switch here and support for long filenames.
Remove this portion if Squashman's code below shows the right things on your screen.
@echo found filesize "%~zG" in file "%G" & @echo

The same code will work for a single folder as in Squashman's post if you remove the /R switch and it is the double quotes around the "%G" that allows it to handle long filenames and long pathnames.

Code: [Select]for /r %G in (\\SERVER\SHARE\*) do @if %~zG equ 0 @echo found filesize "%~zG" in file "%G" & @echo delete "%G"


Discussion

No Comment Found