运行IF然后else循环,但有例外



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

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

提前感谢的任何欢迎提示

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

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.

编辑:很抱歉,我想我太兴奋了,没能发第一篇帖子。我会离开原来的帖子,提醒我下次不要太快。

下面的Sub将把一个布尔变量初始化为False以用作开关。然后,它将查找您的值,如果找到布尔变量,则将其设置为True,并根据该布尔变量的值执行代码的其余部分。

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的新版本。它们更快、更灵活。不过我没有测试。随心所欲地使用它。

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

最新更新