批处理文件嵌套for循环和if语句未运行

时间:2015-07-16 18:44:51

标签: windows batch-file cmd

::read in csv column 1 through 5
for /F "tokens=1,2,3,4,5 delims=," %%A in (file.txt) do (
::look for file extension foo in the first column
    IF EXIST findstr ".foo" \%%A(
        do some stuff >> Z:\completed.txt ) ELSE (
::try to change into a directory
            cd /d %%A
::go through all foo files and do something with them
            for /d %%F in (dir *.foo) do (
                do some stuff >> Z:\completed.txt 
            ) )
)

当我运行它时,ELSE是“意外”。我确定批处理有奇怪的括号处理,但Notepad ++似乎认为它们都是有效的。

2 个答案:

答案 0 :(得分:0)

这是伪代码的一些问题!

很难说,但我认为你正在寻找类似的东西:

>completed.txt (
  for /f "tokens=1-5 delims=," %%A in (file.txt) do (
    if /i "%%~xA" equ ".foo" (
      echo Doing stuff with %%A: a presumed file with .foo extension
    ) else pushd "%%~A" && (
      for %%F in (*.foo) do (
        echo Doing stuff with file %%F in folder %%A
      )
      popd
    )
  )
)

但逻辑假设.foo的扩展名意味着一个文件,可能是也可能不是。

答案 1 :(得分:0)

阅读Comments within bracketed code blocks.

避免在括号内的代码块或tar zxvf SDL_mixer-1.2.12.tar.gz cd SDL_mixer-1.2.12 ./configure make sudo make install 命令中使用:labels:: labe-like comments。始终使用FOR代替rem

证明:一个简单的脚本(任何人都可以提供更复杂的示例):

::

然而,很难理解你的目标......