I want to handle the error if anything went wrong while deleting the files and folders via batch file.
My target files and folders are on remote server not on the same server..
I tried with pushd, popd and rd commands but unfortunately it doesn't sends the errorlevel**
My current bat file as follows
set "Adminlogpathtofolder=\\%servername%\e$\Oracle\Middleware\user_projects\domains\Skandia\servers\AdminServer\logs\"
set "Adminpathtofolder=\\%servername%\e$\Oracle\Middleware\user_projects\domains\Skandia\servers\AdminServer\"
if exist "%Adminlogpathtofolder%" goto deleteadminlog
if not exist "%Adminlogpathtofolder%" goto noadminlog
:deleteadminlog
"pushd "%Adminlogpathtofolder%" && (rd /s /q "%Adminlogpathtofolder%" 2>nul & popd)"
if %errorlevel%==0 goto cachedeletesuccess
if NOT %errorlevel%==0 goto cachedeleteunsuccess
:cachedeletesuccess
set "cache_delete_status=success"
echo "Admin Cache Deletion Successfully"
goto exit
:cachedeleteunsuccess
set "cache_delete_status=failure"
echo "Unable to delete the cache"
goto :EOF
:noadminlog
echo "There is no log folder on the Admin Server"
goto exit
:exit
pushd "%Adminpathtofolder%"
for /f "delims=" %%i in ('dir /ad /b^|findstr /vlix "stage security logs"') do rd /s /q "%%i"
popd
if %errorlevel%==0 goto cachedeletesuccess
if NOT %errorlevel%==0 goto cachedeleteunsuccess
:cachedeletesuccess
set "cache_delete_status=success"
echo "Cache Deletion Successfully"
goto exit
:cachedeleteunsuccess
set "cache_delete_status=failure"
echo "Unable to delete the cache"
goto :EOF
Can anyone help me on this..
MY Requirement with following conditions
- Delete all the files and folders expect the parent folder (log) on a remote server for example my remote server path
"Adminlogpathtofolder=\%servername%\e$\Oracle\Middleware\user_projects\domains\Skandia\servers\AdminServer\logs\"
- Delete all files and folders with some exclusion say for example this is the following path
"Adminpathtofolder=\%servername%\e$\Oracle\Middleware\user_projects\domains\Skandia\servers\AdminServer\"
I want to delete all the files and folders except some three folders and its content (logs, security, stage)
- I want to handle the error if something went wrong while deleting the files or folder the script needs to come out and echo the error which I got in cmd.
Answer
Why not use IF EXIST
again? From my experience ERRORLEVEL
is weird sometimes (as is it in this case, read more), so as long as there is an alternative (which works), use it!
Anyways, here is my tested and working solution:
PUSHD "%Adminlogpathtofolder%"
FOR /F "delims=" %%i IN ('DIR /A:D /B') DO (
RD /S /Q "%%i" >nul 2>&1
IF EXIST "%%i" ECHO Failed to delete "%%i" && SET "cache_delete_status=failure"
)
IF "cache_delete_status"=="failure" (ECHO Unable to delete the admin cache) ELSE (ECHO Admin Cache Deletion Successfully & SET "cache_delete_status=success")
And for the 2nd part:
PUSHD "%Adminpathtofolder%"
FOR /F "delims=" %%i IN ('DIR /A:D /B^|FINDSTR /VLIX "stage security logs"') DO (
RD /S /Q "%%i" >nul 2>&1
IF EXIST "%%i" ECHO Failed to delete "%%i" && SET "cache_delete_status=failure"
)
IF "cache_delete_status"=="failure" (ECHO Unable to delete the cache) ELSE (ECHO Cache Deletion Successfully & SET "cache_delete_status=success")
I don't know if this is on purpose or not, but you use the variable chache_delete_status
for both parts, so it might shows an error in the 2nd part even if there was none. I recommend you use a different variable to be checked in the 2nd part if you don't want this to happen.
No comments:
Post a Comment