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-06-21 04:29
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to filter specified characters in a variable [Solved] View 8,216 Replies 31
Floor 16 Posted 2006-09-06 03:04 ·  中国 甘肃 张掖 临泽县 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
The things above are very practical, take them
Floor 17 Posted 2006-09-09 04:44 ·  中国 北京 鹏博士BGP
初级用户
Credits 98
Posts 35
Joined 2006-01-14 22:43
20-year member
UID 48986
Gender Male
Status Offline
After my experiment, using `set name=|` is not okay. But using `set /p name=input:` to input and then it is okay, as shown in the figure:
Then you can use the if statement to judge each character one by one. This can indeed be done and I have experimented and succeeded. It can filter out many sensitive characters. Then combined with the limitation of findstr, the purpose can be achieved: it is stipulated that the input is 6-12 uppercase letters, lowercase letters, numbers and the symbol _, and other symbols are filtered. It is necessary to ensure that each bit is the characters mentioned above.
Look at my code:

@setlocal
@set /p user=Input:
:: Filter empty characters and input with less than six characters
@if "%user%"=="" goto error
@if "%user:~5,1%"=="" goto error
:: Filter "|"
@if "%user:~0,1%"=="|" goto error
@if "%user:~1,1%"=="|" goto error
@if "%user:~2,1%"=="|" goto error
@if "%user:~3,1%"=="|" goto error
@if "%user:~4,1%"=="|" goto error
@if "%user:~5,1%"=="|" goto error
@if "%user:~6,1%"=="|" goto error
@if "%user:~7,1%"=="|" goto error
@if "%user:~8,1%"=="|" goto error
@if "%user:~9,1%"=="|" goto error
@if "%user:~10,1%"=="|" goto error
@if "%user:~11,1%"=="|" goto error
:: Filter other sensitive symbols in the same way
:: Force the effective bit to be at most 12 bits
@set user=%user:~0,12%
:: Limit the input characters to uppercase letters, lowercase characters, numbers and _
@echo %user% |findstr "[A-Za-z0-9_]"
@if errorlevel 1 goto error
@goto next
endlocal
:next
@echo succeed!
@goto end
:error
@echo Input error!
:end

But there is a symbol " The little brother of the quotation mark doesn't know what to do, and can't filter it out no matter what. It's depressing. And when inputting Chinese, it will prompt that it is filtered, but if there are English letters after Chinese, it will pass the filter successfully.
These two points are to ask the great gods!
Attachments
未命名.GIF
Floor 18 Posted 2006-09-09 23:11 ·  中国 北京 鹏博士BGP
初级用户
Credits 98
Posts 35
Joined 2006-01-14 22:43
20-year member
UID 48986
Gender Male
Status Offline
...... No one replies?
No one will?
Moderator, give an answer ah
Floor 19 Posted 2006-09-10 01:02 ·  中国 北京 鹏博士BGP
初级用户
Credits 98
Posts 35
Joined 2006-01-14 22:43
20-year member
UID 48986
Gender Male
Status Offline
Could it be that really no one knows how to do it?!
Floor 20 Posted 2006-09-10 23:03 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by fornever at 2006-9-9 04:45:
It is not possible to use set name=| through my experiment.
But it is possible after inputting with set /p name=input:, as shown in the figure:
Then you can use the if statement to judge each character one by one, which I have indeed experimented with...

  Although it is possible to input in the CMD command line, it will still cause errors when used in batch processing. Confused.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 21 Posted 2006-09-10 23:43 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Well,Assigning | to a variable in batch is not impossible:

@echo off
set "str=^|"
echo %str%
pause


Regarding filtering characters, the following code is written casually, hoping to be helpful to friends in need:


@echo off

:start
setlocal
cls
set /p var=Please enter characters:
call :filter "%var%"
endlocal
goto start

:filter
setlocal
set "str=%~1★"
set "str=%str:|=%"
set "str=%str:&=%"
set "str=%str:"=%"
echo The filtered characters are: %str:~0,-1%
pause
endlocal
goto :eof

It seems that |, ||, &, &&, " can be filtered out. You can test and change it yourself.
Floor 22 Posted 2006-09-11 00:42 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
The code of moderator 3742668 can be filtered out by using call for calling. I really can't think of it for a while

In the first case, if no escape characters are added, it can't pass in the batch processing, but it can run successfully in the CMD command line. I'm still quite puzzled about this point.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 23 Posted 2006-09-11 09:06 ·  中国 北京 鹏博士BGP
初级用户
Credits 98
Posts 35
Joined 2006-01-14 22:43
20-year member
UID 48986
Gender Male
Status Offline
Originally posted by namejm at 2006-9-10 23:03:

Although it works when entered in the CMD command line, it still causes errors when used in batch processing. I'm confused.


In batch processing, if you follow "set /p user=input:" with an if statement for judgment, it works, but if there's no IF, it also doesn't work.... I can't figure it out

And "set "str=|" " works both in the command line and in batch processing. Could it be related to patches>?

Here is my patch information:

