对一个大任务 BATCH 使用多个 if 存在和 if 不存在时遇到问题
Posted
技术标签:
【中文标题】对一个大任务 BATCH 使用多个 if 存在和 if 不存在时遇到问题【英文标题】:Having an issue with using multiple if exist and if not exist for one big task BATCH 【发布时间】:2021-09-09 21:03:11 【问题描述】:所以我正在处理一个批处理文件,当您打开此文件时将清除这些特定文件,但我想让很多人公开发布它,但我在使用 4 IF NOT EXIST 时遇到问题和 4 IF EXIST 行来执行这些。
我尝试寻找多个来源或尝试过此操作的人,但我似乎找不到任何能接近运行这么大任务的东西。
这是我当前的代码:
title Zyorby Cleaner V2 \ ENB Remover
cls
if exist C:\Program Files\Epic Games\GTAV\enbseries (
if exist D:\Program Files\Epic Games\GTAV\enbseries (
if exist C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbseries (
if exist D:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbseries (
rmdir /s /q "C:\Program Files\Epic Games\GTAV\enbseries"
del /s /q "C:\Program Files\Epic Games\GTAV\d3d11.dll"
del /s /q "C:\Program Files\Epic Games\GTAV\d3dcompiler_46e.dll"
del /s /q "C:\Program Files\Epic Games\GTAV\enbadaptation.fx"
del /s /q "C:\Program Files\Epic Games\GTAV\enbbloom.fx"
del /s /q "C:\Program Files\Epic Games\GTAV\enbeffect.fx"
del /s /q "C:\Program Files\Epic Games\GTAV\enbeffectpostpass.fx"
del /s /q "C:\Program Files\Epic Games\GTAV\enblens.fx"
del /s /q "C:\Program Files\Epic Games\GTAV\enblightsprite.fx"
del /s /q "C:\Program Files\Epic Games\GTAV\enblocal.ini""
del /s /q "C:\Program Files\Epic Games\GTAV\enbseries.ini"
rmdir /s /q "D:\Program Files\Epic Games\GTAV\enbseries"
del /s /q "D:\Program Files\Epic Games\GTAV\d3d11.dll"
del /s /q "D:\Program Files\Epic Games\GTAV\d3dcompiler_46e.dll"
del /s /q "D:\Program Files\Epic Games\GTAV\enbadaptation.fx"
del /s /q "D:\Program Files\Epic Games\GTAV\enbbloom.fx"
del /s /q "D:\Program Files\Epic Games\GTAV\enbeffect.fx"
del /s /q "D:\Program Files\Epic Games\GTAV\enbeffectpostpass.fx"
del /s /q "D:\Program Files\Epic Games\GTAV\enblens.fx"
del /s /q "D:\Program Files\Epic Games\GTAV\enblightsprite.fx"
del /s /q "D:\Program Files\Epic Games\GTAV\enblocal.ini"
del /s /q "D:\Program Files\Epic Games\GTAV\enbseries.ini"
rmdir /s /q "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbseries"
del /s /q "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\d3d11.dll"
del /s /q "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\d3dcompiler_46e.dll"
del /s /q "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbadaptation.fx"
del /s /q "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbbloom.fx"
del /s /q "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbeffect.fx"
del /s /q "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbeffectpostpass.fx"
del /s /q "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enblens.fx"
del /s /q "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enblightsprite.fx"
del /s /q "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enblocal.ini"
del /s /q "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbseries.ini"
rmdir /s /q "D:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbseries"
del /s /q "D:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\d3d11.dll"
del /s /q "D:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\d3dcompiler_46e.dll"
del /s /q "D:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbadaptation.fx"
del /s /q "D:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbbloom.fx"
del /s /q "D:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbeffect.fx"
del /s /q "D:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbeffectpostpass.fx"
del /s /q "D:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enblens.fx"
del /s /q "D:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enblightsprite.fx"
del /s /q "D:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enblocal.ini"
del /s /q "D:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbseries.ini"
echo %DATE% %TIME% - ENB has been removed >> %userprofile%\Downloads\logs.txt
cls
echo.
echo ----------------------------------
echo - your ENB has been cleaned -
echo - to go back to the home screen: -
echo ----------------------------------
echo.
)
)
)
)
if not exist C:\Program Files\Epic Games\GTAV\enbseries (
if not exist D:\Program Files\Epic Games\GTAV\enbseries (
if not exist C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbseries (
if not exist D:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbseries (
echo %DATE% %TIME% - could not find enbseries folder >> %userprofile%\Downloads\logs.txt
cls
echo.
echo --------------------------------------
echo - your ENB could not be found :( -
echo --------------------------------------
echo.
)
)
)
)
)
pause
goto main```
【问题讨论】:
【参考方案1】:我很快就解决了这个问题,我使用了很多其他方法制作了一个简单的清单,但它确实有效。我不确定是否有更有效的方法,但目前这对我有用。
这是固定代码:
:x
title Zyorby Cleaner V2 \ ENB Remover
cls
if exist "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbseries" (
goto enbyes
) else (
if exist "D:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbseries" (
goto enbyes
) else (
if exist "C:\Program Files\Epic Games\GTAV\enbseries" (
goto enbyes
) else (
if exist "D:\Program Files\Epic Games\GTAV\enbseries" (
goto enbyes
) else (
goto enbno
)
)
)
)
:enbyes
rmdir /s /q "C:\Program Files\Epic Games\GTAV\enbseries"
del /s /q "C:\Program Files\Epic Games\GTAV\d3d11.dll"
del /s /q "C:\Program Files\Epic Games\GTAV\d3dcompiler_46e.dll"
del /s /q "C:\Program Files\Epic Games\GTAV\enbadaptation.fx"
del /s /q "C:\Program Files\Epic Games\GTAV\enbbloom.fx"
del /s /q "C:\Program Files\Epic Games\GTAV\enbeffect.fx"
del /s /q "C:\Program Files\Epic Games\GTAV\enbeffectpostpass.fx"
del /s /q "C:\Program Files\Epic Games\GTAV\enblens.fx"
del /s /q "C:\Program Files\Epic Games\GTAV\enblightsprite.fx"
del /s /q "C:\Program Files\Epic Games\GTAV\enblocal.ini""
del /s /q "C:\Program Files\Epic Games\GTAV\enbseries.ini"
rmdir /s /q "D:\Program Files\Epic Games\GTAV\enbseries"
del /s /q "D:\Program Files\Epic Games\GTAV\d3d11.dll"
del /s /q "D:\Program Files\Epic Games\GTAV\d3dcompiler_46e.dll"
del /s /q "D:\Program Files\Epic Games\GTAV\enbadaptation.fx"
del /s /q "D:\Program Files\Epic Games\GTAV\enbbloom.fx"
del /s /q "D:\Program Files\Epic Games\GTAV\enbeffect.fx"
del /s /q "D:\Program Files\Epic Games\GTAV\enbeffectpostpass.fx"
del /s /q "D:\Program Files\Epic Games\GTAV\enblens.fx"
del /s /q "D:\Program Files\Epic Games\GTAV\enblightsprite.fx"
del /s /q "D:\Program Files\Epic Games\GTAV\enblocal.ini"
del /s /q "D:\Program Files\Epic Games\GTAV\enbseries.ini"
rmdir /s /q "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbseries"
del /s /q "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\d3d11.dll"
del /s /q "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\d3dcompiler_46e.dll"
del /s /q "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbadaptation.fx"
del /s /q "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbbloom.fx"
del /s /q "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbeffect.fx"
del /s /q "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbeffectpostpass.fx"
del /s /q "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enblens.fx"
del /s /q "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enblightsprite.fx"
del /s /q "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enblocal.ini"
del /s /q "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbseries.ini"
rmdir /s /q "D:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbseries"
del /s /q "D:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\d3d11.dll"
del /s /q "D:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\d3dcompiler_46e.dll"
del /s /q "D:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbadaptation.fx"
del /s /q "D:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbbloom.fx"
del /s /q "D:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbeffect.fx"
del /s /q "D:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbeffectpostpass.fx"
del /s /q "D:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enblens.fx"
del /s /q "D:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enblightsprite.fx"
del /s /q "D:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enblocal.ini"
del /s /q "D:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\enbseries.ini"
echo %DATE% %TIME% - ENB has been removed >> %userprofile%\Downloads\logs.txt
cls
echo.
echo ----------------------------------
echo - your ENB has been cleaned -
echo - to go back to the home screen: -
echo ----------------------------------
echo.
pause
goto main
:enbno
echo %DATE% %TIME% - could not find enbseries folder >> %userprofile%\Downloads\logs.txt
cls
echo.
echo --------------------------------------
echo - your ENB could not be found -
echo --------------------------------------
echo.
pause
goto main
说明:
基本上,它会遍历用户文件并检查文件夹是否存在,如果存在,它将被发送到 :enbyes 部分,该部分只是删除文件夹和文件的部分,但如果找不到文件夹它将它与 else 一起发送到链中,并继续执行此操作,直到它用完选项为止。一旦代码到达那里,它就会将其发送到 :enbno 部分,这只是说它找不到它想要找到的东西。
【讨论】:
为什么要使用多个 IF - ELSE 语句?您可以简单地使用if exist "%ProgramFiles%\Steam\steamapps\common\Grand Theft Auto V\enbseries" set "GamePath=%ProgramFiles%\Steam\steamapps\common\Grand Theft Auto V\enbseries" & goto DeleteGameFiles
并在下一行使用 if exist "%ProgramFiles(x86)%\Steam\steamapps\common\Grand Theft Auto V\enbseries" set "GamePath=%ProgramFiles(x86)%\Steam\steamapps\common\Grand Theft Auto V\enbseries" & goto DeleteGameFiles
和 ...
... 在下一行 if exist "%ProgramW6432%\Steam\steamapps\common\Grand Theft Auto V\enbseries" set "GamePath=%ProgramW6432%\Steam\steamapps\common\Grand Theft Auto V\enbseries" & goto DeleteGameFiles
和下一行是错误消息,因为找不到游戏路径,goto :EOF
在此错误条件下退出此批处理文件的处理。然后是:DeleteGameFiles
行,在其他命令行下方使用%GamePath%
。但是,手动删除文件和文件夹始终是错误的方法。运行游戏的卸载程序,而不是删除其文件/文件夹。以上是关于对一个大任务 BATCH 使用多个 if 存在和 if 不存在时遇到问题的主要内容,如果未能解决你的问题,请参考以下文章
万树IT:Spring Batch批处理框架技巧,让你不再重复造轮子
首次使用批处理框架 Spring Batch ,被震撼到了,太强大...