Find a specified empty directory

  • Thread starter Thread starter Charles Theodore
  • Start date Start date
C

Charles Theodore

Hello to all,

Want to be able to browse in \\s45r\files\msi\ share to parse all the
directory structure the presence of a directory named "programs",
verify if it is empty and output the result on a text file with the
UNC path when the dir are empty.

Here are some part of source code grabbed from this forum, but i'm not
strong enough in batch to assemble all the part. Thanks you to all.

Charles

::========================= F_Empty.cmd =============================

@echo off&setlocal ENABLEEXTENSIONS
set dir=\\s45r\files\msi
set log=logfile.log

for /f "delims=" %%A in ('dir/B/S/AD "%dir%"') do (
find "programs" & if exist "%%A\programs" call :check "%%A"
)
goto :eof

:check
:: check if directory exists
dir %dir% /ad 2>nul >nul||(echo/Dir not exist&goto :EOF)

:: check if directory has no files
set f=0&for /f %%a in ('dir %dir% /a-d/b 2^>nul') do set/a f+=1
if %f% NEQ 0 (echo/Dir has files) else (echo/Dir has no files)

:: check if directory has no subdirectorys
set d=0&for /f %%a in ('dir %dir% /ad/b 2^>nul') do set/a d+=1
if %d% NEQ 0 (echo/Dir has dirs) else (echo/Dir has no subdirs)

:: check if directory is completely empty
if 1%f%%d% NEQ 100 (echo/Dir not empty) else (echo/Dir is empty)

:log
???
 
Charles said:
Hello to all,
Hi Charles.
Want to be able to browse in \\s45r\files\msi\ share to parse all the
directory structure the presence of a directory named "programs",
verify if it is empty and output the result on a text file with the
UNC path when the dir are empty.
::========================= F_Empty.cmd =============================
@echo off&setlocal ENABLEEXTENSIONS
set dir=\\s45r\files\msi
set log=logfile.log

for /f "delims=" %%A in ('dir/B/S/AD "%dir%\programs"'
) do call :check "%%A"
goto :eof

:check
:: check if directory is empty
set found=0
for /f "delims=" %%B in ('dir %1 /B/S/A') do set /A found+=1
if %found% GTR 0 echo Not empty : %1 & exit /B
echo empty : %1
echo empty: %1 >>%log%
::=====================================================================

Untested.
 
Thank you very much Matthias,

Just one typo error and your code works very good.
Correction:

for /f "delims=" %%A in ('dir/B/S/AD "%dir%"') do (
call :check "%%A"
)
goto :eof

CT
 
Charles said:
Thank you very much Matthias,

Just one typo error and your code works very good.
Correction:

for /f "delims=" %%A in ('dir/B/S/AD "%dir%"') do (
call :check "%%A"
)
goto :eof

Hi Charles,
no typo, usage of possible syntax to avoid unintended line break.
Did you really test my code and receive an error ?

If there is only one command following the do - no need to enclose in
parenthes. Parenthes *may* force you to use DelayedExpansion,see call /?

The shell expects more to come after an opening parenthes so you may
insert line breaks behind and in front of the parenthes.

A caret as the last char on a line is an alternative way to have
line breaks.

HTH
 
Bonjour Matthias,

Script is strong to parse a very large fileserver infrastructure but
it take time to complete, even if running local on the server.

My question is: how to optimize the code, if possible, to run more
faster without cutting in is reliability?

Thanks,

CT


 
Charles said:
Bonjour Matthias,

Script is strong to parse a very large fileserver infrastructure but
it take time to complete, even if running local on the server.

My question is: how to optimize the code, if possible, to run more
faster without cutting in is reliability?
It's difficult to guess from here how your tree looks like and what
the time cinsuming part is.

The call might slow down or the initial dir ...programs. You might
put the dir output in a tempfile and iterate the tempfile with for -
if you output a time stamp you can see what hogs.

Removing the echo to screen might speed up also.

Stripped down as much as possible with time stamp:

::========================= F_Empty.cmd =============================
@echo off&setlocal ENABLEEXTENSIONS
set dir=\\s45r\files\msi
set log=logfile.log
echo Start dir: %time%
dir /B /S /AD "%dir%\programs" >tmp.dir
echo Start chk: %time%
for /f "delims=" %%A in (tmp.dir) do call :check "%%A"
echo Chk finished: %time%
::del tmp.dir
goto :eof
:check
for /f "delims=" %%B in ('dir %1 /B/S/A') do exit /B
echo empty: %1 >>%log%
::=====================================================================

The deletion of the new tmp.dir is commented out, so you can peek into.

Untested.
 
Dear Matthias,

I'm going to try your old and new code tommorow with time stamps parts
in production and compare the result. The results of the "benchmark"
will be posted here.
TTL,

CT
 
Hi,

Old and new codes take approximately the same time to process. Thank
you again for help.

Charles,
 
Back
Top