在批處理腳本中,我想在 a.txt 中查找 a.txt 中的內容我有更多記錄如何檢查記錄是否包含 nonaci 并寫入 b.txt ?我有中間字串的代碼但也失敗了
@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
)
uj5u.com熱心網友回復:
下面定義了一個具有有效 Ascii 字符(不包括",由替換處理)的變數,用于逐字符比較。
編輯:對高性能版本進行了更改。
@Echo off
REM the label marker ":#" is used within this script to delimit help output.
:#
:# ========================= ASCII string filter v3.1 by T3RRY ======================
Rem - This script iterates over an input string character by character and tests
Rem each character against a a whitelist of printable ASCII characters, with
Rem succesful matches used to build a new string containing only printable
Rem ASCII characters.
Rem - Switch /R modifies this script to into a testing tool
Rem to check if a string contains any NonASCII or nonprintable ASCII characters.
Rem - Errorlevel 0 indicates the string contains only printable ASCII characters
Rem - A Positive errorlevel is returned containing the 1 indexed position of the
Rem first NonASCII or nonprintable ASCII character found.
Rem - Execution time increases as string length increases. Each character in the
Rem string is tested against a whitelist containing 95 printable ASCII characters.
:#
:# Usage: Filepath <"String"> [ /P | /R ] | [ -? | /? | -help ]
:#
:# Rem to use from another batch file:
:# For /f delims^= %%G in ('FilePath "string"')Do Echo(%%G
:#
:# Accepts input String via doublequoted argument 1.
:# - No escaping of characters in the argument is required
:# - If unbalanced doublequotes exist in the string all doublequotes will be Removed.
:#
:# Use Switch /P to preserve original spaces
:# - Default behaviour is to Remove all double spaces from the string.
:#
:# Use Switch /R to reject input containing NonASCII characters
:# - If non ASCII character encountered, returns a positive errorlevel
:# ( the 1 indexed position of first non ASCII character encountered )
:#
Rem Version changes 08/Dec/2021 :
Rem - Added Help Switches -? /? and -help
Rem - Added switch: /R
Rem - Reject strings containing non ASCII characters. Default: Strip NonASCCi
Rem characters from the string.
Rem Note: this switch does not define Return[Len] or Return[String]
Rem Version changes 07/Dec/2021 :
Rem - Rewritten for much faster performance - NOTE:
Rem - Added Switch: /P
Rem - Preserve all whitespace. Default: multiple spaces truncated to single.
Rem - Renamed variable for returning String : Return[String]
Rem - Added variable Return[Len] to return 0 indexed string length.
Rem - Corrected handling of completely non ASCII strings to return empty / 0 Len
Rem ** Utilize alternate data stream to store variable containing printable ASCII
Rem characters so the variable only needs to be generated on first execution.
Rem ** Requires this batch file to be run from an NTFS drive.
:# =================================================================================
Set "{Input}=" & Set "Return[String]="
For /f "tokens=4 delims=: " %%G in ('CHCP')Do Set "Restore_Codepage=CHCP %%G > nul"
Rem default maximum stringlength to support. Modify here to propagate to RemoveChar loop and Return[Len]
Set "SupportLength=1000"
Setlocal EnableExtensions DisableDelayedExpansion
If "!!"=="" (
Rem return error to StdERR in event Delayed expansion is still in effect.
1>&2 Echo(Delayed Expansion must not be enabled prior to calling %~nx0
Pause
Exit /B 1
)
Rem the below can be used to Remove the aleternate data stream this file creates.
Rem Powershell -c "Remove-item -path '%~nx0' -Stream '*'"
Set "{input}=%~1"
If defined {Input} (
Set {input} | %SystemRoot%\System32\Findstr.exe /Xlic:"{input}=/?" /Xlic:"{input}=-?" /Xlic:"{input}=-help" > nul && (
Setlocal EnableDelayedExpansion
For /f "tokens=2* delims=#" %%G in ('%SystemRoot%\System32\Findstr.exe /blic:":# " "%~f0"')Do (
Set "Usage=%%G"
Echo(!Usage:Filepath=%~f0!
)
ENDLOCAL & ENDLOCAL
Exit /b 0
))
if not "%~2" == "" Set Params=%*
CHCP 65001 > nul
If not defined {input} (
Echo(Demo:
Set "{input}=this is a demo) * ! & ? ^= ? | ! <. ~ ^^ & %% ? ╔ § ? This"
Set {input}
)
Set "{input}=%{input}:!={EX}%"
Set "ASCII= !"
2> nul (
more < "%~f0:ASCII.dat" > nul || (
Setlocal EnableDelayedExpansion
For /l %%i in (34 1 126) Do (
Cmd /c Exit %%i
Set "ASCII=!ASCII!!=ExitCodeAscii!"
)
>"%~f0:ASCII.dat" (Echo(Set ^^"ASCII=!ASCII!")
ENDLOCAL
))
Set "ASCII="
For /f "delims=" %%G in ('More ^< "%~f0:ASCII.dat"')Do %%G
If not Defined ASCII (
2> nul (
Powershell.exe -c "Remove-item -path '%~nx0' -Stream '*'"
)
1>&2 Echo(An error has occured. Ensure "%~nx0" is located on an NTFS drive.
Pause
Exit /b 1
)
Set "ASCIISwitch[R]="
Set "ASCIISwitch[P]="
If Defined Params (
Set Params | %SystemRoot%\System32\findstr.exe /lic:" /P" > nul && Set "ASCIISwitch[P]=true"
Set Params | %SystemRoot%\System32\findstr.exe /lic:" /R" > nul && Set "ASCIISwitch[R]=true"
)
Set Div="is=#", "1/(is<<9)"
Set "count=%{input}%"
SETLOCAL EnableDelayedExpansion
Set "{DQ}=1"
Set ^"count=!count:"={DQ}!"
Set "null=%count:{DQ}=" & Set /A {DQ} =1& set "null=%"
Set /A !Div:#=%{DQ}% %% 2! 2> nul || Set ^"{input}=!{input}:"=!"
Rem RemoveChar loop - iterate over input character by character; Compare against each character in whitelist
Rem Removes Character if not present in Whitelist unless /R switch used, in which case rejects input and returns
Rem 1 indexed position of character.
Set "end=" & Set "New="
For /l %%i in (0 1 %SupportLength%)Do If not "!{input}:~%%i,1!"=="" (
Set "Char=!{input}:~%%i,1!"
Set "ISAscii="
For /l %%c in (0 1 94)Do If not "!ASCII:~%%c,1!" == "" (
Set "C_Char=!ASCII:~%%c,1!"
if "!Char!"=="!C_Char!" (
Set "New=!New!!Char!"
Set "ISAscii=true"
)
)
If Defined ASCIISwitch[R] (
If Not Defined ISAscii (
Endlocal & Endlocal & %Restore_Codepage%
For /f "delims=" %%G in ('Set /A %%i 1')Do Exit /b %%G
))
)
Set "{Input}=!New!"
If not Defined ASCIISwitch[P] (
For /l %%i in (0 1 9)Do if defined {Input} Set "{Input}=!{Input}: = !"
)
If defined {input} (
ENDLOCAL & Set "{input}=%{input}%"
) Else ENDLOCAL & Set "{input}="
If defined {input} (
ENDLOCAL & Set "Return[string]=%{input}:{EX}=!%" )
) Else ENDLOCAL & Set "Return[string]="
SETLOCAL EnableDelayedExpansion
Echo(!Return[string]!
Set "Return[Len]="
For /l %%i in (0 1 %SupportLength%)Do If not defined Return[Len] If "!Return[String]:~%%i,1!"=="" Set "Return[Len]=%%i"
ENDLOCAL & Set "Return[Len]=%Return[Len]%"
%Restore_Codepage%
Exit /B 0
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/376734.html
