批量随机崩溃为什么?

时间:2017-01-11 17:16:54

标签: batch-file cmd

所以我把它缩小到这些代码行,没有它们它不会崩溃,它们有什么问题? (%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"

2 个答案:

答案 0 :(得分:0)

您的if exists代码仅包含条件且缺少命令。

请参阅下面的正确语法:

IF EXIST filename (command) ELSE (command

答案 1 :(得分:0)

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%可能包含not allowed in file names字符。尝试下一个与语言环境无关的方法:

@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%

资源(必读):