How to remove accent using bat/cmd

Asked

Viewed 1,693 times

2

How to create folder using a user name without special accent(s) or character(s)?

Like, for example, changing càmbio for cambio

Is there a command that converts this?

Thanks!

2 answers

3


No need for external dependencies/languages you can use a simple "replace", of course this would have to run in windows-1252 and everything would have to be configuring this way.

Replace in CMD/BAT

set STR=Boa noite!
call set STR=%%STR:noite=tarde%%
echo %STR%

This %%STR:noite=tarde%% exchange the word night for evening of the variable STR

Remove accents (windows-1252)

To change the seats create a .bat and save as ANSI or windows-1252 (compatible) and then for a simple test put this (does not contain all accents, but is just an example of use):

@echo off

chcp 1252>nul

cls

set ORIGINAL=Letra "á", letra "é", letra "í", letra "ó", letra "ú", letra "Á", letra "É", letra "Í", letra "Ó", letra "Ú, letra "ç" e letra "Ç"

set DADOS=%ORIGINAL%

set DADOS=%DADOS:â=a%
set DADOS=%DADOS:Â=A%
set DADOS=%DADOS:à=a%
set DADOS=%DADOS:À=A%
set DADOS=%DADOS:á=a%
set DADOS=%DADOS:Á=A%
set DADOS=%DADOS:ã=a%
set DADOS=%DADOS:Ã=A%
set DADOS=%DADOS:ê=e%
set DADOS=%DADOS:Ê=E%
set DADOS=%DADOS:è=e%
set DADOS=%DADOS:È=E%
set DADOS=%DADOS:é=e%
set DADOS=%DADOS:É=E%
set DADOS=%DADOS:î=i%
set DADOS=%DADOS:Î=I%
set DADOS=%DADOS:ì=i%
set DADOS=%DADOS:Ì=I%
set DADOS=%DADOS:í=i%
set DADOS=%DADOS:Í=I%
set DADOS=%DADOS:õ=o%
set DADOS=%DADOS:Õ=O%
set DADOS=%DADOS:ô=o%
set DADOS=%DADOS:Ô=O%
set DADOS=%DADOS:ò=o%
set DADOS=%DADOS:Ò=O%
set DADOS=%DADOS:ó=o%
set DADOS=%DADOS:Ó=O%
set DADOS=%DADOS:ü=u%
set DADOS=%DADOS:Ü=U%
set DADOS=%DADOS:û=u%
set DADOS=%DADOS:Û=U%
set DADOS=%DADOS:ú=u%
set DADOS=%DADOS:Ú=U%
set DADOS=%DADOS:ù=u%
set DADOS=%DADOS:Ù=U%
set DADOS=%DADOS:ç=c%
set DADOS=%DADOS:Ç=C%

echo Original:

echo %ORIGINAL%

echo -----------------

echo Trocado:

echo %DADOS%

pause

From what I understood of your question (create folder with the name of "user"), using the mkdir would look like this:

@echo off

chcp 1252>nul

:: Pega o que o usuário digitar
set /p NOME="Qual seu nome?"

:: Nome da pasta
set NOME_PASTA=%NOME%

