zsabo
asked on
check existence of folders and output to nagios ( OK/ Critical)
Hello,
I am looking for a batch file which verifies a list of directories (for example w,x,y,z folders), and in case some folder is absent, the output will be "Critical" and the absent folder/s, or in case that is not absent any, the output will be "OK".
Thanks
I am looking for a batch file which verifies a list of directories (for example w,x,y,z folders), and in case some folder is absent, the output will be "Critical" and the absent folder/s, or in case that is not absent any, the output will be "OK".
Thanks
In case you don't care about each individual folder but rather output Critical if at least one is missing try the next script.
Hope it helps!
Delyan
Hope it helps!
Delyan
@echo off
set output=OK
for %%i in (folder1 folder2 folder3) do if not exist %%i set output=Critical
echo %output%
ASKER
Hello Delyan,
Is it possible that the output be in rows?
Is it possible that the output be in rows?
delyan_valchev
Unfortunately, your code does not truly validate the existence of folders. let's say you have a FILE named 'folder1' and two FOLDERS named 'folder2' and 'folder3'. Your batch file would list all three of them as FOLDERS!!! Quite clearly, this is wrong!
delyan_valchev
Unfortunately, your code does not work correctly either for the same reason as above.
Unfortunately, your code does not truly validate the existence of folders. let's say you have a FILE named 'folder1' and two FOLDERS named 'folder2' and 'folder3'. Your batch file would list all three of them as FOLDERS!!! Quite clearly, this is wrong!
delyan_valchev
Unfortunately, your code does not work correctly either for the same reason as above.
zsabo
Niether of the two previous solutions work correctly. I have commented why in my previous post.
The following code is the correct method for testing for the existence of folders:
@echo off
for %%a in (folder1 folder2 folder3) do (
if exist %%a\ (
echo %%a OK
) else (
echo %%a Critical
)
)
Niether of the two previous solutions work correctly. I have commented why in my previous post.
The following code is the correct method for testing for the existence of folders:
@echo off
for %%a in (folder1 folder2 folder3) do (
if exist %%a\ (
echo %%a OK
) else (
echo %%a Critical
)
)
@t0t0
Thanks for the special case handling fix.
@zsabo
What do you mean by "output be in rows"?
Thanks for the special case handling fix.
@zsabo
What do you mean by "output be in rows"?
ASKER
Hello Delyan,
I'm refering that output would be nice in a row. For example, instead of:
folder1
folder2
folder2
this way:
folder1 folder2 folder3...
I'm refering that output would be nice in a row. For example, instead of:
folder1
folder2
folder2
this way:
folder1 folder2 folder3...
zsabo
That's not 'rows', that's columns.
@echo off
setlocal enabledelayedexpansion
set output=
for %%a in (folder1 folder2 folder3) do (
if exist %%a\ (
set output=!output! OK
) else (
set output=!output! Critical
)
)
echo %output%
That's not 'rows', that's columns.
@echo off
setlocal enabledelayedexpansion
set output=
for %%a in (folder1 folder2 folder3) do (
if exist %%a\ (
set output=!output! OK
) else (
set output=!output! Critical
)
)
echo %output%
Or you could have something like this:
@echo off
setlocal enabledelayedexpansion
set output=
for %%a in (folder1 folder2 folder3) do (
if exist %%a\ (
set output=!output! %%a OK
) else (
set output=!output! %%a Critical
)
)
echo %output%
@echo off
setlocal enabledelayedexpansion
set output=
for %%a in (folder1 folder2 folder3) do (
if exist %%a\ (
set output=!output! %%a OK
) else (
set output=!output! %%a Critical
)
)
echo %output%
How many folders do you anticipate testing the existence for?
I ask because if it's more than a few it would be easier to list the names of the folders in a separate text file which are then read by the batch file itself. This makes good sense as you can amend the text file without having to change the batch file code.
I ask because if it's more than a few it would be easier to list the names of the folders in a separate text file which are then read by the batch file itself. This makes good sense as you can amend the text file without having to change the batch file code.
Above, I showed you code which produce these outputs:
(comment ID: 24479224)
folder1 Critcal
folder2 OK
folder3 OK
(comment ID: 24491832)
folder1 Critical folder2 OK folder3 OK
(comment ID: 24491823)
Critical OK OK
How about (where folder1 does not exist) just this (see the following code):
folder2 folder3
@echo off
setlocal enabledelayedexpansion
set output=
for %%a in (folder1 folder2 folder3) do (
if exist %%a\ set output=!output! %%a
)
echo %output%
(comment ID: 24479224)
folder1 Critcal
folder2 OK
folder3 OK
(comment ID: 24491832)
folder1 Critical folder2 OK folder3 OK
(comment ID: 24491823)
Critical OK OK
How about (where folder1 does not exist) just this (see the following code):
folder2 folder3
@echo off
setlocal enabledelayedexpansion
set output=
for %%a in (folder1 folder2 folder3) do (
if exist %%a\ set output=!output! %%a
)
echo %output%
zsabo
(Magyar?)
Could we please have some feedback so that we can provide you with further assistance if need be.
(Magyar?)
Could we please have some feedback so that we can provide you with further assistance if need be.
ASKER
Hello t0t0,
I would like to monitor approximately 8 directories.
The result that I need is of the type:
OK
Critical FolderX FolderY
I would like to monitor approximately 8 directories.
The result that I need is of the type:
OK
Critical FolderX FolderY
ASKER
OK --> All the directories exist.
Critical -->The directories do not exist or only some.
Critical -->The directories do not exist or only some.
Here's the script with the requested output. In case some folders don't exist it also outputs the ones that do exist.
@echo off
setlocal EnableDelayedExpansion
set OK=OK
set Critical=Critical
for %%i in (folder1 folder2 folder3) do (
if exist %%i\ (
set OK=!OK! %%i
) else (
set Critical=!Critical! %%i
)
)
if "%Critical%"=="Critical" (
rem All folders exist
echo OK
) else (
rem Some folders exist
echo %OK%
echo %Critical%
)
ASKER
Hello delyan,
The output of the script always show OK. In my test there isn't any folder to test , but output shows always OK:
>test_folder.cmd
OK
Critical E:\j
The output of the script always show OK. In my test there isn't any folder to test , but output shows always OK:
>test_folder.cmd
OK
Critical E:\j
Normally when some or none of the folders exist it shows which are OK and which are not. In your case OK line is empty, meaning no folder is OK. If you want just the missing folders list, delete the line:
echo %OK%
(the third from the bottom)
echo %OK%
(the third from the bottom)
Please try the following and give some feedback as to how close we're arriving at a solution. (Included are my sample set of folders - but you can change these of course).
@echo off
setlocal enabledelayedexpansion
set ok=
set critical=
for %%a in (folder1 folder2 folder3 desktop sendto folder4 windows "my documents") do (
if exist %%a\ (
set ok=!ok! %%~a
) else (
set critical=!critical! %%~a
)
)
if defined ok echo OK %ok%
if defined critical echo Critical %critical%
@echo off
setlocal enabledelayedexpansion
set ok=
set critical=
for %%a in (folder1 folder2 folder3 desktop sendto folder4 windows "my documents") do (
if exist %%a\ (
set ok=!ok! %%~a
) else (
set critical=!critical! %%~a
)
)
if defined ok echo OK %ok%
if defined critical echo Critical %critical%
Please review the code in my previous comment BUT also review the current code.
The code below simply outputs either "Critical" or "OK" depending if some of the folders do not exist:
@echo off
set critical=
for %%a in (folder1 folder2 folder3 folder4) do (
if not exist %%a\ set critical=1
)
if defined critical (echo Critical) else (echo OK)
The code below simply outputs either "Critical" or "OK" depending if some of the folders do not exist:
@echo off
set critical=
for %%a in (folder1 folder2 folder3 folder4) do (
if not exist %%a\ set critical=1
)
if defined critical (echo Critical) else (echo OK)
Please check the following:
@echo off
setlocal enabledelayedexpansion
set ok=
set critical=
for %%a in (folder1 folder2 folder3 desktop sendto folder4 windows "my documents") do (
if exist %%a\ (
echo %%~a - OK
set ok=ok
) else (
echo %%~a - Critical
set critical=critical
)
)
if defined ok echo Some folders were present
if defined critical echo Some folders were not present
@echo off
setlocal enabledelayedexpansion
set ok=
set critical=
for %%a in (folder1 folder2 folder3 desktop sendto folder4 windows "my documents") do (
if exist %%a\ (
echo %%~a - OK
set ok=ok
) else (
echo %%~a - Critical
set critical=critical
)
)
if defined ok echo Some folders were present
if defined critical echo Some folders were not present
do you require further assistance or do you wish to close this question?
Here's another way of assugning folders to variables and reading their names in a FOR loop. You can customise the settings for 'folder[1]...folder[8]'. Adding more folders is easy.
@echo off
setlocal enabledelayedexpansion
set ok=
set critical=
set folder[1]=c:\temp
set folder[2]=c:\documents and settings\paul
set folder[3]=c:\documents and settings\john
set folder[4]=c:\application a
set folder[5]=c:\application b
set folder[6]=c:\folderA
set folder[7]=c:\folderB
set folder[8]=c:\folderC
for /l %%a in (1,1,8) do (
if exist !folder[%%a]!\ (
echo !folder[%%a]! - OK
set ok=ok
) else (
echo !folder[%%a]! - Critical
set critical=critical
)
)
if defined ok echo Some folders were present
if defined critical echo Some folders were not present
@echo off
setlocal enabledelayedexpansion
set ok=
set critical=
set folder[1]=c:\temp
set folder[2]=c:\documents and settings\paul
set folder[3]=c:\documents and settings\john
set folder[4]=c:\application a
set folder[5]=c:\application b
set folder[6]=c:\folderA
set folder[7]=c:\folderB
set folder[8]=c:\folderC
for /l %%a in (1,1,8) do (
if exist !folder[%%a]!\ (
echo !folder[%%a]! - OK
set ok=ok
) else (
echo !folder[%%a]! - Critical
set critical=critical
)
)
if defined ok echo Some folders were present
if defined critical echo Some folders were not present
zsabo
Please confirm whether the above solution solved your problem
Please close this long-overdue question.
Thank you.
Please confirm whether the above solution solved your problem
Please close this long-overdue question.
Thank you.
@echo off
if "%1"=="" exit /b 1
set ok=
set critical=
:loop
if exist "%1\" (
if not defined ok (
set ok=%1
) else (
set ok=%ok% %1
)
) else (
if not defined critical (
set critical=%1
) else (
set critical=%critical% %1
)
)
shift
if not "%1"=="" goto loop
if defined ok echo ok %ok%
if defined critical echo critical %critical%
if "%1"=="" exit /b 1
set ok=
set critical=
:loop
if exist "%1\" (
if not defined ok (
set ok=%1
) else (
set ok=%ok% %1
)
) else (
if not defined critical (
set critical=%1
) else (
set critical=%critical% %1
)
)
shift
if not "%1"=="" goto loop
if defined ok echo ok %ok%
if defined critical echo critical %critical%
Oops! I forgot to add....
call the above batch file with foldernames as paramters, like this for example:
CONFIRM c:\temp c:\c:\tempfolder1 c:\temp\folder2 etc...
where 'confirm.bat' is the name of the batch file.
call the above batch file with foldernames as paramters, like this for example:
CONFIRM c:\temp c:\c:\tempfolder1 c:\temp\folder2 etc...
where 'confirm.bat' is the name of the batch file.
qlemo
i believe my last code works a charm... how come I don't get points?
i believe my last code works a charm... how come I don't get points?
Paul,
See http:#a24507316:
See http:#a24507316:
The result that I need is of the type:
OK
Critical FolderX FolderY
You provided batch variations which always report the folders which are ok, besides zsabo stated twice which output format he wants. He might have given you partitial or all points, but that is something a Cleanup Volunteer is not able to do (and that's true for varying the Grade, too). Strictly seen, the question is NOT answered.OK
Critical FolderX FolderY
qlemo
After looking at it now, it's simpler then I first thought.
I was wondering if it's worth asking the asker if he'll accept the code below and perhaps giving him time to reply again.
zsabo
use the batch file by passing folders as command line options like this:
test_folder.cmd folder1 folder2 folder3 etc...
@echo off
if "%1"=="" exit /b 1
set critical=
:loop
if not exist "%~1\" (
if not defined critical (
set critical=%1
) else (
set critical=%critical% %1
)
)
shift
if not "%~1"=="" goto loop
if not defined critical (
echo OK
) else (
echo Critical %critical%
)
After looking at it now, it's simpler then I first thought.
I was wondering if it's worth asking the asker if he'll accept the code below and perhaps giving him time to reply again.
zsabo
use the batch file by passing folders as command line options like this:
test_folder.cmd folder1 folder2 folder3 etc...
@echo off
if "%1"=="" exit /b 1
set critical=
:loop
if not exist "%~1\" (
if not defined critical (
set critical=%1
) else (
set critical=%critical% %1
)
)
shift
if not "%~1"=="" goto loop
if not defined critical (
echo OK
) else (
echo Critical %critical%
)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Looking at the response rates and account stats of zsabo, there will be no answer. Last action recorded was of June 2009, so there is nothing to expect.
However, the code you provided last is correct, and would fulfill the requirements. Regarding the absense of the OP, I recommend to accept it.
However, the code you provided last is correct, and would fulfill the requirements. Regarding the absense of the OP, I recommend to accept it.
Thank you.
Open in new window