Installed 111 patches.
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: File 1
: Q147222
: S867460 - Update
: KB917734_WMP9
: KB834707 - Update
: KB873339 - Update
: KB885835 - Update
: KB885836 - Update
: KB886185 - Update
: KB886677 - Update
: KB887472 - Update
: KB888302 - Update
: KB890046 - Update
: KB890859 - Update
: KB891781 - Update
: KB893756 - Update
: KB893803v2 - Updat
: KB894391 - Update
: KB896358 - Update
: KB896423 - Update
: KB896424 - Update
: KB896428 - Update
: KB898461 - Update
: KB899587 - Update
: KB899589 - Update
: KB899591 - Update
: KB900485 - Update
: KB900725 - Update
: KB901017 - Update
: KB901190 - Update
: KB901214 - Update
: KB904706 - Update
: KB905414 - Update
: KB905749 - Update
: KB908519 - Update
: KB908531 - Update
: KB910437 - Update
: KB911280 - Update
: KB911562 - Update
: KB911567 - Update
: KB911927 - Update
: KB912919 - Update
: KB913580 - Update
: KB914388 - Update
: KB914389 - Update
: KB916595 - Update
: KB917159 - Update
: KB917344 - Update
: KB917422 - Update
: KB917953 - Update
: KB918439 - Update
: KB918899 - Update
: KB920214 - Update
: KB920670 - Update
: KB920683 - Update
: KB921398 - Update
: KB921883 - Update
: KB922616 - Update

[ Last edited by fornever on 2006-9-11 at 09:28 ]
Floor 24 Posted 2006-09-11 09:45 ·  中国 北京 鹏博士BGP
初级用户
Credits 98
Posts 35
Joined 2006-01-14 22:43
20-year member
UID 48986
Gender Male
Status Offline
RE 3742668:
Little brother is a newbie, many things in your code are not very understandable
call :filter "%var%"
call to the filter label and use the var variable as a parameter, but the var variable is not used under the filter label?
set "str=%~1★"
And what does the above sentence mean

Thank you!
Floor 25 Posted 2006-09-11 09:49 ·  中国 北京 鹏博士BGP
初级用户
Credits 98
Posts 35
Joined 2006-01-14 22:43
20-year member
UID 48986
Gender Male
Status Offline
I also have a discovery. After I experimented with the moderator's code, I found that if you put "&" at the front when entering characters, then all characters after "&" will be ignored... Can you explain it?
Floor 26 Posted 2006-09-11 20:57 ·  中国 北京 联通
银牌会员
★★★
努力做坏人
Credits 1,185
Posts 438
Joined 2006-08-28 12:00
19-year member
UID 61449
From 北京
Status Offline
@echo off

:start
setlocal
cls
set /p var=Please enter characters:
call :filter "%var%"
endlocal
goto start

:filter
setlocal
set "str=%~1★"
set "str=%str:|=%"
set "str=%str:&=%"
set "str=%str:"=%"
echo The filtered characters are: %str:~0,-1%
pause
endlocal
goto :eof

I'll babble a few words. The line call :filter "%var%" is using CALL to invoke the procedure to execute the corresponding code. "%var%" is passed as the %1 parameter to the executed code. The moderator mainly filters out the three characters | & ", and the reason for using %str:~0,-1% to display is because the first line set "str=%~1★" removes the last five-pointed star...
我今后在论坛的目标就是做个超级坏人!!!
Floor 27 Posted 2006-09-12 23:08 ·  IANA 局域网IP(Private-Use)
新手上路
Credits 2
Posts 1
Joined 2006-01-12 01:14
20-year member
UID 48836
Status Offline
%Var:a=b%
Replace all occurrences of "a" with "b" in the environment variable Var
Floor 28 Posted 2006-09-13 10:10 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Today the LZ told me that if "and & are connected together, it will filter out all characters after &. It seems that there are still places that need to be considered in the code of moderator 3742668. The modification is as follows, which can avoid this problem:


@echo off

:start
setlocal
cls
set /p var=Please enter characters:
set "var=%var:"=%"
call :filter "%var%"
endlocal
goto start

:filter
setlocal
set "str=%~1★"
set "str=%str:|=%"
set "str=%str:&=%"
set "str=%str:"=%"
echo The filtered characters are: %str:~0,-1%
pause
endlocal
goto :eof


[ Last edited by namejm on 2006-9-13 at 10:11 ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 29 Posted 2006-09-15 10:20 ·  中国 北京 鹏博士BGP
初级用户
Credits 98
Posts 35
Joined 2006-01-14 22:43
20-year member
UID 48986
Gender Male
Status Offline
First, thank you very much to Brother NAMEJM for his selfless help!

Now I recall that I was a bit foolish. Although it ended up being a bit different from my initial idea, there is finally a solution.

For my method, actually it's mainly a problem with quotation marks, and other characters can be checked smoothly. If I still followed my original intention, I just needed to add a line at the front "set "var=%var:"=%" to handle the quotation marks. But then there would be no prompts, but it would just pass through while processing the characters (filtering out the quotation marks). In this case, I could just use echo to explain it. Looking back, my code would be so bloated when written out, sigh!

So the conclusion is that if you want concise code, just use set "var=%var:"=%" to filter out sensitive characters (mainly sensitive to the findstr statement), and then use findstr "[A-Za-z0-9_]" plus if errorlevel to directly restrict, without having to think about the situations of the remaining characters (there are many symbols, tabs, etc.).

If you want to have prompts when there's an error, just use set /p var=input: followed by the statement if "%var%"=="|" goto error to jump.

That's all. I can't think of anything else. Let's discuss together if there are any other situations! Untying the bell still requires the person who tied it, haha!
Floor 30 Posted 2007-11-22 23:13 ·  中国 湖北 黄冈 电信
初级用户
Credits 25
Posts 16
Joined 2006-10-12 00:35
19-year member
UID 65375
Status Offline
Forum Jump: