Saving a filename with a unicode character

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Saving a filename with a unicode character

#16 Post by Liviu » 07 Apr 2012 20:09

MKANET wrote:Is there any way to just change to the codepage when the special character is needed; then switch back to the default codepage for the rest of the characters?

Not that I know of. It is possible to some extent to do that using a temporary UTF-encoded external file, but once that file is built, there is no way I am aware of to read it back into usable form (environment or "for /f" loop variable).

That's essentially the still open question at http://www.dostips.com/forum/viewtopic.php?p=13318#p13318.

MKANET wrote:Edit: Maybe I'm not understanding, don't those special characters belong to a codepage that I can change to?

No, there are lots of unicode characters not mapped into any particular codepage.

MKANET wrote:I found a batch file by doing a search on google which switches between codepages. I just dont know what code pages those special characters belong to.

Characters do not belong to codepages. And 65001 is UTF-8, which is a semi-supported codepage, useful for little else then generating UTF-8 encoded text files, or running some apps aware of it. That's not really relevant to the topic here.

Liviu

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Saving a filename with a unicode character

#17 Post by MKANET » 07 Apr 2012 20:37

Thanks german, but unfortunately those characters aren't a good enough replacement. If someone can tell me how to echo a (Ctrl-Z) key combination in a batch file, I can simply use copy con to create the file with ◄ ► (U+25BA/25C4) characters (mentioned by Liviu) which are partially supported (no echo redirection).

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Saving a filename with a unicode character

#18 Post by MKANET » 07 Apr 2012 20:48

My idea was to use the below. However, Im not sure if copy con can be used within a batch file and how to echo an F6(control-z) after that.

copy con "◄◄◄◄◄◄.txt" | echo ^Z


Edit: Actually, Id need to echo a control-z AND "Enter"

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Saving a filename with a unicode character

#19 Post by Ed Dyreen » 07 Apr 2012 21:13

