Windows CMD - Setar String with Accentuation

Asked

Viewed 6,054 times

5

I am trying to create a Batch (CMD) in Windows that "sweeps" folders in a particular directory and then renames the sub-folders according to a criterion I set.

I am using the code below to carry out the operation (detail: this code I found on the internet and made some small adjustments).

    FOR /D %%D IN (C:\Users\meuUsuario\Desktop\Music\*) DO CALL :RENAME "%%D"
pause

:RENAME
SET CRITERIA=\"(Músicas)"
FOR /D %%R IN (%1%CRITERIA%) DO RENAME %%R "(Singles)"

If I change my criteria to a folder that the nomenclature does not accentuate, it works normally.

I’ve been able to fix this for a while, but it’s hard...

  • 2

    Edit the script on CMD itself. You are probably saving the file in the wrong encoding. Give a type meuarquivo.bat and see if it is appearing right to check if the accent is correct in the CMD.

  • @Bacco I understood yes and it makes sense. I did the "type" and the accent appeared "bugada" really... How do I edit the script or create one by CMD instead of Notepad? Edit "myBat.bat" didn’t work...

  • Don’t have a code editor there? Several of them allow you to choose the encoding when saving. You could use a copy con arquivo.bat and type in the console itself, but it is infernal to correct typos :)

  • @Bacco I created the script by Notepad++ (selecting the Batch language). I also tried to create it by Notepad and saved it in 3 different enconding keys. All the accents were buggered.

  • 1

    try to use encoding Character Sets > Western European > OEM 850 NP++ - To find out which is the correct encoding, enter the CMD and type chcp [enter]

  • 1

    @Bacco Solved! That’s right! 850. I saved by NP++ and it worked perfectly. Thank you!

Show 1 more comment

2 answers

9


The likely problem is that when saving the file, the correct encoding was not chosen.

The first step is to know the encoding correct, typing the command chcp on the console:

chcp

In most systems, the return will be 850. The solution is to use a code editor that allows saving in the correct Codepage.

As you mentioned that you have Notepad++, this option is in the menu

Encoding > Character sets > Western European > OEM 850

(if not 850, you need to find the most suitable option in the menu)

A good striptease to see if the file was in order after editing, is in the CMD itself run:

type arquivo.bat

and check that accented characters appear correctly.


I basically formalized in response the steps that were indicated in separate comments, to eventually help someone with a similar problem

  • Perfect explanation! Congratulations!

  • I really didn’t know about the encoding, it helped me a lot ;)

  • 1

    @Guilhermenascimento just for the record, chcp 65001 changes the console to UTF-8, but then can mess up the output of other applications,

  • @Bacco is a simple thing, are only a few menus by "Choose", I have an environment and English and another in Portuguese, I will test both and see the variations. Thank you

1

As @Bacco said, it could be a problem with encoding. A while ago I had this problem, which still generated new .bat from another .bat, to settle I switched to Windows-1252, adding the command below in the first line:

chcp 1252

Or to convert:

CMD / U / C type ascii.txt > unicode.txt
  • 1

    I tried the chcp and he claimed to have changed the enconding but continued with the accentuation problem. Still displayed the message more or less like this: "P#gina of c%digo activated 1252", rsrs... The "CMD / U / C ..." command I executed and it created another file. However, it could not be executed, as it generated an error in the "F" of the "FOR" (script instruction).

  • @Antôniofilho Try without space and with quotes on the command (/c) invoked by cmd: CMD/U/S/C "type ascii.txt>unicode.txt"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.