Windows – How to remove non-ASCII characters from a string in bat script

batch filewindows 7

Sometimes the %COMPUTERNAME% variable value contains non-ASCII characters, but when I use this variable in a batch script, I need to keep only the ASCII characters of the correlated value.

How could I go about getting a string value with non-ASCII characters to only display the ASCII characters using a batch script?

Best Answer

You can use some of the methods as listed on Remove special characters from a string using Regular Expression (Regex) with a batch script as the one below for example.

Just use the native power of Windows as designed by Microsoft and get this job done simply by using PowerShell in your batch script to set the %computername% without the non-ASCII character for what's returned with the PowerShell commands.


Batch Script to Remove Non-Ascii

Be sure to set the SET PCName= variable accordingly in your script to incorporate with however your logic already works (e.g. SET PCName=%computername%, etc.)

Rather than using the DO ECHO %%~F with the FOR /F loop in the below script, you could set that to DO SET PCNameASCII=%%~F instead for example and then the %PCNameASCII% variable can be incorporated into the rest of your batch script logic accordingly and that'd always be the %ComputerName% variable value but only the ASCII characters.

@ECHO ON

SET PCName=Laäff¥yTaäffi¡

SET PSScript=%Temp%\~tmpRemovenonascii.ps1
IF EXIST "%PSScript%" DEL /Q /F "%PSScript%"

ECHO $String = '%PCName%'                                             >>"%PSScript%"
ECHO $String = $String -replace '[^^\x30-\x39\x41-\x5A\x61-\x7A]+', ''>>"%PSScript%"
ECHO ECHO $String                                                     >>"%PSScript%"

SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD /D "%PowerShellDir%"
FOR /F "TOKENS=*" %%F IN ('Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%'"') DO ECHO %%~F

PAUSE

GOTO EOF

See the Further Resources section and the notes therein for other methods using Regex to remove special characters, etc.


Before (non-ascii)

LaõffÑyTaõffií

After (results)

LaffyTaffi

Further Resources

  • Replace()
  • FOR /F
  • Remove special characters from a string using Regular Expression (Regex)

  • # Regular Expression - Using the \W (opposite of \w) 
    $String -replace '[\W]', ''
    
    # Regular Expression - Using characters from a-z, A-Z, 0-9 
    $String -replace '[^a-zA-Z0-9]', ''
    
    # Regular Expression - Using ASCII 
    $String -replace '[^\x30-\x39\x41-\x5A\x61-\x7A]+', ''
    
    # Regular Expression - Unicode - Matching Specific Code Points 
    $String -replace '[^\u0030-\u0039\u0041-\u005A\u0061-\u007A]+', ''
    
    # Regular Expression - Unicode - Unicode Categories 
    $String -replace '[^\p{L}\p{Nd}]', ''
    
    # Regular Expression - Unicode - Unicode Categories
    #  Exceptions: We want to keep the following characters: ( } _ 
    $String -replace '[^\p{L}\p{Nd}/(/}/_]', ''