windows batch file to check for folder in a loop and pass parameter

hi Experts,
Could you pls help.

1. Trying to parse to list of directories.
2. Check if each folder is NOT empty, if not empty call a function/goto by passing the folder full path
3. function/goto should take the argument and echo

so far have scripted below but not working and not sure is this the right logic.

FOR /F %%i IN (E:\folder_list.txt) DO CALL :dirEmpty %%i

:dirEmpty
dir /A /B "E:\base_folder\\%~1" | findstr /R "." > NUL && GOTO doFurther <pass_folder_path>

:doFurther
echo "Folder not empty %~1"

Open in new window

enthuguyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
Is a folder considered empty if it has no files in it, or no files in it, and no subfolders under it?


»bp
0
Bill PrewCommented:
This should do what you want I believe, I added a few comments and made a few changes.  An important one is to use EXIT /B at the end of subroutines, and after the main logic.  And avoid GOTO if at all possible, especially within a CALLed routine, it can be tricky if you aren't carefull.  There were also a few other small errors...

@echo off
setlocal

rem Define base folder and list file locations
set BaseDir=B:\EE\EE29070886\Files
set ListFile=list.txt

rem Read each line fron folder list, call routine to check folder
for /F "usebackq tokens=*" %%i in ("%ListFile%") do (
    call :dirEmpty %%i
)

rem Exit main logic
exit /b

rem Subroutine to check if any files in a passed in folder name
:dirEmpty
    rem If files found in folder call routine to process this folder
    dir /B "%BaseDir%\%~1" | findstr /R "." > NUL && (
        call :doFurther "%BaseDir%\%~1"
    )
    exit /b

:doFurther
    echo Folder not empty "%~1"
    exit /b

Open in new window


»bp
0
enthuguyAuthor Commented:
Thanks Bill, I'm trying out now.

To answer your first question. no files in a folder (not sub folder at this stage)
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

enthuguyAuthor Commented:
Thanks Bill it worked for most of it.

could you help me how to echo (gracefully exit) if files not found please?. Since I invoke this from Jenkins, it makes the jenkins job as failure :)

File Not Found
Build step 'Execute Windows batch command' marked build as failure
Started calculate disk usage of build
Finished Calculation of disk usage of build in 0 seconds
Started calculate disk usage of workspace
Finished Calculation of disk usage of workspace in 0 seconds
Notifying upstream projects of job completion
Finished: FAILURE
0
enthuguyAuthor Commented:
Quick update.

Added exit code 0 in dirEmpty  and it worked. Is this the right way?

exit /b 0

how can I echo saying folder is empty pls?
0
Bill PrewCommented:
Not sure what you mean, are you asking that if no files are found in any of the folders in the text file then do something special?


»bp
0
enthuguyAuthor Commented:
Sorry,
Just an echo saying files not available

Or is it possible to call another routine bill?
0
Bill PrewCommented:
If you want the BAT script to return a status to Jenkins, then you should be able to do something like this (which includes a message to the console too).  This will return a status of 0 from the BAT if files are found, and a 1 if no files found (wasn't sure which was "normal" for your usage).  Hopefully gives you the idea...

@echo off
setlocal

rem Define base folder and list file locations
set BaseDir=B:\EE\EE29070886\Files
set ListFile=list.txt

rem Read each line fron folder list, call routine to check folder
for /F "usebackq tokens=*" %%i in ("%ListFile%") do (
    call :dirEmpty %%i
    set Return=%ERRORLEVEL%
)

rem Exit main logic
exit /b %Return%

rem Subroutine to check if any files in a passed in folder name
:dirEmpty
    rem If files found in folder call routine to process this folder
    dir /B "%BaseDir%\%~1" | findstr /R "." > NUL && (
        echo Folder not empty "%BaseDir%\%~1"
        exit /b 0
    ) || (
        echo Folder empty "%BaseDir%\%~1"
        exit /b 1
    )

Open in new window


»bp
0
enthuguyAuthor Commented:
Thanks a lot Bil, implementing this. Will keep you posted
0
Bill PrewCommented:
Are there multiple folders in the incoming TXT file?  If so that that isn't really addressed well in the current script I proposed.

If you can tell me in english what the script needs to do, and what is an "error" or not an error (if that is even the case), I could get closer I'm sure...


»bp
0
enthuguyAuthor Commented:
Yes,

There will be multiple folder name listed in the text file.
e.g
subfolder1
subfolder2
subfolder3

2. Our script needs to check %BaseDir%\<each folder name from text file>\ if it is empty or not.
e.g
%BaseDir%\subfolder1\
%BaseDir%\subfolder2\
%BaseDir%\subfolder3\

3. For example if %BaseDir%\subfolder1\ is empty, call a subroutine_notfound > and this subroutine_notfound will have an echo "%BaseDir%\subfolder1\" empty. But it should continue to the next folder subfolder2 in the loop.

4. For example if %BaseDir%\subfolder2\ is NOT empty, call a subroutine_found > and this subroutine_found will have an echo "%BaseDir%\subfolder1\" is NOT empty. But it should continue to the next folder subfolder3 in the loop.

Thanks again for your help
0
Bill PrewCommented:
Okay, if that's all there is to it, and no status passed back out of the BAT script, then it could be simplified to:

@echo off
setlocal

rem Define base folder and list file locations
set BaseDir=B:\EE\EE29070886\Files
set ListFile=list.txt

rem Read each line fron folder list, call routine to check folder
for /F "usebackq tokens=*" %%i in ("%ListFile%") do (
    rem Display message indicating if folder is empty or not
    dir /B "%BaseDir%\%%~i" | findstr /R "." > NUL && (
        echo Folder not empty "%BaseDir%\%%~i"
    ) || (
        echo Folder empty "%BaseDir%\%%~i"
    )
)

Open in new window


»bp
0
enthuguyAuthor Commented:
Thanks
Instead of an echo in same section.

can we call specific routine/goto pls

e.g.
dirEmpty will echo "%BaseDir%\subfolder1\" empty
doFurther will echo "%BaseDir%\subfolder1\" is NOT empty
0
Bill PrewCommented:
You can, but curious why?  That just adds code and slows down the process.  I like modular coding myself too, but I wouldn't typically call a one line routine.  It can certainly be written that way, but trying to understand what you're thinking, in case I am missing something...

@echo off
setlocal

rem Define base folder and list file locations
set BaseDir=B:\EE\EE29070886\Files
set ListFile=list.txt

rem Read each line fron folder list, call routine to check folder
for /F "usebackq tokens=*" %%i in ("%ListFile%") do (
    rem If files found in folder call routine to process this folder
    dir /B "%BaseDir%\%%~i" | findstr /R "." > NUL && (
        call :DoFurther "%%~i"
    ) || (
        call :DirEmpty "%%~i"
    )
)

exit /b

:DirEmpty
    echo "%BaseDir%\%~1" empty
    exit /b

:DoFurther
    echo "%BaseDir%\%~1" in NOT empty
    exit /b

Open in new window


»bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
enthuguyAuthor Commented:
Thanks Bill, I got ur point.

Would like to process few things in these two scenarios (pass/failure). hence requested that way.
0
enthuguyAuthor Commented:
Thanks Bill, that really helped
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.