China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-31 22:26
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to determine what type of character the input content is? View 3,298 Replies 8
Original Poster Posted 2006-06-15 16:10 ·  中国 四川 雅安 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
If you want to judge whether the input is pure letters, pure numbers or pure Chinese characters after entering a line of characters, how to solve it?
Floor 2 Posted 2006-06-16 10:45 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
Brother namejm's imagination is really rich! You can come up with this too. This is really amazing. Judging whether a line of characters is a special symbol, a Chinese character, or a letter is even more difficult. There can be many situations:
1. All are special symbols in a line
2. All are Chinese characters in a line
3. All are letters in a line
4. All are numbers in a line
5. There are symbols + Chinese characters in a line
I won't write anymore. There are too many situations. There is no accurate judgment object, and it's not easy to implement
Floor 3 Posted 2006-06-16 11:10 ·  IANA 局域网IP(Private-Use)
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
Put forward an idea, cscript + regex

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>"
Floor 4 Posted 2006-06-16 11:14 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
I guess I can only use regular expressions for searching to complete this task........Oh my goodness, Brother EST, you, you, you, you, you, you have already become a "Silver Member", OH, MY GOD!!!
Floor 5 Posted 2006-06-16 13:39 ·  中国 四川 雅安 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
I want to judge whether the entered content is a date format such as 1, 2, 3 or M, T, W (the first letters of the week), etc., and use it in my file backup tool. It is best to implement it only with commands in CMD.
Floor 6 Posted 2006-06-16 16:13 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
This is to judge whether an input exists:
@echo off
setlocal
set /p a=Judge whether the entered character is within the range:
set b=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,M,T,W,Th,F,S,Su
echo %b%|findstr /i "%a%" >nul&&echo Your input is correct||echo I'm sorry comrade, the date or week you entered does not exist
endlocal


This is to judge the judgment of continuous input of three, for example: judge whether 1,6,9 is correct

@echo off
setlocal
set b=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,M,T,W,Th,F,S,Su
set /p a=Judge whether the entered character is within the range:
for /f "tokens=1,2,3 delims=," %%a in ("%a%") do (
echo %b%|findstr /i "%%a" >nul&&echo %b%|findstr /i "%%b" >nul&&echo %b%|findstr /i "%%c" >nul&&echo You input correctly||echo You input

wrong
)
endlocal


This is the current situation, the limitation is too big, but see if it can be simplified or increase the judgment ability
Floor 7 Posted 2006-06-17 02:31 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re namejm:

The regular expression of findstr itself can realize the detection of character types.

Pure letters: findstr /r "^[a-zA-Z][a-zA-Z]*$"
Pure numbers: findstr /r "^[0-9][0-9]*$"
Specific letters: findstr /r "^[MTW][MTW]*$"

But considering your specific application, it seems that the success or failure of the execution of the at command can be directly used to judge the rationality of the input parameters. Of course, it also has the defect that it cannot point out which one of the multiple parameters is invalid.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 8 Posted 2006-06-17 08:45 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
Moderator willsort, can you post the help for the FINDSTR command regarding regular expressions, please? Thanks..........
Floor 9 Posted 2006-06-17 09:03 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
FINDSTR

Search for strings in files.

Syntax
FINDSTR

Key
string Text to search for.
pathname(s) The file(s) to search.
/C:string Use string as a literal search string.
/G:file Get search string from a file (/ stands for console).
/F:file Get a list of pathname(s) from a file (/ stands for console).
/d dirlist Search a comma-delimited list of directories.

options may be any combination of the following switches:
/I Case-insensitive search.
/S Search subfolders.
/P Skip any file that contains non-printable characters

/L Use search string(s) literally.
/R Use search string(s) as regular expressions.(default)

/B Match pattern if at the Beginning of a line.
/E Match pattern if at the END of a line.

/X Print lines that match exactly.
/V Print only lines that do NOT contain a match. /N Print the line number before each line that matches.
/M Print only the filename if a file contains a match.
/O Print character offset before each matching line.

/a color_attribute Display filenames in colour (2 hex digits)

Options in bold are new in Windows 2000When the search string contains multiple words (separated with spaces) then FINDSTR will show show lines that contains any one word - (an OR of each word) - this behaviour is reversed if the string argument is prefixed with /C.


Regular Expressions
(Searching for patterns of text)

The FINDSTR syntax notation can use the following metacharacters which have special meaning either as an operator or delimiter.

. Wildcard: any character

* Repeat: zero or more occurances of previous character or class

^ Line position: beginning of line
$ Line position: end of line

Character class: any one character in set
Inverse class: any one character not in set

Range: any characters within the specified range

\x Escape: literal use of metacharacter x

\<xyz Word position: beginning of
xyz\> Word position: end of word
Metacharacters are most powerful when they are used together. For example, the combination of the wildcard character (.) and repeat (*) character is similar in effect to the filename wildcard (*.*)

.* Match any string of charactersThe .* expression may be useful within a larger expression, for example f.*ing will match any string beginning with F and ending with ing.

Examples:

FINDSTR "granny Smith" MyFile.txt
searches for "granny" OR "Smith" in MyFile.txt.

FINDSTR /C:"granny Smith" MyFile.txt
searches for "granny Smith" in MyFile.txt
This is effectively the same as the FIND command

To search every file in the current folder and all subfolders for the word "Smith",
regardless of upper/lower case use:

FINDSTR /s /i smith *.*

Note that /S will only search below the current directory

To find every line containing the word SMITH, preceeded by any number of spaces, and to prefix each line found with a consecutive number:

FINDSTR /b /n /c:" *smith" MyFile.txt

Finding a string only if surrounded by the standard delimiters
To find the word "computer", but not the words "supercomputer" or "computerise":

FINDSTR "\<computer\>" MyFile.txt

Now assume you want to find not only the word "computer", but also any other words that begin with the letters comp, such as "computerise" or "compete"

FINDSTR "\<comp.*" MyFile.txt

Example of a literal search

Searching a text file that contains the following

the quick brown fox
the darkbrown fox
the really *brown* foxFINDSTR /r .*brown MyFile.txt
or
FINDSTR .*brown MyFile.txt
Will both match the word "brown" in all 3 lines

FINDSTR /L *brown* MyFile.txt
Will only match the last string

Using a script file

Multiple search criteria can be specified with a script file /G.
Multiple files to search can be specified with a source file /F.

When preparing a source or script file, place each item on a new line.

For example: to use the search criteria in CRIT.TXT and
search the files listed in FILES.TXT then
store the results in the file RESULTS.OUT, type

FINDSTR /g:CRIT.TXT /f:FILES.TXT > results.out

Errorlevel

When an item is not found FINDSTR will return an errorlevel >0

Echo 12G6 |FindStr /R ""
If %ERRORLEVEL% EQU 0 echo The string contains one or more numeric characters

Echo 12G6 |FindStr /R ""
If %ERRORLEVEL% EQU 0 echo The string contains one or more non numeric characters

Bugs
In early versions of FindStr /F:file a path length of more than 80 chars will be truncated.

"Twenty years from now, you will be more disappointed by the things you didn't do than by the ones you did do. So throw off the bowlines, sail away from the safe harbour. Catch the trade winds in your sails. Explore. Dream. Discover." - Mark Twain
Forum Jump: