对于每个循环:如何走出条件

时间:2015-06-15 09:09:49

标签: loops vbscript conditional-statements

我很难创建一个用VBScript测试文件名的循环。确切地说,我试图在For Each循环中执行类似跳转的操作。循环应该测试文件夹中的所有文件,在某些情况下应该删除一些文件。

但是在运行时,它会删除一个文件并尝试针对下一个条件测试已删除的文件。所以我寻找一个解决方案来结束这个检查并跳转到下一个文件,但我找不到可行的解决方案。

for each file in folder.files
  if (filename = "foo.bar") then
    log.writeline("found foo.bar -> delete!")
    fs.deletefile (folder & file.name), true
    'exit for
  end if
  if (isNumeric(firstPosition)) then
    if (isNumeric(secondPosition)) then
      log.writeline("2 numbers seen -> alright!")
    else
      log.writeline("Filename is corrupt!")
      fs.copyFile file, errorFolder, true
      fs.deleteFile file, true
      'exit for 
    end if
  end if

1 个答案:

答案 0 :(得分:2)

使用else,如下所示:

for each file in folder.files
  if (filename = "foo.bar") then
    log.writeline("found foo.bar -> delete!")
    fs.deletefile (folder & file.name), true
    'exit for
  else
    if (isNumeric(firstPosition)) then
      if (isNumeric(secondPosition)) then
        log.writeline("2 numbers seen -> alright!")
      else
        log.writeline("Filename is corrupt!")
        fs.copyFile file, errorFolder, true
        fs.deleteFile file, true
        'exit for 
      end if
    else
    end if
  end if
next

甚至elseif如下

for each file in folder.files
  if (filename = "foo.bar") then
    log.writeline("found foo.bar -> delete!")
    fs.deletefile (folder & file.name), true
    'exit for
  elseif (isNumeric(firstPosition)) then
    if (isNumeric(secondPosition)) then
      log.writeline("2 numbers seen -> alright!")
    else
      log.writeline("Filename is corrupt!")
      fs.copyFile file, errorFolder, true
      fs.deleteFile file, true
      'exit for 
    end if
  else
  end if
next

VBScript中没有goto之类的声明;只有结构化控制机制。阅读VBScript User's GuideVBScript Language Reference