VB 用Fso函数处理带有问号的Unicode文件名

时间:2021-09-07 20:19:25

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

PrivateSub Ren_unicodeFile()

    DimFso, FsoFldr

    DimsFile, shrFile, lngFile AsString, ib AsLong

    DimsF1 As String, sF2 AsString, sPath1 AsString

          

 If1 = 1 Then

     CommonDialog1.FileName ="*.htm"

     CommonDialog1.ShowOpen

     sF1 = CommonDialog1.FileName

     sPath1 = Left(sF1, InStrRev(sF1, "\"))

     sF2 ="r:\new\test.htm"

       

    SetFso = CreateObject("Scripting.FileSystemObject")

    SetFsoFldr = Fso.GetFolder(sPath1).Files

    ForEach sFile InFsoFldr

        lngFile = sFile.Name

        ib = InStr(lngFile, ChrW(160))

        IfLCase(sFile.shortName) Like"*.htm" And ib > 0 Then

            shrFile = sFile.ShortPath

            Name shrFileAs sF2

        EndIf

    Next

 ElseIf1 = 0 Then

    SetFso = CreateObject("Scripting.FileSystemObject")

    IfFso.FolderExists(sPath) Then

  

        ForEach sFile InFso.GetFolder(sPath).Files

            SetFsoFile = Fso.GetFile(sFile)

            shrFile = FsoFile.ShortPath

            Name shrFileAs "r:\new\" & Format(I, "0000") & ".txt"'文件改名为0001.txt-

            I = I + 1

        Next

    EndIf

  EndIf

    SetFso = Nothing

    MsgBox"OK"

EndSub