VB 用Fso函数处理带有问号的Unicode文件名
VB6 编写程序时,经常遇到文件名中含有Unicode字符的文件名,常规的Name语句,Open语句,甚至Windows 中API函数都无法处理这里的文件名,因为这些文件在VB String字符串中变成了?,而问句是无法处理的,找了很多资料都没能解决这个问题,原来以为Fso仅能处理文本文件流,但是其目录对象、文件对象中有些方法,可以解决这一难题。
故此记录下来,以备后用。程序代码,随写随记,就没有规范化了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
Private Sub
Ren_unicodeFile()
Dim Fso, FsoFldr
Dim sFile, shrFile, lngFile
As String , ib
As Long
Dim sF1
As String , sF2
As String , sPath1
As String
If 1 = 1
Then
CommonDialog1.FileName = "*.htm"
CommonDialog1.ShowOpen
sF1 = CommonDialog1.FileName
sPath1 = Left(sF1, InStrRev(sF1, "\"))
sF2 = "r:\new\test.htm"
Set Fso = CreateObject( "Scripting.FileSystemObject" )
Set FsoFldr = Fso.GetFolder(sPath1).Files
For Each
sFile In FsoFldr
lngFile = sFile.Name
ib = InStr(lngFile, ChrW(160))
If LCase(sFile.shortName)
Like "*.htm"
And ib > 0 Then
shrFile = sFile.ShortPath
Name shrFile As
sF2
End If
Next
ElseIf 1 = 0
Then
Set Fso = CreateObject( "Scripting.FileSystemObject" )
If Fso.FolderExists(sPath)
Then
For Each
sFile In Fso.GetFolder(sPath).Files
Set FsoFile = Fso.GetFile(sFile)
shrFile = FsoFile.ShortPath
Name shrFile As
"r:\new\" & Format(I, " 0000 ") & " .txt"
I = I + 1
Next
End If
End If
Set Fso =
Nothing
MsgBox "OK"
End Sub
|