How to handle Unicode character file names in VBA using Dir?

I select a list of files based on criteria using Dir and then save them in an array of strings. Then I iterate over the array and process the files.

How can I handle file names using Unicode characters?

I tried to do StrConv , but that didn't help.

 vfile = Dir(vCurrDir & vCrit, vbNormal) 'vCrit is something like *test* 

Then later I will try to access the file properties as follows:

 vfilename = vCurrDir & "\" & vfile Set objFSO = CreateObject("Scripting.FileSystemObject") vDateMod = objFSO.GetFile(vfilename).datelastmodified 

in the last line, I get an error that the file was not found. This only happens for Unicode character file names.

Here was another post: Working with Unicode file names in VBA (using Dir, FileSystemObject, etc.) , but that does t solve the problem with Dir '.

My example: VBA (see) c: \ my-test-file.pdf Unicode: c: \ my-test-file.pdf

(you cannot see the difference here, but if you copy the names and paste them into Notepad ++, you will see that dashes are different characters for two files, VBA converts a hyphen character to Unicode in a Windows locale dash)

Or an example from a related question: 3_PoΕ‚ish.txt (in Unicode) versus 3_Polish.txt (in VBA)

+6
source share
3 answers

Actually the problem is using Dir to create a list of files, instead I suggest a wider use of FileSystemObject , i.e. properties of its Folder and File objects.

The following procedure prints the Name , DateLastModified and Path properties for all txt files in the D:\@D_Trash\

 Sub Files_with_Unicode_Chars() Dim oFso As Object 'Scripting.FileSystemObject Dim oFdr As Object 'Scripting.Folder Dim oFle As Object 'Scripting.File Dim vCurrDir As String, vCrit As String Set oFso = CreateObject("Scripting.FileSystemObject") vCurrDir = "D:\@D_Trash\" 'Update as needed! vCrit = ".txt" 'Update as needed! Set oFdr = oFso.GetFolder(vCurrDir) For Each oFle In oFdr.Files With oFle Select Case Mid(.Name, InStrRev(.Name, ".")) Case vCrit Rem Perform actions as needed Rem This sample shows the use some of the File object properties Debug.Print Now; vbTab; "["; .Name; "]"; Tab(71); .DateLastModified; Tab(101); .Path End Select: End With: Next End Sub 
0
source

I had a similar problem. Using FileSystemObject to traverse files in subdirectories was too slow. For my needs, I wrote code to create and run a batch file, which, among other things, used the Dir command and passed the output to a file. Then, when the batch file is complete, my code parsed the output file. Strange, but the unicode characters were just good, and then all of a sudden - no. (I never tried to find out why. I assume that it was either because we moved the files to another place, I started using the client instead of the server for processing, or updating Win10, but none of this was under my control.)

I solved this by changing the creation of the batch file from the dir command to instead run the DOS command call cmd /u /c dir "dirpath" > c:\Temp\DirOutput.txt , and then read the file.

  • cmd /u is the key part. This forces the output of the file to a file in Unicode
  • call , because it was inside the batch file, and call pauses the batch file execution until the command completes. Not needed for a simple dir , but I found it needed for cmd . (Maybe there was a better way, but this is the one I came across first.)
  • The /c flag in cmd tells cmd treat the rest of the line as a command to execute and then exit.
  • dir can also have flags, of course. I used the /s flag because I needed all the files inside and below the dirpath
  • > tell dir to output the results to a text file.
0
source

Try using BuildPath https://msdn.microsoft.com/en-us/library/office/gg251615.aspx instead

 vfilename = vCurrDir & "\" & vfile 
-1
source

Source: https://habr.com/ru/post/1244107/


All Articles