En Dash in file path batch job query

梦想与她 提交于 2019-12-13 04:22:48

问题


Hoping someone can help. I have a batch job (Windows env) which simply copies a file to another folder.

copy "\\ACP-MS-NAS21\Global\MEC Daily Productivity\Business Analysts\Master_List\HCP_Master_List.xlsx" ^
     "\\ACP-MS-NAS21\Global\CSD [?] DWP Medical Services\CSL_CSD_DB\Master_List"

But I get the following error:

The filename, directory name, or volume label syntax is incorrect.

I can see there's an en dash in the file path which I believe is causing the problem. Is there any way to have a wildcard in the file path or any other way the job can recognise this?

Thanks in advance. PS. newbie at batch programming, coding etc, so please can explanations be in plain English. Many thanks


回答1:


The command-line windows (and by extension - BAT files) operate in OEM codepage by default. Which exact codepage is defined by your OS settings (Language for non-Unicode programs or similar). Therefore you cannot normally use anything outside ASCII or whatever codepage you have.

Save script as UTF-8

To use those characters you will need to work in a codepage that actually has them. UTF-8 is the best one for this tasks (and probably the only one that will work in your case).

First, save your script as UTF-8. In notepad you can select codepage from save as menu.

If your editor does not allow you to select UTF-8 (no BOM) leave first line of your BAT file blank as some editors may preface your file a special header called BOM that helps detecting codepage. If it does and you leave 1st line blank you will get a Bad command or file name error as soon as your script starts but this won't prevent it from running properly.

Select UTF-8 codepage

Now, your script is in UTF-8 but windows command processor will still execute it as if it was in ASCII, thus corrupting all special characters. To specify our encoding we need to add following command, preferentially as a first non-blank line of your script, including comments (those may have non-ASCII characters in them - with unpredictable results)

CHCP 65001

CHCP changes current codepage to 65001 which is internal codepage number for UTF-8.

This works because latin letters and numbers in UTF-8 have the same encoding as in ASCII and OEM codepages. Thus your scripts starts executing in OEM codepage, but since CHCP 65001 command itself does not have non-ASCII characters it understood correctly. All following commands will be executed in UTF-8 and may have non-ASCII characters.

You may now insert em-dash into the filename and it won't be replaced with ? upon saving.

Set UTF-8 font

Unfortunately default console window font does not display UTF-8 correctly so you won't be able to see non-ASCII characters correctly. To solve this you should right-click command-line window title bar, select properties, and change font to UNICODE one. Consolas, Lucida Console and Courier New should work.




回答2:


Thanks for all the responses. I've had to make a design change to my DB so have managed to get around the need to do this now (phew :))

Thanks again



来源:https://stackoverflow.com/questions/53392237/en-dash-in-file-path-batch-job-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!