Error 52: Bad File Name (Chinese characters)


C

Craig

I'm using access 2003 vba...

I hope this comes through clearly: I have the following file name that
includes chinese characters (there are thousands of these files that need
renaming, the example below has the first 2 characters as chinese symbols,
but they could be embedded anywhere in the file name):

Example:
ç­”å¤_China brochure.msg

I'm doing a rename process using VBA (ie, name oldname as newname).

I'm getting the error: Error 52: Bad file name or number...

Obviously, VBA can't find the original file name as it doesn't recognize it
(the chinese characters come across as "??")

Any ideas? Thanks.
 
Ad

Advertisements

C

Craig

???

I'm confused...you mean the section "How to write out Unicode text files in
VBA"???
 
T

Tokyo Alex

Hi Craig,

What OS are you using? Can you open the file(s) by double clicking?

How are you getting the current filename into oldfilename for your 'name
oldfilename as newfilename' command?

Cheers,
Alex.
 
C

Craig

Hello...thanks for the response.

i'm using XP....

Here's the thing...you can't see the chinese characters in Windows explorer
because it can't display them. The characters are there, they just show up
as litte squares (think carriage returns, etc.).

What I want to do is replace these characters with some other character
(like an underscore or something). So in my vba script I read open the file
as such:

dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
with application.filesearch
...blah blah
oldname= .foundfiles(x)

Then, I cursor through each character in this name to find non ascii
characters and replace them with an underscore.

Then, this:

Name oldname as newname

This is where I get the error. The problem is when I read the filename into
vba it doesn't recognize the chinese characters. So it doesn't recognize
"oldname".

I hope this makes sense.

I'm thinking I need to get load a library of chinese characters or something..

Thanks for any and all help. Signing off for the evening but will check in
the AM
 
S

Stuart McCall

Response inline:

Craig said:
Hello...thanks for the response.

i'm using XP....

Here's the thing...you can't see the chinese characters in Windows
explorer
because it can't display them. The characters are there, they just show
up
as litte squares (think carriage returns, etc.).

What I want to do is replace these characters with some other character
(like an underscore or something). So in my vba script I read open the
file
as such:

dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
with application.filesearch
..blah blah
oldname= .foundfiles(x)

Then, I cursor through each character in this name to find non ascii
characters and replace them with an underscore.

One thing you could try. Read the whole file into a string variable, delete
the file, then write the string out as a newly named file:

Dim f As Integer, buf As String

f = FreeFile
Open FileName For Binary Access Read As f
buf = Space(LOF(f))
Get #f, , buf
Close f

Kill Filename

'Modify the filename here

Open NewName For Binary Access Write As f
Put #f, , buf
Close f

Of course that will only work so long as the VBA Open statement can deal
with the spurious chars. I don't know if the fso can also do this, because I
don't use it much. (I don't have time to test anything today).

HTH
 
Ad

Advertisements

T

Tom van Stiphout

On Thu, 25 Mar 2010 21:51:01 -0700, Craig

I was hoping you could write:
Name StrConv(strFile, vbUnicode) As "renamed.txt"
but indeed that does not work. I will have to look into this some
more.

-Tom.
Microsoft Access MVP
 
C

Craig

Thanks for the response.

No, VBA can't read the spurious characters. It shows them as "?" But, I
can replace any "?" with an underscore. That's what I do to create "newname".

And I guess that's the overall problem: VBA reads the characters as "?".
Then, when it encounters this statement:

Name oldname as newname

....it tries to find and replace the file in explorer (with the chinese
characters) with "oldname", but obviously can't as "oldname" has "?" instead
of chinese characters.

Geez, now my head's spinning ;o)

I'll give this a shot and report back.

Thanks.
 
C

Craig

Actually, I think I answered my own question. This won't work as VB can't
read the spurious characters...

I think I'm going to need to load a library of chinese characters so my
system can see them...if that's at all possible.

Unless anyone can think of something else?

Thanks.
 
Ad

Advertisements

C

Craig

Yes...on my previous thread I posted this:

dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
with application.filesearch
...blah blah
oldname= .foundfiles(x)

Assuming the file has 2 chinese characters in the beginning of the name, VB
reads the file as "??filename.msg".

Unless I misinterpreted the question?
 
S

Stuart McCall

Craig said:
Yes...on my previous thread I posted this:

dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
with application.filesearch
..blah blah
oldname= .foundfiles(x)

Assuming the file has 2 chinese characters in the beginning of the name,
VB
reads the file as "??filename.msg".

Unless I misinterpreted the question?
<snip>

You could give this a shot: grab the file's short path and feed that to Name
or Open & see if vba can then deal with it:

http://www.smccall.demon.co.uk/Strings.htm#ShortPath
 
T

Tokyo Alex

Hi Craig,

I had the chance to do some testing, and I couldn't find much to help.

I deal with (Japanese) DBCS file and object names all the time, and although
Access is usually pretty good, this really threw it a curve.

I can display (in Windows Explorer) your file name, and can open and act on
the file. Access can store and display it properly in a table, and VBA can
read it, and write it to that table correctly.

But, debug.print filename can't display the non-Japanese DBCS characters,
they come out as "?", as in your case. And, of course, name OFN as NFN fails
with error 52.

It seems that it fails when there's no native way of displaying the filename
in Windows, in which case Windows reverts to Unicode which it can display but
not process.

I'm thinking you have 2 possible solutions:
If you're using XP Pro, I believe you can download language packs from MS
(I'm not sure if they're free). So you could get the Chinese language pack
and IME which should allow native display and processing of Chinese filenames.

Or, there may be some way other than the filename to refer to the file and
force VBA to open it. This may be beyond VBA, and is certainly beyond my
knowledge of VBA.

Not very helpful, I know, but that's all I can think of right now. I'll
post back if anything else occurs to me.

Cheers,
Alex.
 
T

Tom van Stiphout

On Fri, 26 Mar 2010 07:26:47 -0700, Tom van Stiphout

OK, I think I figured it out.
You can't use the Dir function to iterate over the filenames.
Apparently Dir returns an ascii string, not a unicode string.
The FileSystemObject works much better:
Dim fs As Scripting.FileSystemObject
Dim fil As Scripting.File
Dim fol As Scripting.Folder

Set fs = CreateObject("Scripting.FileSystemObject")
Set fol = fs.GetFolder("c:\test\")
For Each fil In fol.Files
If AscW(fil.name) > 255 Then Stop
Next fil
Set fol = Nothing
Set fs = Nothing

I created a file in the test folder and gave it a leading Chinese
character (u+5609 in my case). I ran the above code and it stops at
that file. When I hover over fil.name I see a questionmark for that
character. I conclude that the debug tooltip cannot display unicode
chars. When I debug.print fil.name I also get a questionmark. I
conclude the debug window (at least on a US version of Windows and
Office) cannot display unicode chars.
But AscW can handle unicode chars, and the above code proves that
fil.name is a unicode string.
So now you can write:
fil.name = "renamed.txt" and you will have renamed the unicode file.

-Tom.
Microsoft Access MVP
 
C

Craig

Thank you very much.

Yes, I get the same errors ("?" showing up in place of the character).

At this point I might tap into my forensic/ediscovery comrades to see if
there are alternative software solutions (ie LAW, Encase, etc.). I will
report back here with findings.

VB has yet to fail me, though. I would be interested to discover a VB
workaround if possible.

Thanks.
 
Ad

Advertisements

T

Tom van Stiphout

On Fri, 26 Mar 2010 20:40:01 -0700, Craig

Check my answer in another part of this thread.
-Tom.
 
D

David W. Fenton

Yes...on my previous thread I posted this:

dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
with application.filesearch
..blah blah
oldname= .foundfiles(x)

Assuming the file has 2 chinese characters in the beginning of the
name, VB reads the file as "??filename.msg".

But what command are you using to rename the file? VBA's Name
command or the FSO MoveFile method?

Also, have you tried operating directly on the name returned by the
FoundFiles collection instead of assigning it to a string variable?
 
Ad

Advertisements

D

David W. Fenton

Yes...on my previous thread I posted this:

dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
with application.filesearch
..blah blah
oldname= .foundfiles(x)

Assuming the file has 2 chinese characters in the beginning of the
name, VB reads the file as "??filename.msg".

Unless I misinterpreted the question?

Tom van Stiphout has obviously taken this in the direction I was
recomending. In my previous post today, I mis-characterized what you
need to do (I don't use the FSO myself -- I'm partial to simply
using VBA's built-in functions, so I've never quite learned the FSO
object model) -- there is a Name method that works like the VBA Name
command.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top