set NOME_PASTA=%NOME_PASTA:â=a%
set NOME_PASTA=%NOME_PASTA:Â=A%
set NOME_PASTA=%NOME_PASTA:à=a%
set NOME_PASTA=%NOME_PASTA:À=A%
set NOME_PASTA=%NOME_PASTA:á=a%
set NOME_PASTA=%NOME_PASTA:Á=A%
set NOME_PASTA=%NOME_PASTA:ã=a%
set NOME_PASTA=%NOME_PASTA:Ã=A%
set NOME_PASTA=%NOME_PASTA:ê=e%
set NOME_PASTA=%NOME_PASTA:Ê=E%
set NOME_PASTA=%NOME_PASTA:è=e%
set NOME_PASTA=%NOME_PASTA:È=E%
set NOME_PASTA=%NOME_PASTA:é=e%
set NOME_PASTA=%NOME_PASTA:É=E%
set NOME_PASTA=%NOME_PASTA:î=i%
set NOME_PASTA=%NOME_PASTA:Î=I%
set NOME_PASTA=%NOME_PASTA:ì=i%
set NOME_PASTA=%NOME_PASTA:Ì=I%
set NOME_PASTA=%NOME_PASTA:í=i%
set NOME_PASTA=%NOME_PASTA:Í=I%
set NOME_PASTA=%NOME_PASTA:õ=o%
set NOME_PASTA=%NOME_PASTA:Õ=O%
set NOME_PASTA=%NOME_PASTA:ô=o%
set NOME_PASTA=%NOME_PASTA:Ô=O%
set NOME_PASTA=%NOME_PASTA:ò=o%
set NOME_PASTA=%NOME_PASTA:Ò=O%
set NOME_PASTA=%NOME_PASTA:ó=o%
set NOME_PASTA=%NOME_PASTA:Ó=O%
set NOME_PASTA=%NOME_PASTA:ü=u%
set NOME_PASTA=%NOME_PASTA:Ü=U%
set NOME_PASTA=%NOME_PASTA:û=u%
set NOME_PASTA=%NOME_PASTA:Û=U%
set NOME_PASTA=%NOME_PASTA:ú=u%
set NOME_PASTA=%NOME_PASTA:Ú=U%
set NOME_PASTA=%NOME_PASTA:ù=u%
set NOME_PASTA=%NOME_PASTA:Ù=U%
set NOME_PASTA=%NOME_PASTA:ç=c%
set NOME_PASTA=%NOME_PASTA:Ç=C%

:: Exibe o nome digitado
echo Nome do usuário %NOME%

:: Quebra de linha na tela
echo.

:: Exibe o nome da pasta
echo Nome da pasta %NOME_PASTA%

:: Cria pasta (coloque a pasta)
mkdir "%NOME_PASTA%"

I know very little about batch/cmd and Windows, but I think maybe with a for can simplify, but the result would be basically the same result. About more complex characters and variations of encodings the use of .bat by itself is already extremely complicated and in this case it would be more worthwhile to write in another language (if it is a script to help in some work) or if to create something for the end user the same would be to create a "real program", with a language that provides you the Apis (libraries) necessary to facilitate.

  • Obgd was what I needed. I noticed an error in this second code in the last line mkdir %NOME_PASTA% right would be %DATA not ? vlws

  • @The program corrected the example, it was in the last edition I did, now this ok. The NAME variable is the name of the user and NOME_PASTA the name without accents to generate the folder

2

Welcome to the ONLY!


No command to create user named folders without the accents!


inserir a descrição da imagem aqui


Unfortunately we do not have a command to perform the task of "remove accents" provided by the operating system itself, but what we can do to remove/convert accented characters is to use a bat along with C#, and that, at runtime, can give the necessary treatment in removing accents in the string, and create the folder "disabled".


Below a bat that during its execution, will generate a code C# to be used in the task of converting the accented strings, allowing the creation of folders with the names of the system users without the respective accents:

Obs.: To use the bat, use in the same folder where you will create the folders with the names, or, you have to replace in line 8, where you have "_path=." for _path=drive\caminho where you need to create folders:


@echo off & setlocal EnableExtensions EnableDelayedExpansion 

cd /d "%~dp0" && title >nul && title Q399493 & >nul 2>nul chcp 1252
for /f ^delims^=*^~ %%i in ('%__APPDIR__%\where wmic.exe')do set _wmic=%%~i
set /a "_cnt=0" && set "_p=pathping 127.1 -n -q 1 -p 50" && set "_usr_log=%temp%\tmp_usr_.log"

