0

enter image description here

I am programming in a Chinese character environment. I ran into two problems. First, I can't change directory using bat. In bat file I cd D:, but this line does not actually change the directory. The next line working directory goes back to C:.

And, it seems that the cmd is not recognizing Chinese characters. I tried chcp 65001 but it does not work. How to make the cmd able to recognize Chinese characters?

I did a lot of googling but did not quite get an answer. Thanks in advance.

Magoo
  • 77,302
  • 8
  • 62
  • 84
YMS
  • 1
  • 1
  • 1
  • 3
    `cd /?` - see the `/d` parameter. (btw: a rough translation of the Chinese text would help us much...) – Stephan Jun 12 '17 at 14:36

2 Answers2

1

Use the /D switch to change current drive in addition to changing current directory for a drive:

cd /D D:

Another approach: PUSHD Change the current directory/folder and store the previous folder/path for use by the POPD command:

pushd D:

Some commands in CMD Shell (which runs inside the Windows Console cmd) recognise Chinese characters quite well even in non-Chinese environment, see below example. For instance, my Windows settings are

  • display language is English (United Kingdom), and
  • current system locale (Language for non-Unicode programs) is Czech i.e. Slavic (Central European) code pages CP852/ANSI1250.

Please note that I have no special CJK support set up! Unfortunately, there is still very limited support for unicode in the CMD shell.

C:> reg query HKLM\SYSTEM\CurrentControlSet\Control\Nls\CodePage | find "CP"
    ACP    REG_SZ    1250
    OEMCP    REG_SZ    852
    MACCP    REG_SZ    10029

C:\> chcp
Active code page: 852

C:\> pushd "D:\bat\Unusual Names\CJK (中文(繁體))"

D:\bat\Unusual Names\CJK (中文(繁體))> dir /B / S
D:\bat\Unusual Names\CJK (中文(繁體))\中文(繁體)
D:\bat\Unusual Names\CJK (中文(繁體))\装备女印度舞娘时装上衣
D:\bat\Unusual Names\CJK (中文(繁體))\中文(繁體)\chinese traditional.txt
D:\bat\Unusual Names\CJK (中文(繁體))\中文(繁體)\二月.mp3
D:\bat\Unusual Names\CJK (中文(繁體))\中文(繁體)\女
D:\bat\Unusual Names\CJK (中文(繁體))\中文(繁體)\装备女印度舞娘时装上衣.rtf
D:\bat\Unusual Names\CJK (中文(繁體))\中文(繁體)\女\印度舞娘时装上衣
D:\bat\Unusual Names\CJK (中文(繁體))\装备女印度舞娘时装上衣\inside_CJK.txt

D:\bat\Unusual Names\CJK (中文(繁體))> popd

C:\> dir /B /S "D:\bat\Unusual Names\CJK (中文(繁體))"
D:\bat\Unusual Names\CJK (中文(繁體))\中文(繁體)
D:\bat\Unusual Names\CJK (中文(繁體))\装备女印度舞娘时装上衣
D:\bat\Unusual Names\CJK (中文(繁體))\中文(繁體)\chinese traditional.txt
D:\bat\Unusual Names\CJK (中文(繁體))\中文(繁體)\二月.mp3
D:\bat\Unusual Names\CJK (中文(繁體))\中文(繁體)\女
D:\bat\Unusual Names\CJK (中文(繁體))\中文(繁體)\装备女印度舞娘时装上衣.rtf
D:\bat\Unusual Names\CJK (中文(繁體))\中文(繁體)\女\印度舞娘时装上衣
D:\bat\Unusual Names\CJK (中文(繁體))\装备女印度舞娘时装上衣\inside_CJK.txt

Edit. This can be done even using a .bat file providing that the .bat file is saved and run in the same ANSI code page (see National Language Support (NLS) API Reference to choose code page for Simplified or Traditional Chinese, i.e. 936 or 950, respectively), or that the .bat file is saved in UTF-8 (without BOM) and run under its cmd equivalent chcp 65001:

C:\Utils> chcp 65001
Active code page: 65001

C:\Utils> type D:\bat\SO\q44501393.bat
@ECHO ON
pushd "D:\bat\Unusual Names\CJK (中文(繁體))"
dir /B *.txt
@rem next command opens "中文(台灣).txt" in default app for `.txt` files
start "" "中文(台灣).txt"    

C:\Utils> D:\bat\SO\q44501393.bat

C:\Utils> pushd "D:\bat\Unusual Names\CJK (中文(繁體))"

D:\bat\Unusual Names\CJK (中文(繁體))> dir /B *.txt
中文(台灣).txt

D:\bat\Unusual Names\CJK (中文(繁體))> start "" "中文(台灣).txt"

D:\bat\Unusual Names\CJK (中文(繁體))> popd

C:\Utils>
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • Thanks, I will try chcp 852, but I see on the Microsoft TechNet chcp 852 is Slavic (Latin 2). Should I use chcp 936 instead if I want my directory with Chinese characters to be recognized? – YMS Jun 13 '17 at 02:10
-1

To change the directory in batch (cmd) you have to do:

cd D:\

In your picture you did "D:" without the "\"; this is wrong.

I hope I was able to help you.

PS: There isn't anyway to change the display language from Chinese to English because cmd.exe is an Windows System Application which is based on the system language. If you want to have cmd in English, you must change the system language in English or reinstall Windows with a English distro.

Antonio Gschossmann
  • 532
  • 1
  • 7
  • 18
  • `cd "D:\"` differs from `cd "D:"`. The OP needs to use `cd /D "D:"` or `pushd "D:"`. – JosefZ Jun 12 '17 at 20:03
  • @JosefZ Who's the OP? But there's always the possibility to do cd D:\ to change dir on drive D, or not? – Antonio Gschossmann Jun 12 '17 at 20:10
  • Nope. Read `cd /?`: **1.** Type `CD drive:` to display the current directory in the specified drive. **2.** Type `CD` without parameters to display the current drive and directory. **3.** Use the `/D` switch to change current drive in addition to changing current directory for a drive. BTW, _OP_ = _Original Poster_. – JosefZ Jun 12 '17 at 20:27
  • @AntonioGschossmann thanks, I used the pushd and it worked. Maybe I didn't phrase my question very well. I wrote a directory in my bat file(the Chinese characters displayed well in bat). I execute my bat file and the cmd couldn't recognize these characters. I did a lot of research and many said chcp 65001. I tried but every time the codepage 65001 is active, it seems that my bat file can't be executed, even no error message that I used to get in codepage 936. – YMS Jun 13 '17 at 05:22