'
I try every trick I master, but I fail miserably, I give up :(

Code: Select all

@echo off &setlocal enableDelayedExpansion

set lf=^


::
echo.!lf! |copy con ".CMD"
echo.$err=!errorlevel!_

> "test.CMD" (

   echo._
)

type "test.CMD"

pause
exit /b 0

Code: Select all

De syntaxis van de bestandsnaam, mapnaam of volumenaam is onjuist.
        0 bestand(en) gekopieerd.
$err=1_
◄◄◄◄◄◄◄◄_
Druk op een toets om door te gaan. . .
Liviu and aGerman are unfortunately likely to be right.

Maybe it's time for someone to suggest an external app, but even then,
how would you read the file back in, the problem remains...

ps: not sure but ^Z seems to be a linefeed.

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Saving a filename with a unicode character

#20 Post by MKANET » 07 Apr 2012 21:22

Yeah, if it can't be done with clever use of copy con, then I have reached a dead end unfortunately. It's just frusterating that I'm soooooo close (it works for normal echo, but not anything that can actually save a file name).

If there's nothing more that can be done, I'll just accept the limitation. Thanks for everyone chipping in and trying.

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Saving a filename with a unicode character

#21 Post by Liviu » 07 Apr 2012 21:26

MKANET wrote:If someone can tell me how to echo a (Ctrl-Z) key combination in a batch file, I can simply use copy con to create the file with ◄ ► (U+25BA/25C4) characters (mentioned by Liviu) which are partially supported (no echo redirection).

You don't need to "copy con" then ^Z, you could simply "copy nul" instead. But it won't work if the destination filename contains a control character like 0x10/0x11.

Sorry, you must have misread my previous p.s. note. I was not suggesting you tried to create a filename with literal 0x10/0a11 characters. I don't think it's even possible from the cmd line or a batch file. Control characters are illegal in pathnames (http://ss64.com/nt/syntax-filenames.html, http://msdn.microsoft.com/en-us/library/Aa365247). If you found a codepage where U+25BA/25C4 were mapped to the legal range of 0x20-0xFF then you could use that but, again, I don't know of one offhand.

Still not clear on what the ultimate goal of all this may be, and why aGerman's alternative is not "a good enough replacement". If it's just for visual effect, then I'd say you are wasting too much effort on eye candy ;-)

Liviu

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Saving a filename with a unicode character

#22 Post by Ed Dyreen » 07 Apr 2012 21:46

'
The reason I hook in on this conversation is because I'm also interested if you can enumerate filenames that have unicode characters, ( it kinda happens a lot especially with torrent/MP3/etc..)

a file exists named "Special◄.CMD"

Code: Select all

@echo off &setlocal enableDelayedExpansion

for /f "usebackq delims=" %%? in ( `dir /b Special*.CMD` ) do echo.set "$=%%?" &set "$=%%?"

set "$"

if exist "!$!" (

   echo.locate succes "!$!"
) else    echo.locate errors "!$!"

copy "!$!" "copy succes"

pause
exit /b 0

Code: Select all

set "$=Special◄.CMD"
$=Special◄.CMD
locate errors "Special◄.CMD"
De syntaxis van de bestandsnaam, mapnaam of volumenaam is onjuist.
Druk op een toets om door te gaan. . .
For the moment, I'll just use http://www.bulkrenameutility.co.uk :|

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Saving a filename with a unicode character

#23 Post by Liviu » 07 Apr 2012 22:03

Ed Dyreen wrote:'The reason I hook in on this conversation is because I'm interested if you can enumerate filenames that have unicode character

You certainly can, but must avoid "dir /b" which kills characters outside the current codepage outright, and use a plain file loop instead.

A bit outdated, yet related: http://groups.google.com/group/alt.msdos.batch.nt/browse_thread/thread/e16b6069fe5a6414/374bbc2cd74095e?hl=en.

Liviu

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Saving a filename with a unicode character

#24 Post by Ed Dyreen » 07 Apr 2012 22:17

'
Thanks Liviu, that's very interesting, I will look into it if I find some time...
This makes me wonder, why haven't microsoft updated the console to support unicode yet ?Image
And will it ever use color-coding like UNIX is doing since 15 years ?

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Saving a filename with a unicode character

#25 Post by Liviu » 08 Apr 2012 00:15

Ed Dyreen wrote:This makes me wonder, why haven't microsoft updated the console to support unicode yet ?
And will it ever use color-coding like UNIX is doing since 15 years ?

I agree with the sentiment that the cmd prompt is a hack'ish toy of a shell ;-)

That said, the Win32 console itself (which hosts cmd.exe) is quite capable, fully unicode enabled, and colorful as well. It's just that cmd itself doesn't take advantage of many of those capabilities. See for (counter)example "ztreewin" or "lugaru epsilon" - both of which I've used for decades, still am.

Liviu

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Saving a filename with a unicode character

#26 Post by aGerman » 08 Apr 2012 06:55

MKANET wrote:Thanks german, but unfortunately those characters aren't a good enough replacement. If someone can tell me how to echo a (Ctrl-Z) key combination in a batch file, I can simply use copy con to create the file with ◄ ► (U+25BA/25C4) characters (mentioned by Liviu) which are partially supported (no echo redirection).

Well, the only solution I found out is to work with predefined environment variables. See
http://technet.microsoft.com/en-us/libr ... 10%29.aspx
Add a variable fullrectleft with value ◄, variable fullrectright with value ► etc.
Works for me on Win7:

Code: Select all

set "y=%fullrectleft% NOT HERE %fullrectright%.txt"
>"%y%" type nul

Regards
aGerman

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Saving a filename with a unicode character

#27 Post by MKANET » 08 Apr 2012 12:41

aGerman,

Thanks for the tip. There are a couple of different way to add the global environment variable via command line I can either add a global environment variable via registry:

Code: Select all

Reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /f /v fullrectleft /t REG_SZ /d "◀ NOT T HERE ▶"


or...

via SETX command:

Code: Select all

setx fullrectleft "◀ NOT T HERE ▶" -m


The problem is only a completely brand new command session will be able to see this new environment variable. Unfortunately, I can't figure out how to do that using a batch file. Even the below command didn't work:

Code: Select all

CMD /U /C setx fullrectleft "◀ NOT T HERE ▶" -m


Is there any way to call a completely new command window unrelated to the command session initiating it?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Saving a filename with a unicode character

#28 Post by aGerman » 08 Apr 2012 13:19

If you open the batch file via double click it inherits the global environment from the explorer.exe process. Even if the explorer environment is updated during the runtime of the batch file it will never influence the environment of the running cmd process.
Also, if you run another batch file B from batch file A then B inherits the environment from A. Any changes in B do not influence A. (This statement is not valid for CALLed batch files since they're running in the same instance of cmd.exe. But it would be valid for CMD or START.)

In summary you have to change the parent environment BEFORE it is inherited by the cmd process.

One more possibility: Use another scripting language with a (slightly) better unicode support, change its environment and call the batch file from it.

callbat.vbs

Code: Select all

Dim objWShell, objEnv
Set objWShell = CreateObject("WScript.Shell")

Set objEnv = objWShell.Environment("PROCESS")
objEnv("fulltriangleleft") = ChrW(&H25C0)
objEnv("fulltriangleright") = ChrW(&H25B6)
objEnv("emptytriangleleft") = ChrW(&H140A)
objEnv("emptytriangleright") = ChrW(&H1405)
Set objEnv = Nothing

objWShell.Run "test.bat"

Set objWShell = Nothing


test.bat

Code: Select all

@echo off &setlocal
set "x=%emptytriangleleft% Here %emptytriangleright%.txt"
set "y=%fulltriangleleft% NOT HERE %fulltriangleright%.txt"
>"%x%" type nul
>"%y%" type nul

Sorry for not having better ideas :wink:

Regards
aGerman

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Saving a filename with a unicode character

#29 Post by MKANET » 08 Apr 2012 14:08

Thanks I dont mind using vbscript to help out. However, I need to be able to call the vbscript from the batch file, not the other way around. I'm pretty sure that wouldn't work.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Saving a filename with a unicode character

#30 Post by Ed Dyreen » 08 Apr 2012 14:09

'
aGerman, why does it work with you and not for me :cry:

Code: Select all

@echo off &setlocal enableDelayedExpansion

chcp
for /f "usebackq delims=" %%? in ( `dir /b Special*.CMD` ) do echo.set "$=%%?" &set "$=%%?"
set "$"

if exist "!$!" ( echo.locate succes "!$!" ) else echo.locate errors "!$!"

chcp 850
> "!$!" type nul
chcp 1252
> "!$!" type nul
chcp 437
> "!$!" type nul

pause
exit /b 0
you'll need a file "Special◄.CMD" to test.

Code: Select all

Actieve codetabel: 850
set "$=Special◄.CMD"
$=Special◄.CMD
locate errors "Special◄.CMD"
Actieve codetabel: 850
De syntaxis van de bestandsnaam, mapnaam of volumenaam is onjuist.
Actieve codetabel: 1252
De syntaxis van de bestandsnaam, mapnaam of volumenaam is onjuist.
Actieve codetabel: 437
De syntaxis van de bestandsnaam, mapnaam of volumenaam is onjuist.
Druk op een toets om door te gaan. . .
Die Syntax der Dateinamen, Verzeichnisnamen oder die Datenträgerbezeichnung ist falsch.

Post Reply