Vbscript 运行带有异常的IF-then-else循环

Vbscript 运行带有异常的IF-then-else循环,vbscript,Vbscript,我必须(将)用FDM(免费下载管理器)从INSEE(法国统计局)下载大约一百个文件。因此,我使用一个脚本来重命名这些下载的文件,以找到每个社区的五位插入代码(在文件的第二行),并将它们放在文件名之前,因此rp009_cc_pop.xls变为67181_rp009_cc_pop.txt。那些下载的.xls文件不是真正的Excel文件,而是扩展名错误的纯文本文件。 有时会有一个下载的文件,其内容中没有“commune”(带有空格)一词。如何跳过这些文件的重命名。所以一个好的建议是昂贵的。 以下是脚本

我必须(将)用FDM(免费下载管理器)从INSEE(法国统计局)下载大约一百个文件。因此,我使用一个脚本来重命名这些下载的文件,以找到每个社区的五位插入代码(在文件的第二行),并将它们放在文件名之前,因此rp009_cc_pop.xls变为67181_rp009_cc_pop.txt。那些下载的.xls文件不是真正的Excel文件,而是扩展名错误的纯文本文件。 有时会有一个下载的文件,其内容中没有“commune”(带有空格)一词。如何跳过这些文件的重命名。所以一个好的建议是昂贵的。 以下是脚本:

Dim objFso, strFolder
 ' Begin Main
 Set objFso = CreateObject("Scripting.FileSystemObject")
 strFolder = objFso.GetParentFolderName(WScript.ScriptFullName)  
 If objFso.FolderExists(strFolder) Then
 Call GetJspFiles(objFso.GetFolder(strFolder))
End If
Set objFso = Nothing
' End Main

Sub GetJspFiles(ByRef objFolder)
Dim objFile, objSubFolder
For Each objFile In objFolder.Files
    If LCase(objFso.GetExtensionName(objFile.Name)) = "xls" Then
        Call JSPRename(objFile.Path, objFolder.Path)
    End If
Next
For Each objSubFolder In objFolder.SubFolders
  Call GetJSPRename(objSubFolder)
Next
' objFile.Close
End Sub

Sub JSPRename(ByRef strPath, ByRef strFolder)
Dim arrText, strText, strTextLine, Position , objJspFile, newFilename, strVerz
Set objJspFile = objFso.OpenTextFile(strPath)
arrText = Split(objJspFile.ReadAll, vbCrLf) ' split into lines
For Each strTextLine In arrText
  If strTextLine <> "" Then
     strText = Trim(strTextLine)
   If Instr(1,strText,"Commune ",1) Then
    Position=Instr(1, strText, "(",1)
   newFilename=mid(strText,Position+1, 5) ' 5 Characters long
   else
   end if
else
' newFilename=......  ' <== skip those files without the word 'commune ' from renaming
  end if
