所以我已经将范围缩小到这些代码行,没有它们就不会崩溃,它们有什么问题吗? (%DRIVE% 是在此之前已设置的用户设置变量)(并且 mm dd yy 在此设置):
set yy=%date:~10,4%
set mm=%date:~4,2%
set dd=%date:~7,2%
if exist "%DRIVE%:\MC\Logs\Log-%mm%-%dd%-%yy%.txt"
) else (
echo [Mobile Mc Log Data] > %DRIVE%:\MC\Logs\Log-%mm%-%dd%-%yy%.txt
)
echo [%TIME%] [%DATE%] - Log data that i will write later, >> "%DRIVE%:\MC\Logs\Log-%mm%-%dd%-%yy%.txt"
您的
if exist
代码仅包含条件,缺少命令。
请参阅下面,正确的语法:
IF EXIST filename (command) ELSE (command
)
if exist "%DRIVE%:\MC\Logs\Log-%mm%-%dd%-%yy%.txt" (
rem mismatched `IF` syntax
) else (
rem use double quotes ↓ ↓
echo [Mobile Mc Log Data] > "%DRIVE%:\MC\Logs\Log-%mm%-%dd%-%yy%.txt"
)
或
if NOT exist "%DRIVE%:\MC\Logs\Log-%mm%-%dd%-%yy%.txt" (
rem use double quotes ↓ ↓
echo [Mobile Mc Log Data] > "%DRIVE%:\MC\Logs\Log-%mm%-%dd%-%yy%.txt"
)
此外,
%date%
可能包含文件名中不允许的字符。尝试下一个独立于语言环境的方法:
@ECHO OFF
SETLOCAL enableextensions
for /F %%g in ('
wmic OS get LocalDateTime /value^|findstr "="
') do for /F %%G in ("%%g") do set "_%%G"
set "_LocalDateTime=%_LocalDateTime:~0,8%"
set yy=%_LocalDateTime:~0,4%
set mm=%_LocalDateTime:~4,2%
set dd=%_LocalDateTime:~6,2%
资源(必读):