如果通过批处理文件删除文件和文件夹时出现任何问题,我想处理错误。我的目标文件和文件夹位于远程服务器上,而不是在同一台服务器上。 我尝试使用 pushd、popd 和 rd 命令但不幸的是它没有发送错误级别**
我当前的bat文件如下
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
谁可以帮我这个事..
MY 要求符合以下条件
- 删除所有文件和文件夹期望父文件夹(日志)在远程服务器上,例如我的远程服务器路径
“Adminlogpathtofolder=\%servername%\e$\Oracle\Middleware\user_projects\domains\Skandia\servers\AdminServer\logs\”
- 删除所有文件和文件夹,但排除一些文件和文件夹,例如以下路径
“Adminpathtofolder=\%servername%\e$\Oracle\Middleware\user_projects\domains\Skandia\servers\AdminServer\”
我想删除除三个文件夹及其内容(日志、安全、阶段)之外的所有文件和文件夹
- 我想处理错误如果在删除文件或文件夹时出现问题,则脚本需要出来并回显我在 cmd 中收到的错误。
答案1
为什么不再次使用IF EXIST
?根据我的经验,ERRORLEVEL
有时会很奇怪(就像在这种情况下一样,阅读更多),因此只要有替代方案(可行的),就使用它!
无论如何,这是我经过测试并且有效的解决方案:
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")
第二部分:
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")
我不知道这是否是故意的,但你chache_delete_status
在两个部分都使用了变量,所以即使没有错误,也可能在第二部分显示错误。如果你不想发生这种情况,我建议你在第二部分使用不同的变量进行检查。