Option Explicit Sub test() Dim FSO As New FileSystemObject, bFile As File For Each bFile In FSO.GetFolder("f:\").Files If bFile.Name Like "w?.txt" Then‘’这个文件名就是Alt+0160输入的 bFile.OpenAsTextStream(ForWriting).Write "sdvssnvs" Debug.Print bFile.OpenAsTextStream(ForReading).ReadAll End If Next End Sub
...
#13
啊,可以写入呀。
Option Explicit Sub test() Dim FSO As New FileSystemObject, bFile As File For Each bFile In FSO.GetFolder("f:\").Files If bFile.Name Like "w?.txt" Then‘’这个文件名就是Alt+0160输入的 bFile.OpenAsTextStream(ForWriting).Write "sdvssnvs" Debug.Print bFile.OpenAsTextStream(ForReading).ReadAll End If Next End Sub
我主要是要写入二进制数据,用的是Open语句
Dim c(1) As Byte c(0) = &HFF c(1) = &HD8
Open bFile.Name For Binary As #1 Put #1, , c Close #1
提示错误的文件名
#14
我把1.txt的文件名加了Alt + 0160。
全部要用Unicode版本的函数进行处理。
Option Explicit
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileW" (ByVal lpFileName As Long, lpFindFileData As WIN32_FIND_DATA) As Long Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileW" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Any) As Long Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileW" (ByVal lpFileName As Long, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
Private Type FILETIME dwLowDateTime As Long dwHighDateTime As Long End Type
Private Type WIN32_FIND_DATA dwFileAttributes As Long ftCreationTime As FILETIME ftLastAccessTime As FILETIME ftLastWriteTime As FILETIME nFileSizeHigh As Long nFileSizeLow As Long dwReserved0 As Long dwReserved1 As Long cFileName(MAX_PATH * 2 - 1) As Byte 'Unicode 需要双倍长度 cAlternate(14 * 2 + 1) As Byte 'Unicode 需要双倍长度 End Type
Const eeEndOfFile = 62 'Input past end of file
Function StripNulls(bytes() As Byte) As String Dim i As Long For i = 0 To UBound(bytes) Step 2 If bytes(i) = 0 And bytes(i + 1) = 0 Then Exit For End If Next StripNulls = LeftB(bytes, i) End Function
Function FindFiles(path As String, searchPattern As String) As Collection Dim oFiles As Collection Dim hSearch As Long Dim bFound As Boolean Dim sFileName As String Dim WFD As WIN32_FIND_DATA
Set oFiles = New Collection If Right(path, 1) <> "\" Then path = path & "\"
sFileName = path & searchPattern hSearch = FindFirstFile(StrPtr(sFileName), WFD) If hSearch <> INVALID_HANDLE_VALUE Then bFound = True While bFound sFileName = StripNulls(WFD.cFileName) If (sFileName <> ".") And (sFileName <> "..") Then oFiles.Add path & sFileName End If bFound = FindNextFile(hSearch, WFD) Wend Call FindClose(hSearch) End If
Set FindFiles = oFiles End Function
Function ReadAllText(ByVal path As String) As String Dim hFile As Long Dim nSize As Long Dim bytes() As Byte Dim lBytesRead As Long
ReadAllText = StrConv(bytes, vbUnicode) End Function
Sub Main() Dim oFiles As Collection Dim i As Long Dim s As String
Set oFiles = FindFiles(App.path, "1*.*") For i = 1 To oFiles.Count s = oFiles(i) Debug.Print s, "U+" & Hex(AscW(Mid$(s, InStrRev(s, "\") + 2, 1))) Debug.Print ReadAllText(s) Next End Sub
D:\temp\UnicodeFileName\1?.TXT U+A0 abc中文
又:不如改用VB.Net吧,一点问题都没有。
Imports System.Text Imports System.IO
Module Module1
Sub Main() For Each s As String In My.Computer.FileSystem.GetFiles("D:\temp\UnicodeFileName\", FileIO.SearchOption.SearchTopLevelOnly, "1*.*") Console.WriteLine(s) Console.WriteLine(BitConverter.ToString(Encoding.Unicode.GetBytes(Path.GetFileName(s)))) Console.WriteLine(My.Computer.FileSystem.ReadAllText(s, Encoding.Default)) Next Console.ReadLine() End Sub
End Module
#15
试试:文件名变量不要使用 String 类型。
在中文系统下,如果是字符串类型变量,不可识别的字符会统统被“自动”替换成?。
#16
我把1.txt的文件名加了Alt + 0160。
全部要用Unicode版本的函数进行处理。
Option Explicit
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileW" (ByVal lpFileName As Long, lpFindFileData As WIN32_FIND_DATA) As Long Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileW" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Any) As Long Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileW" (ByVal lpFileName As Long, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
Private Type FILETIME dwLowDateTime As Long dwHighDateTime As Long End Type
Private Type WIN32_FIND_DATA dwFileAttributes As Long ftCreationTime As FILETIME ftLastAccessTime As FILETIME ftLastWriteTime As FILETIME nFileSizeHigh As Long nFileSizeLow As Long dwReserved0 As Long dwReserved1 As Long cFileName(MAX_PATH * 2 - 1) As Byte 'Unicode 需要双倍长度 cAlternate(14 * 2 + 1) As Byte 'Unicode 需要双倍长度 End Type
Const eeEndOfFile = 62 'Input past end of file
Function StripNulls(bytes() As Byte) As String Dim i As Long For i = 0 To UBound(bytes) Step 2 If bytes(i) = 0 And bytes(i + 1) = 0 Then Exit For End If Next StripNulls = LeftB(bytes, i) End Function
Function FindFiles(path As String, searchPattern As String) As Collection Dim oFiles As Collection Dim hSearch As Long Dim bFound As Boolean Dim sFileName As String Dim WFD As WIN32_FIND_DATA
Set oFiles = New Collection If Right(path, 1) <> "\" Then path = path & "\"
sFileName = path & searchPattern hSearch = FindFirstFile(StrPtr(sFileName), WFD) If hSearch <> INVALID_HANDLE_VALUE Then bFound = True While bFound sFileName = StripNulls(WFD.cFileName) If (sFileName <> ".") And (sFileName <> "..") Then oFiles.Add path & sFileName End If bFound = FindNextFile(hSearch, WFD) Wend Call FindClose(hSearch) End If
Set FindFiles = oFiles End Function
Function ReadAllText(ByVal path As String) As String Dim hFile As Long Dim nSize As Long Dim bytes() As Byte Dim lBytesRead As Long
ReadAllText = StrConv(bytes, vbUnicode) End Function
Sub Main() Dim oFiles As Collection Dim i As Long Dim s As String
Set oFiles = FindFiles(App.path, "1*.*") For i = 1 To oFiles.Count s = oFiles(i) Debug.Print s, "U+" & Hex(AscW(Mid$(s, InStrRev(s, "\") + 2, 1))) Debug.Print ReadAllText(s) Next End Sub
D:\temp\UnicodeFileName\1?.TXT U+A0 abc中文
又:不如改用VB.Net吧,一点问题都没有。
Imports System.Text Imports System.IO
Module Module1
Sub Main() For Each s As String In My.Computer.FileSystem.GetFiles("D:\temp\UnicodeFileName\", FileIO.SearchOption.SearchTopLevelOnly, "1*.*") Console.WriteLine(s) Console.WriteLine(BitConverter.ToString(Encoding.Unicode.GetBytes(Path.GetFileName(s)))) Console.WriteLine(My.Computer.FileSystem.ReadAllText(s, Encoding.Default)) Next Console.ReadLine() End Sub
Option Explicit Sub test() Dim FSO As New FileSystemObject, bFile As File For Each bFile In FSO.GetFolder("f:\").Files If bFile.Name Like "w?.txt" Then‘’这个文件名就是Alt+0160输入的 bFile.OpenAsTextStream(ForWriting).Write "sdvssnvs" Debug.Print bFile.OpenAsTextStream(ForReading).ReadAll End If Next End Sub
...
#13
啊,可以写入呀。
Option Explicit Sub test() Dim FSO As New FileSystemObject, bFile As File For Each bFile In FSO.GetFolder("f:\").Files If bFile.Name Like "w?.txt" Then‘’这个文件名就是Alt+0160输入的 bFile.OpenAsTextStream(ForWriting).Write "sdvssnvs" Debug.Print bFile.OpenAsTextStream(ForReading).ReadAll End If Next End Sub
我主要是要写入二进制数据,用的是Open语句
Dim c(1) As Byte c(0) = &HFF c(1) = &HD8
Open bFile.Name For Binary As #1 Put #1, , c Close #1
提示错误的文件名
#14
我把1.txt的文件名加了Alt + 0160。
全部要用Unicode版本的函数进行处理。
Option Explicit
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileW" (ByVal lpFileName As Long, lpFindFileData As WIN32_FIND_DATA) As Long Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileW" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Any) As Long Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileW" (ByVal lpFileName As Long, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
Private Type FILETIME dwLowDateTime As Long dwHighDateTime As Long End Type
Private Type WIN32_FIND_DATA dwFileAttributes As Long ftCreationTime As FILETIME ftLastAccessTime As FILETIME ftLastWriteTime As FILETIME nFileSizeHigh As Long nFileSizeLow As Long dwReserved0 As Long dwReserved1 As Long cFileName(MAX_PATH * 2 - 1) As Byte 'Unicode 需要双倍长度 cAlternate(14 * 2 + 1) As Byte 'Unicode 需要双倍长度 End Type
Const eeEndOfFile = 62 'Input past end of file
Function StripNulls(bytes() As Byte) As String Dim i As Long For i = 0 To UBound(bytes) Step 2 If bytes(i) = 0 And bytes(i + 1) = 0 Then Exit For End If Next StripNulls = LeftB(bytes, i) End Function
Function FindFiles(path As String, searchPattern As String) As Collection Dim oFiles As Collection Dim hSearch As Long Dim bFound As Boolean Dim sFileName As String Dim WFD As WIN32_FIND_DATA
Set oFiles = New Collection If Right(path, 1) <> "\" Then path = path & "\"
sFileName = path & searchPattern hSearch = FindFirstFile(StrPtr(sFileName), WFD) If hSearch <> INVALID_HANDLE_VALUE Then bFound = True While bFound sFileName = StripNulls(WFD.cFileName) If (sFileName <> ".") And (sFileName <> "..") Then oFiles.Add path & sFileName End If bFound = FindNextFile(hSearch, WFD) Wend Call FindClose(hSearch) End If
Set FindFiles = oFiles End Function
Function ReadAllText(ByVal path As String) As String Dim hFile As Long Dim nSize As Long Dim bytes() As Byte Dim lBytesRead As Long
ReadAllText = StrConv(bytes, vbUnicode) End Function
Sub Main() Dim oFiles As Collection Dim i As Long Dim s As String
Set oFiles = FindFiles(App.path, "1*.*") For i = 1 To oFiles.Count s = oFiles(i) Debug.Print s, "U+" & Hex(AscW(Mid$(s, InStrRev(s, "\") + 2, 1))) Debug.Print ReadAllText(s) Next End Sub
D:\temp\UnicodeFileName\1?.TXT U+A0 abc中文
又:不如改用VB.Net吧,一点问题都没有。
Imports System.Text Imports System.IO
Module Module1
Sub Main() For Each s As String In My.Computer.FileSystem.GetFiles("D:\temp\UnicodeFileName\", FileIO.SearchOption.SearchTopLevelOnly, "1*.*") Console.WriteLine(s) Console.WriteLine(BitConverter.ToString(Encoding.Unicode.GetBytes(Path.GetFileName(s)))) Console.WriteLine(My.Computer.FileSystem.ReadAllText(s, Encoding.Default)) Next Console.ReadLine() End Sub
End Module
#15
试试:文件名变量不要使用 String 类型。
在中文系统下,如果是字符串类型变量,不可识别的字符会统统被“自动”替换成?。
#16
我把1.txt的文件名加了Alt + 0160。
全部要用Unicode版本的函数进行处理。
Option Explicit
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileW" (ByVal lpFileName As Long, lpFindFileData As WIN32_FIND_DATA) As Long Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileW" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Any) As Long Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileW" (ByVal lpFileName As Long, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
Private Type FILETIME dwLowDateTime As Long dwHighDateTime As Long End Type
Private Type WIN32_FIND_DATA dwFileAttributes As Long ftCreationTime As FILETIME ftLastAccessTime As FILETIME ftLastWriteTime As FILETIME nFileSizeHigh As Long nFileSizeLow As Long dwReserved0 As Long dwReserved1 As Long cFileName(MAX_PATH * 2 - 1) As Byte 'Unicode 需要双倍长度 cAlternate(14 * 2 + 1) As Byte 'Unicode 需要双倍长度 End Type
Const eeEndOfFile = 62 'Input past end of file
Function StripNulls(bytes() As Byte) As String Dim i As Long For i = 0 To UBound(bytes) Step 2 If bytes(i) = 0 And bytes(i + 1) = 0 Then Exit For End If Next StripNulls = LeftB(bytes, i) End Function
Function FindFiles(path As String, searchPattern As String) As Collection Dim oFiles As Collection Dim hSearch As Long Dim bFound As Boolean Dim sFileName As String Dim WFD As WIN32_FIND_DATA
Set oFiles = New Collection If Right(path, 1) <> "\" Then path = path & "\"
sFileName = path & searchPattern hSearch = FindFirstFile(StrPtr(sFileName), WFD) If hSearch <> INVALID_HANDLE_VALUE Then bFound = True While bFound sFileName = StripNulls(WFD.cFileName) If (sFileName <> ".") And (sFileName <> "..") Then oFiles.Add path & sFileName End If bFound = FindNextFile(hSearch, WFD) Wend Call FindClose(hSearch) End If
Set FindFiles = oFiles End Function
Function ReadAllText(ByVal path As String) As String Dim hFile As Long Dim nSize As Long Dim bytes() As Byte Dim lBytesRead As Long
ReadAllText = StrConv(bytes, vbUnicode) End Function
Sub Main() Dim oFiles As Collection Dim i As Long Dim s As String
Set oFiles = FindFiles(App.path, "1*.*") For i = 1 To oFiles.Count s = oFiles(i) Debug.Print s, "U+" & Hex(AscW(Mid$(s, InStrRev(s, "\") + 2, 1))) Debug.Print ReadAllText(s) Next End Sub
D:\temp\UnicodeFileName\1?.TXT U+A0 abc中文
又:不如改用VB.Net吧,一点问题都没有。
Imports System.Text Imports System.IO
Module Module1
Sub Main() For Each s As String In My.Computer.FileSystem.GetFiles("D:\temp\UnicodeFileName\", FileIO.SearchOption.SearchTopLevelOnly, "1*.*") Console.WriteLine(s) Console.WriteLine(BitConverter.ToString(Encoding.Unicode.GetBytes(Path.GetFileName(s)))) Console.WriteLine(My.Computer.FileSystem.ReadAllText(s, Encoding.Default)) Next Console.ReadLine() End Sub