2016-10-21 80 views
0

我正在编写批处理脚本以从其目录中提取视频文件。
到目前为止,它将进入与特定名称匹配的目录。然后,我在该目录中的每个项目上循环执行FOR循环。我想检查一个项目是否是一个目录(,我以前做过),如果它是进入该目录,否则做其他的东西。
IF声明中断批处理脚本

的问题是,第二IF声明(的检查项目是否是一个目录或不)更是打破了批处理脚本,给人的错误:

) was unexpected at this time

如果我删除IF ,代码按预期执行(我已清楚标记了哪个IF陈述在代码中)...

CODE:

@ECHO off 

SET title=%1 
SET mp4=".mp4" 
SET mkv=".mkv" 
SET avi=".avi" 
SET minimumSize=300 
SET needCompressingDir="E:\Documents\Films\Need_compressing" 

CD %needCompressingDir% 
FOR /f "delims=" %%i IN ('DIR /B') DO (
    IF %%i == %title% (
     IF EXIST %%i (
      CD %%i 
      FOR /f "delims=" %%j IN ('DIR /B') DO (
       rem this 'IF' statement breaks it 
       IF EXIST %%j (

       ) ELSE (

       ) 
      ) 
     ) ELSE (
      MOVE %%i %needCompressingDir% 
     ) 
    ) 
) 
+3

'%% j'返回的任何文件名是否都包含空格?你应该尝试使用'if exists'%% j“'(和'if exists'%% i''')来代替。 – SomethingDark

+0

@SomethingDark是的,这完全有可能发生,谢谢你的洞察力,并会尝试这种方法,当我可以 – wmash

+2

尝试'IF EXIST'%%〜j“' – npocmaka

回答

2

阅读if /?

Performs conditional processing in batch programs. 

IF [NOT] ERRORLEVEL number command 
IF [NOT] string1==string2 command 
IF [NOT] EXIST filename command 
… 
    command   Specifies the command to carry out if the condition is 
        met. Command can be followed by ELSE command which 
        will execute the command after the ELSE keyword if the 
        specified condition is FALSE 

The ELSE clause must occur on the same line as the command after the IF. For 
example: 

    IF EXIST filename. (
     del filename. 
    ) ELSE (
     echo filename. missing. 
    ) 

你可以看到,commandifelse部分的强制性一部分。 Windows cmd/.bat解释器不接受命令块():它不被视为命令。使用至少REM评论(有没有效果的命令):

 FOR /f "delims=" %%j IN ('DIR /B') DO (
      rem this 'IF' statement breaks it 
      IF EXIST "%%~j\" (
       rem directory 
      ) ELSE (
       rem not directory 
      ) 
     ) 

更新:使用IF EXIST "%%~j\"(注斜杠)的检查项目是否是一个目录或不