add a counter to the batch file

13 views
Skip to first unread message

Ammammata

unread,
Nov 30, 2022, 6:31:09 AM11/30/22
to
Hi

I use this short batch file:

for /d /r %%d in (*.*) do rd "%%d"

to remove empty folders on a backup device (NAS)

it takes quite a long time to complete, alse because I repeat the
command 10 times, to delete nested folders that have been ignored (not
empty) in the previous run

to avoid the output I add

> nul

at the end, so on screen it shows only the a.m. errors

"The directory is not empty"

now, while it is running

https://i.imgur.com/BHmcuWb.png

I'd like to see a sort of counter before the error message, to have a
visual confirmation that the command is running :'), i.e.

125 The directory is not empty
288 The directory is not empty
327 The directory is not empty
458 The directory is not empty
and so on

(note skipped numbers, the missing were those successfully deleted)

Thank you

--
/-\ /\/\ /\/\ /-\ /\/\ /\/\ /-\ T /-\
-=- -=- -=- -=- -=- -=- -=- -=- - -=-
........... [ al lavoro ] ...........

R.Wieser

unread,
Nov 30, 2022, 7:44:22 AM11/30/22
to
Ammammata,

The below is a basic example of how to implement a counter. I'm sure that
with a bit of work you will be able to incoorporate it into your own batch.
:-)

- - - - - - - - - - - - - - - - - -
setlocal ENABLEDELAYEDEXPANSION
set COUNT=0

for %%v in (a b c d) do (
set /A COUNT=!COUNT! + 1
echo Count = !COUNT!
)
- - - - - - - - - - - - - - - - - -

By the way : not my work, I just found it on the web somewhere and have kept
it for future usage.

Regards,
Rudy Wieser


Herbert Kleebauer

unread,
Nov 30, 2022, 9:58:25 AM11/30/22
to
On 30.11.2022 12:31, Ammammata wrote:
> Hi
>
> I use this short batch file:
>
> for /d /r %%d in (*.*) do rd "%%d"
>
> to remove empty folders on a backup device (NAS)
>

> I'd like to see a sort of counter before the error message, to have a
> visual confirmation that the command is running :'), i.e.
>
> 125 The directory is not empty
> 288 The directory is not empty
> 327 The directory is not empty
> 458 The directory is not empty
> and so on
>

@echo off
certutil -f -decode %~f0 cr.txt>nul
echo.

for /d /r %%d in (*.*) do (
type cr.txt
echo.|set /p =%%d .
rd "%%d")

del cr.txt
echo.
echo.
goto :eof

-----BEGIN CERTIFICATE-----
DQ==
-----END CERTIFICATE-----


Kerr-Mudd, John

unread,
Nov 30, 2022, 11:21:44 AM11/30/22
to
That's a groovy "certificate"!

--
Bah, and indeed Humbug.

Zaidy036

unread,
Nov 30, 2022, 12:47:45 PM11/30/22
to
On 11/30/2022 6:31 AM, Ammammata wrote:
> Hi
>
> I use this short batch file:
>
> for /d /r %%d in (*.*) do rd "%%d"
>
> to remove empty folders on a backup device (NAS)
>
> it takes quite a long time to complete, alse because I repeat the
> command 10 times, to delete nested folders that have been ignored (not
> empty) in the previous run
>

try this - may be faster

SET _SRC=<path>

:: Remove empty folders
FOR /f "delims=" %%i in ('DIR %_SRC% /AD /S /B ^| SORT /R') DO RD "%%i"
> NUL 2>&1


JJ

unread,
Nov 30, 2022, 4:46:42 PM11/30/22
to
On Wed, 30 Nov 2022 15:58:18 +0100, Herbert Kleebauer wrote:
>
> @echo off
> certutil -f -decode %~f0 cr.txt>nul
> echo.
>
> for /d /r %%d in (*.*) do (
> type cr.txt
> echo.|set /p =%%d .
> rd "%%d")
>
> del cr.txt
> echo.
> echo.
> goto :eof
>
> -----BEGIN CERTIFICATE-----
> DQ==
> -----END CERTIFICATE-----

Considering that the CERTUTIL tool is only included starting from in Windows
Vista, using VBScript would be more reliable for at least Windows 2000. e.g.

@echo off
setlocal
>cr.vbs echo wscript.echo vbcr
>cr.txt cscript //nologo cr.vbs
del cr.vbs
set /p a=abc<nul
type cr.txt
echo xyz
del cr.txt

The CR character can also stored into a variable, but DelayedExpansion must
be used to display the CR character. e.g.

@echo off
setlocal
>cr.vbs echo wscript.echo vbcr
for /f %%A in ('cscript //nologo cr.vbs') do set "cr=%%A"
del cr.vbs
setlocal enabledelayedexpansion
echo abc!cr!xyz
endlocal

I haven't checked, but it may also work for other control characters except
NULL (0x00) and ETX (0x03 / CTRL+C) characters.

Kerr-Mudd, John

unread,
Dec 1, 2022, 6:18:58 AM12/1/22
to
On Thu, 1 Dec 2022 04:46:37 +0700
JJ <jj4p...@outlook.com> wrote:

> On Wed, 30 Nov 2022 15:58:18 +0100, Herbert Kleebauer wrote:
> >
> > @echo off
> > certutil -f -decode %~f0 cr.txt>nul
> > echo.
> >
> > for /d /r %%d in (*.*) do (
> > type cr.txt
> > echo.|set /p =%%d .
> > rd "%%d")
> >
> > del cr.txt
> > echo.
> > echo.
> > goto :eof
> >
> > -----BEGIN CERTIFICATE-----
> > DQ==
> > -----END CERTIFICATE-----
>
> Considering that the CERTUTIL tool is only included starting from in Windows
> Vista, using VBScript would be more reliable for at least Windows 2000. e.g.

Heck if you want retro support (and who doesn't run DOS) use a debug
script to create the CR.
off the top of my head, not tested:

mkcr.dbg
<-------------------->
n cr.txt
e100 0D
rcx 1
w
q
<-------------------->

debug<mkcr.dbg


>
> @echo off
> setlocal
> >cr.vbs echo wscript.echo vbcr
> >cr.txt cscript //nologo cr.vbs
> del cr.vbs
> set /p a=abc<nul
> type cr.txt
> echo xyz
> del cr.txt
>
> The CR character can also stored into a variable, but DelayedExpansion must
> be used to display the CR character. e.g.
>
> @echo off
> setlocal
> >cr.vbs echo wscript.echo vbcr
> for /f %%A in ('cscript //nologo cr.vbs') do set "cr=%%A"
> del cr.vbs
> setlocal enabledelayedexpansion
> echo abc!cr!xyz
> endlocal
>
> I haven't checked, but it may also work for other control characters except
> NULL (0x00) and ETX (0x03 / CTRL+C) characters.


Reply all
Reply to author
Forward
0 new messages