尝试制作一个快速而脏的VB程序,根据.dat逗号分隔文件重命名tif文件。得到此错误:
A first chance exception of type 'System.IndexOutOfRangeException' occurred in mscorlib.dll
以下是代码:
Dim objFSO, objFolder, inFile, strInLine, strOldFile, strNewFile
objFSO = CreateObject("Scripting.FileSystemObject")
'Select the folder
objFolder = objFSO.GetFolder("C:\Users\CKILLION\Desktop\planning commission\")
'Open Text File
inFile = objFSO.OpenTextFile("C:\Users\CKILLION\Desktop\consct01.dat", 1)
Do Until inFile.AtEndOfStream
'Read text file line by line and Split each line into 4 parts.
strInLine = Split(inFile.ReadLine, ", ")
'Old File name
strOldFile = strInLine(2)
'new File name
strNewFile = strInLine(1)
'Loop through the files in the folder
For Each File In objFolder.Files
'If the file name matches the old file name above
If File.Name = strOldFile Then
'Replace it
File.Name = strNewFile
End If
Next
Loop
'Close the text reader
inFile.Close()
MsgBox("Done.")
这是.dat文件中的一些文字:
“1,967”,“08-26-47”,“00001875.tif”,“。\ images \ 00001”,“5414”,“TIFF”,“9/13/2001 2:13:44 am”, “c:\ users \ ckillion \ desktop \ city commission minutes \ 1947 \ 00001875.tif”,“City Commission Minutes \ 1947”,“” “1,966”,“09-02-47”,“00001874.tif”,“。\ images \ 00001”,“28,142”,“TIFF”,“9/13/2001 2:13:14 am”,“c: \ users \ ckillion \ desktop \ city commission minutes \ 1947 \ 00001874.tif“,”City Commission Minutes \ 1947“,”“ “1,965”,“09-30-47”,“00001873.tif”,“。\ images \ 00001”,“23,342”,“TIFF”,“9/13/2001 2:12:40 am”,“c: \ users \ ckillion \ desktop \ city commission minutes \ 1947 \ 00001873.tif“,”City Commission Minutes \ 1947“,”“ “1,964”,“10-14-47”,“00001872.tif”,“。\ images \ 00001”,“38,444”,“TIFF”,“9/13/2001 2:12:28 am”,“c: \ users \ ckillion \ desktop \ city commission minutes \ 1947 \ 00001872.tif“,”City Commission Minutes \ 1947“,”“ “1,963”,“10-28-47”,“00001871.tif”,“。\ images \ 00001”,“41,466”,“TIFF”,“9/13/2001 2:12:16 am”,“c: \ users \ ckillion \ desktop \ city commission minutes \ 1947 \ 00001871.tif“,”City Commission Minutes \ 1947“,”“ “1,962”,“11-12-47”,“00001870.tif”,“。\ images \ 00001”,“29,560”,“TIFF”,“9/13/2001 2:12:00 am”,“c: \ users \ ckillion \ desktop \ city commission minutes \ 1947 \ 00001870.tif“,”City Commission Minutes \ 1947“,”“ “1,960”,“12-03-47”,“00001868.tif”,“。\ images \ 00001”,“36,435”,“TIFF”,“9/13/2001 2:11:38 am”,“c: \ users \ ckillion \ desktop \ city commission minutes \ 1947 \ 00001868.tif“,”City Commission Minutes \ 1947“,”“
答案 0 :(得分:1)
尝试在逗号
的分割中使用此行没有空格strInLine = Split(inFile.ReadLine, ",")
行中的逗号之间没有空格,因此行不会被分割,因为没有与逗号和空格匹配的分隔符(“,”)
答案 1 :(得分:0)
该错误表示您正在尝试访问不存在的索引。看来文件中的一行或多行不是您认为的预期长度。如果没有看到实际文件并知道换行符的位置,我就无法提供更多帮助。我建议分析该文件,确保每行具有预期的字段数,并且除了发生分隔之外,不会出现任何额外的逗号。希望有所帮助。