How to Replace special characters in batch?

Viewed 90

I am starting to program in batch and I have this problem. If I enter the variable by argument, it does not change the accented characters or character ñ, but if I declare the variable directly in the program, it does.

@echo off 

set variable=%1
set variable=%variable:á=a%
set variable=%variable:é=e%
set variable=%variable:i=a%
set variable=%variable:ñ=n%
echo %variable%

set var=Holáéñ
set var=%var:á=a%
set var=%var:é=e%
set var=%var:ñ=n%
echo %var% 

C:\Users\bruno\OneDrive\Escritorio\powershell>prova holáéñi
holáéña
Holaen

How can i fix this?

1 Answers

Refer to Mofi's comment :

You have to write the non ASCII characters using the code page which is displayed on running in a command prompt window with your user account chcp like the OEM code page 850 and not using the code page defined for Windows GUI applications as default for single byte per character encoded text files like Windows-1252. Or the batch file contains at top after @echo off a line like %SystemRoot%\System32\chcp.com 1252 >nul


You can write your batch like that :

@echo off
setlocal enableExtensions enableDelayedExpansion
set "cp="
for /F "tokens=2 delims=:." %%a in ('chcp') do set "cp=%%~a"
if not defined cp set "cp=850"
>nul chcp 1256 
set var=Holáéñ
set var=!var:á=a!
set var=!var:é=e!
set var=!var:ñ=n!
echo !var! 
>nul chcp %cp%
endlocal
pause & exit
Related