Next
objJspFile.Close
cutlastoff=Left(strPath, inStrRev(strPath,"\")-1)
strNewName = cutlastoff & "\" & newFilename & "_rp009_cc_pop.txt"  ' cutting the filename
Wscript.echo "New Name: " & strNewName & vbcrlf & "Old Name: " & strPath 
'!! only for Showing the results

objFSO.MoveFile strPath, strNewName
End Sub
Dim objFso,strFolder
“开始干吧
设置objFso=CreateObject(“Scripting.FileSystemObject”)
strFolder=objFso.GetParentFolderName(WScript.ScriptFullName)
如果objFso.folder存在(strFolder),则
调用GetJspFiles(objFso.GetFolder(strFolder))
如果结束
设置objFso=Nothing
'尾干管
子GetJspFiles(ByRef objFolder)
Dim objFile,objSubFolder
对于objFolder.Files中的每个objFile
如果LCase(objFso.GetExtensionName(objFile.Name))=“xls”,则
调用JSPRename(objFile.Path,objFolder.Path)
如果结束
下一个
对于objFolder.SubFolders中的每个objSubFolder
调用GetJSPRename(objSubFolder)
下一个
'objFile.Close
端接头
子JSPRename(ByRef strPath、ByRef strFolder)
Dim arrText、strText、strTextLine、Position、objJspFile、newFilename、strVerz
设置objJspFile=objFso.OpenTextFile(strPath)
arrText=Split(objJspFile.ReadAll,vbCrLf)'拆分为行
对于arrText中的每个strTextLine
如果是strTextLine“”,则
strText=修剪(strTextLine)
如果Instr(1,strText,“Commune”,1),则
位置=仪表(1,strText,“(”,1)
newFilename=mid(strText,位置+1,5)5个字符长
其他的
如果结束
其他的

“newFilename=…”我通常在原始for循环和任何其他循环中使用嵌套循环来跳过项目。 您必须确保嵌套循环不会循环,这样您的原始脚本就不会受到影响,因此在False时使用Do{…}循环

For Each strTextLine In arrText : Do 'Easy skip door
  If strTextLine <> "" Then
     strText = Trim(strTextLine)
   If Instr(1,strText,"Commune ",1) Then
    Position=Instr(1, strText, "(",1)
   newFilename=mid(strText,Position+1, 5) ' 5 Characters long
   else
   end if
else
   Exit Do 'Take the easy skip door
' newFilename=......  ' <== skip those files without the word 'commune ' from renaming
  end if
Loop While False : Next 'Don't forget to close the skip door.

谢谢,但这给我带来了一个无效的“exit”语句,它与exit DOTSANKS a ot一起用于您的第二个脚本-它工作正常。带有正则表达式对象的语句不工作,因此我将始终使用另一个。再次非常感谢!!@Gurkenhobel如果这个答案对您有效,
Sub JSPRename(ByRef strPath, ByRef strFolder)

    Dim boolNumberFound, arrText, strText, strTextLine, Position , objJspFile, newFilename, strVerz

    boolNumberFound = False '<-----------
    Set objJspFile = objFso.OpenTextFile(strPath)
    arrText = Split(objJspFile.ReadAll, vbCrLf) ' split into lines


    For Each strTextLine In arrText
        If strTextLine <> "" Then
            strText = Trim(strTextLine)
            If Instr(1,strText,"Commune ",1) Then
                Position=Instr(1, strText, "(",1)
                newFilename=mid(strText,Position+1, 5) ' 5 Characters long
                boolNumberFound = True '<-----------
                Exit For
            end if
        end if
    Next

    If boolNumberFound Then '<-----------
        objJspFile.Close
        cutlastoff=Left(strPath, inStrRev(strPath,"\")-1)
        strNewName = cutlastoff & "\" & newFilename & "_rp009_cc_pop.txt"  ' cutting the filename
        Wscript.echo "New Name: " & strNewName & vbcrlf & "Old Name: " & strPath 
        '!! only for Showing the results

        objFSO.MoveFile strPath, strNewName
    Else
        Wscript.echo "Commune # not found in file: '" & strPath & "'. File was skipped."
    End If

End Sub
Sub JSPRename(ByRef strPath, ByRef strFolder)

    Dim objJspFile, CommuneNo, arr_path, strNewName
    Dim re, re_matches, re_first_match

    Set objJspFile = objFso.OpenTextFile(strPath)
    Set re = New RegExp

    re.Pattern = "(Commune *\()(\d{5})" 
    'A match will return 2 submatches:
        'submatch(0)="Commune("
        'submatch(1)=[Your 5 digits]

    Set re_matches = re.Execute(objJspFile.ReadAll)

    If re_matches.count Then 'There is at least 1 occurence
        Set re_first_match = re_matches(0)
        CommuneNo = re_first_match.SubMatches(1)
    Else
        WScript.Echo strPath & " did not contain Commnune# and was skipped."
        Exit Sub 'This exits your sub without renaming the file
    End If

    Set re = Nothing

    objJspFile.Close
    Set objJspFile = Nothing


    arr_path = Split(strPath, "\")
    arr_path(UBound(arr_path)) = CommuneNo & "_" & arr_path(UBound(arr_path))
    strNewName = Join(arr_path, "\")

    Wscript.echo "New Name: " & strNewName & vbcrlf & "Old Name: " & strPath 
    '!! only for Showing the results

    objFSO.MoveFile strPath, strNewName

End Sub