for /f %%a in ('"prompt $h&for %%b in (0) do rem"')do set "_bs=%%a"
2>nul >nul del /q /f "%temp%\Rm_Acnt.cs" & 2>nul >nul del /q /f "!_usr_log!" 
set "_csc=%windir%\Microsoft.NET" & set "_where=%__appdir__%where.exe" & set "_path=."
set "_arg=/t:exe /out:"%tmp%\Rm_Acnt.exe" "%tmp%\Rm_Acnt.cs" /platform:anycpu /unsafe+ /w:0 /o /nologo"
!_wmic! /append:"!_usr_log!" useraccount where (status='ok') get name /format:list|findstr .>nul

cd /d "%temp%" && >"%temp%\Rm_Acnt.cs"^
    (
     echo/using System;namespace SO_BR_Q399493 ^{class Program ^{static void Main^(string[] args^)^{^{
     echo/byte[] tempBytes; tempBytes = System.Text.Encoding.GetEncoding^("ISO-8859-8"^).GetBytes^(args[0]^);
     echo/string asciiStr = System.Text.Encoding.UTF8.GetString^(tempBytes^);Console.Write^(asciiStr^);^}^}^}^}
    )

for /f  delims^=^ eol^=* %%i in ('!_where! /r "!_csc!" "csc.exe"^|findstr /lic:"k\v2\."')do "%%~i" !_arg!
2>nul >nul !_p! & for /f ^tokens^=2^delims^=^=  %%f in ('type "!_usr_log!"^|findstr .')do set "_str=%%~f" && call :^] 
2>nul >nul del /q /f "%temp%\Rm_Acnt.*" & 2>nul >nul del /q /f "!_usr_log!" & goto :eof

:^]
for /f delims^=^ eol^=* %%B in ('Rm_Acnt.exe "!_str!"')do call cmd /v/c && cmd /v/c mkdir "!_path!%%~B" 2>nul && (
set "_msg=Usuario: "!_str!" Pasta Criada Em: "!_path!%%~B"" )||( set "_msg=Usuario: "!_str!" Pasta Existe: "!_path!%%~B"")
for /f delims^=^ eol^=* %%a in ('"cmd /u /c echo=!_msg!|find /v """')do set /p "=^|!_bs!%%a"<nul & >nul !_p!
set /p "_str="<nul & echo/ & exit /b

  • Some comments on code/execution ::
@echo off & setlocal EnableExtensions EnableDelayedExpansion 

:: prepara o ambiente para execução setando variaveis para execução em looping ::
cd /d "%~dp0" && title >nul && title Q399493 & >nul 2>nul chcp 1252

:: seta o caminho para usar o wmic.exe no loop, seta o arquivo para salvar nome de usuário  :: 
for /f ^delims^=*^~ %%i in ('%__APPDIR__%\where wmic.exe')do set _wmic=%%~i
set /a "_cnt=0" && set "_p=pathping 127.1 -n -q 1 -p 50" && set "_usr_log=%temp%\tmp_usr_.log"

:: Cria variaveis para uso na busca e na execução da compílação c#/exe + tags :: 
for /f %%a in ('"prompt $h&for %%b in (0) do rem"')do set "_bs=%%a"
2>nul >nul del /q /f "%temp%\Rm_Acnt.cs" & 2>nul >nul del /q /f "!_usr_log!"
set "_csc=%windir%\Microsoft.NET" & set "_where=%__appdir__%where.exe" & set "_path=."
set "_arg=/t:exe /out:"%tmp%\Rm_Acnt.exe" "%tmp%\Rm_Acnt.cs" /platform:anycpu /unsafe+ /w:0 /o /nologo"

:: Coleta e salva os nomes dos usuários em arquivo para uso em loop for + c#/exe  :: 
!_wmic! /append:"!_usr_log!" useraccount where (status='ok') get name /format:list|findstr .>nul


