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

deleting empty files

2 views
Skip to first unread message

beli...@aol.com

unread,
Oct 13, 2004, 5:03:17 PM10/13/04
to
How do I delete all the empty files in a directory using cmd.exe on Windows XP?
Thanks.

Timo Salmi

unread,
Oct 13, 2004, 6:33:32 PM10/13/04
to
Phil Robyn <zipp...@berkeley.edu> wrote:

> beli...@aol.com wrote:
> > How do I delete all the empty files in a directory using cmd.exe on Windows XP?
> > Thanks.

> From the CMD prompt:
> for %a in (c:\temp\*.*) do @(if %~za equ 0 echo del "%~fa")
>
> If this appears to do what you require, remove the word 'echo' to
> actually delete the files.

Good precaution. And for extra security add /p to del.

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip

Timo Salmi

unread,
Oct 14, 2004, 12:41:32 AM10/14/04
to
Phil Robyn <zipp...@berkeley.edu> wrote:
> for %a in (c:\temp\*.*) do @(if %~za equ 0 echo del "%~fa")

A slightly diverging draft for a future update inspired by the above
code:

76) How do I create an empty file? How can I detect the empty files?

Creating an empty file:
if not exist empty.txt copy nul empty.txt

Detecting empty files in a folder:
@echo off & setlocal enableextensions
for %%f in (d:\test\*.*) do (
if %%~zf EQU 0 echo %%~tf %%~zf "%%~ff"
)
endlocal & goto :EOF

And throughout an entire drive
@echo off & setlocal enableextensions
for /f "tokens=*" %%f in ('dir /b/s/a-d d:\*.*') do (
if %%~zf EQU 0 echo %%~tf %%~zf %%~ff
)
endlocal & goto :EOF

........

And an addition to the already existing item
1} How get today's date elements into environment variables?

(megasnip)

Consider an application needing today's date. Say we wish to find
all the files made today in a folder:
@echo off & setlocal enableextensions enabledelayedexpansion
for %%f in ("c:\mytest\*.*") do (
echo "%%~tf" | find "%date%" > nul
if !errorlevel! EQU 0 echo %%~tf %%~zf "%%~ff"
)
endlocal & goto :EOF

Matthias Tacke

unread,
Oct 14, 2004, 6:08:08 AM10/14/04
to
Timo Salmi wrote:
>76) How do I create an empty file? How can I detect the empty files?
<snip>

>Detecting empty files in a folder:
>@echo off & setlocal enableextensions
>for %%f in (d:\test\*.*) do (
>if %%~zf EQU 0 echo %%~tf %%~zf "%%~ff"
>)
>endlocal & goto :EOF
>
>And throughout an entire drive
>@echo off & setlocal enableextensions
>for /f "tokens=*" %%f in ('dir /b/s/a-d d:\*.*') do (
>if %%~zf EQU 0 echo %%~tf %%~zf %%~ff
>)
>endlocal & goto :EOF
>
Hi Timo,

for general use and your faq I'd *always* quote to have it working with
file names containing spaces.

--
Greetings
Matthias________________________________________
For help on nt commands enter in a cmd window:
W2K>HH windows.chm::ntcmds.htm XP>HH ntcmds.chm

Timo Salmi

unread,
Oct 14, 2004, 4:51:11 PM10/14/04
to
Matthias Tacke <Matthia...@web.de> wrote:
> Timo Salmi wrote:
> >76) How do I create an empty file? How can I detect the empty files?

> >for /f "tokens=*" %%f in ('dir /b/s/a-d d:\*.*') do (


> >if %%~zf EQU 0 echo %%~tf %%~zf %%~ff
> >)

> for general use and your faq I'd *always* quote to have it working with
> file names containing spaces.

Matthias, thanks. However, I tested code for also such files. The
"tokens=*" should hopefully take care of that.

You raise a very real issue here. All the solutions should be tested
to cover also the LFN quirks. I've tried to do so throughout the
FAQ. But that is not a guarantee against maybe having missed a beat.

Matthias Tacke

unread,
Oct 15, 2004, 11:06:11 AM10/15/04
to
Timo Salmi wrote:

>Matthias Tacke <Matthia...@web.de> wrote:
>> Timo Salmi wrote:
>> >76) How do I create an empty file? How can I detect the empty files?
>
>> >for /f "tokens=*" %%f in ('dir /b/s/a-d d:\*.*') do (
>> >if %%~zf EQU 0 echo %%~tf %%~zf %%~ff
>> >)
>
>> for general use and your faq I'd *always* quote to have it working with
>> file names containing spaces.
>
>Matthias, thanks. However, I tested code for also such files. The
>"tokens=*" should hopefully take care of that.
>
>You raise a very real issue here. All the solutions should be tested
>to cover also the LFN quirks. I've tried to do so throughout the
>FAQ. But that is not a guarantee against maybe having missed a beat.
>
>All the best, Timo
>

I was a bit to fast Timo, seeing a comparison with unquoted elements
alerted me. In your case without reaon since it's quite unlikely that
a file is deleted in the small time span between the dir and the
comparison.

To ilustrate the behaviour of the tilde commands in case of
non(-existent) file names I append this batch:

::Tilde-commands.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off&setlocal
set of="%USERPROFILE%\DESKTOP\test name.test ext"
del %of% >NUL 2>&1
echo.File doesn't exist&call :sub %of%&echo.%of%>%of%
echo.File does exist&call :sub %of%&del %of% >NUL 2>&1
set of="This is: A meaningless sentence. Dot. It illustrates ~ commands"
echo.Isn't a file&call :sub %of%
goto :eof
:sub
echo.[%%~1] =[%~1]
echo.[%%~f1]=[%~f1]
echo.
echo.[%%~d1]=[%~d1]
echo.[%%~p1]=[%~p1]
echo.[%%~n1]=[%~n1]
echo.[%%~x1]=[%~x1]
echo.
echo.[%%~a1]=[%~a1]
echo.[%%~t1]=[%~t1]
echo.[%%~z1]=[%~z1]
echo.----------------------------------------------------------------
::Tilde-commands.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::

0 new messages