3

I have this code:

Function cpyBIN(cpFilename As String)
    Dim litefile() As Byte
    Dim FN As Integer
    Dim xlof As Long

    FN = 1
    Open cpFilename For Binary As FN
    xlof = LOF(FN)
    ReDim litefile(xlof)

    Get FN, , litefile
    Open cpFilename & "Backup" For 
    Binary As #2

    Put #2, , litefile
    Close #2
    Close FN
End Function

and I use it in a Form like this :

Private Sub cmdBackup_Click()
Dim strComputer, objWMIService, colFiles, objfile

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    

Set colFiles = objWMIService.ExecQuery("Select * from CIM_Datafile where Drive='D:' and path='\\contoh\\'")

For Each objfile In colFiles
cpyBIN (objfile.Name)
Next


End Sub

in the contoh folder there are 2 sample files:

  • namafile.exe
  • namafile♣♣♠.exe

when I run the code there is an error message like in the picture:

enter image description here

the error is in the line as shown in the picture:

enter image description here

how to make it supports unicode filename? or is there any replacement for this function??

  • What error message do you get and on what line? – Slugsie Sep 14 '21 at 07:53
  • Exactly how does it fail? You said it does not work but that isn't very specific... – StayOnTarget Sep 14 '21 at 12:43
  • 1
    Does this answer your question? [Working with Unicode file names in VBA (using Dir, FileSystemObject, etc.)](https://stackoverflow.com/questions/33685990/working-with-unicode-file-names-in-vba-using-dir-filesystemobject-etc) – GSerg Sep 14 '21 at 13:31
  • 1
    Does this answer your question? [How to type currency symbols in Visual Basic Editor](https://stackoverflow.com/q/24384952/11683) – GSerg Sep 14 '21 at 13:33
  • Thanks for the comments and fixing my question. sorry if my question is difficult to understand because my english is not good. I have updated my question. I hope friends can help – enddie onskynet Sep 14 '21 at 23:04
  • @enddieonskynet Your question was fine. Arguably, you've made it more difficult to understand with your last edit. There will always be people who are not competent enough to understand your question, just learn to ignore them. Please see the dupe links that together answer your question. `Open` does not support Unicode file names, and you cannot reliably have Unicode string literals in the code editor. – GSerg Sep 14 '21 at 23:27
  • so what command should I use? if it is open it doesn't support unicode. the point is to be able to read files with unicode names. – enddie onskynet Sep 14 '21 at 23:49
  • You could declare the "Kernel" copy methods and use windows instead ... should make all encoding work -> see my answer for this (coming in 5 minutes) – nabuchodonossor Sep 15 '21 at 07:33
  • @enddieonskynet Have you tried to actually look at the links? – GSerg Sep 15 '21 at 08:06
  • I had remove my answer - must check it first on my vb6 system (currently in the office ... can do this later in the evening) – nabuchodonossor Sep 15 '21 at 08:19
  • @enddieonskynet Do you need to read the file, or simply make a copy of the file? – Brian M Stafford Sep 15 '21 at 11:36
  • @GSerg i want to read binary – enddie onskynet Sep 15 '21 at 14:26
  • @nabuchodonossor thanks for your efforts. I'm waiting for the answer – enddie onskynet Sep 15 '21 at 14:27
  • @Brian M Stafford i want to read binary from file – enddie onskynet Sep 15 '21 at 14:28

1 Answers1

4

There are several ways to make a copy of a file with Unicode file names. The first way is by using the Windows API:

Declare Function CopyFileW Lib "kernel32.dll" (ByVal lpExistingFileName As Long, _
   ByVal lpNewFileName As Long, Optional ByVal bFailIfExists As Long) As Long

For Each objfile In colFiles
   CopyFileW StrPtr(objfile.Name), StrPtr(objfile.Name & ".Backup")
Next

The second way is by using a FileSystemObject:

Dim fso As FileSystemObject
Set fso = New FileSystemObject

For Each objfile In colFiles
   fso.CopyFile objfile.Name, objfile.Name & ".Backup", True
Next

The third way is by using an ADO Stream:

Dim s As ADODB.Stream
Set s = New ADODB.Stream
   
s.Open
s.Type = adTypeBinary

For Each objFile In colFiles
   s.LoadFromFile objFile.Name
   s.SaveToFile objFile.Name & ".Backup", adSaveCreateOverWrite
Next

s.Close

If you want to read data, I would use an ADO Stream:

Dim s As ADODB.Stream
Set s = New ADODB.Stream

s.Open
s.Type = adTypeBinary

For Each objFile In colFiles
   s.LoadFromFile objFile.Name
   data = s.Read()
   'use the data somehow
Next

s.Close
Brian M Stafford
  • 8,483
  • 2
  • 16
  • 25