Home > database >  How can I find non ASCI in content in file in batch script?
How can I find non ASCI in content in file in batch script?

Time:12-07

In batch script I want find content in a.txt in a.txt I have more records how to check record contain nonaci and write to b.txt ? I have code to mid string but fail too

@echo off


setlocal enableDelayedExpansion
SETLOCAL 
set _char= "123456789~abcdef0"
SET /A _startchar=1
SET /A _length=1


for /L %%a in (32,1,125) do (

  cmd /c exit %%a
  
  
  echo !=exitcodeAscii!
  if "!=exitcodeAscii!" EQU "%_char%" echo -- %%a
  CALL SET _substring=!!_char:!_startchar!,2!!
  ECHO !_substring! --- !_startchar!
  SET /A _startchar=!_startchar!   1
   
)

CodePudding user response:

The following defines a variable with valid Ascii characters (excluding ", handled by substitution) for character by character comparison.

Note: I have not tested this for all possible non ascii characters or code pages, however it may be of some use or guidance to you.

@Echo off

 Set "test=%~1"
 SETLOCAL EnableDelayedExpansion
 CHCP 850 > nul
 Set "ASCII= ^!"
 For /l %%i in (35 1 126) Do (
  Cmd /c Exit %%i
  Set "ASCII=!ASCII!!=ExitCodeAscii!"
 )

 Set "Demo=test) & <. %% ▒ This"
 If defined test (
  Call:IsASCII test && (
   Endlocal & Exit /b 0
  ) || (
   For %%v in ("!Errorlevel!")Do Endlocal & Exit /b %%~v
  )
 ) Else (
  Call:IsASCII Demo
  Call:IsASCII Ascii
 )

 Pause
ENDLOCAL & Goto:Eof

:IsASCII ==============================| Author : T3RRY |
rem returns 0 to STDOUT and Errorlevel if string is ASCII
rem If Not ASCII:
rem - returns 0 indexed position of character to STDOUT
rem - returns 1 indexed position of character to Errorlevel

SETLOCAL EnableDelayedExpansion
 Set "String=!%~1!"
 If not defined string Exit /b 0
 Set "isAscii=-1"
 For /l %%i in (0 1 1000)Do If not "!String:~%%i,1!"=="" (
  Set "Char=!String:~%%i,1!"
  Set ^"Char=!Char:"=.!"
  For /l %%c in (0 1 93)Do If not "!ASCII:~%%c,1!" == "" (
   Set "C_Char=!ASCII:~%%c,1!"
   if "!Char!"=="!C_Char!" Set /A "isAscii =1"
  )
  If not !isAscii!==%%i For /f delims^= %%G in ('Set /A %%i 1')Do (
   Echo(%%i
   For /f "Delims=" %%v in ("!Char!")Do Endlocal & Set "Char=%%v" & Exit /B %%G
  )
 )
 Echo(0
ENDLOCAL & Exit /b 0
  • Related