:: gera código em C# (C sharp), que será usado na execução para remover o acento :: 
cd /d "%temp%" && >"%temp%\Rm_Acnt.cs"^
    (
     echo/using System;namespace SO_BR_Q399493 ^{class Program ^{static void Main^(string[] args^)^{^{
     echo/byte[] tempBytes; tempBytes = System.Text.Encoding.GetEncoding^("ISO-8859-8"^).GetBytes^(args[0]^);
     echo/string asciiStr = System.Text.Encoding.UTF8.GetString^(tempBytes^);Console.Write^(asciiStr^);^}^}^}^}
    )

:: busca o compilador C# (csc.exe) no sistema, e o usa para compilar o código em C# criando o executável     ::
:: lê arquivos com nomes dos usuários faz chamada do label :^] que vai executar C#/exe que remove os acentos ::
for /f  delims^=^ eol^=* %%i in ('!_where! /r "!_csc!" "csc.exe"^|findstr /lic:"k\v2\."')do "%%~i" !_arg!
2>nul >nul !_p! & for /f ^tokens^=2^delims^=^=  %%f in ('type "!_usr_log!"^|findstr .')do set "_str=%%~f" && call :^] 
2>nul >nul del /q /f "%temp%\Rm_Acnt.*" & 2>nul >nul del /q /f "!_usr_log!" & goto :eof


:: faz a conversão dos carateres nos nomes dos usuários e verifica se a pasta existe com nome sem acento existe  ::
:: vai criar caso não existe pasta, e emitir uma mensagem na excução já sinalizando se foi criada ou se existia  ::
:^]
for /f delims^=^ eol^=* %%B in ('Rm_Acnt.exe "!_str!"')do call cmd /v/c && cmd /v/c mkdir "!_path!%%~B" 2>nul && (
set "_msg=Usuario: "!_str!" Pasta Criada Em: "!_path!%%~B"" )||( set "_msg=Usuario: "!_str!" Pasta Existe: "!_path!%%~B"")
for /f delims^=^ eol^=* %%a in ('"cmd /u /c echo=!_msg!|find /v """')do set /p "=^|!_bs!%%a"<nul & >nul !_p!
set /p "_str="<nul & echo/ & exit /b

  • C# code used in accent removal task:

using System;
namespace SO_BR_Q399493 
{
  class Program 
  {
   static void Main(string[] args)
    {
      {
       byte[] tempBytes; tempBytes = System.Text.Encoding.GetEncoding("ISO-8859-8").GetBytes(args[0]);
       string asciiStr = System.Text.Encoding.UTF8.GetString(tempBytes);
       Console.Write(asciiStr);
      }
    }
  }
}

Source: Answer published in the SO/en by @azrafe7


Obs.: Consider the suggestion to familiarize yourself with some language programming, such as the C, C#, Python, PHP..., bat/cmd are very limited.



  • 6

    I’m really impressed with the work that and the amount of code in your responses involving . bat and dos. However, I think that, precisely because this type of code is the answer to the question in DOS (.bat), they should be accompanied by the dirty use of some other language - the necessary code is the very demonstration that the . bat is not the best tool for almost any of the problems. (NB - I’m commenting now, I’m not one of the downvoters)

  • 5

    I didn’t respond with another language. But the purpose is to help people, not to help themselves get tangled up even more because they don’t know other technologies. You answer with a . correct bat that makes the service despite being 120% impossible to maintain, does not prevent in the text you invite the author to explore other technologies and even add examples.

  • 5

    In this case, the "hard work" is done in C#, and the program code in C# itself is overshadowed by the necessary Escaping, within .bat. It would no longer be educational to put another response session, with the code in c# cleaned and idented, and the instructions for the compilation and use of?

  • 4

    And there’s a reason for this: in other languages, a task like this is done by a program 2 orders of magnitude simpler. That is, with the same effort you make a program 100 times better! You can do amazing things with . bat - but in general, who asks how to do something more complex in . bat is precisely because it knows no alternatives. The business of the site is to try to make people learn more so they can solve their own problems.

Browser other questions tagged

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