![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-09-29 18:59 |
47,813 topics / 349,918 posts / today 0 new / 48,278 members |
| DOS批处理 & 脚本技术(批处理室) » How to filter specified characters in a variable [Solved] |
| Printable Version 9,559 / 31 |
| Floor16 vkill | Posted 2006-09-06 03:04 |
| 金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽 | |
|
The things above are very practical, take them
|
|
| Floor17 fornever | Posted 2006-09-09 04:44 |
| 初级用户 Posts 35 Credits 98 | |
|
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 (12.91 KiB) |
|
| Floor18 fornever | Posted 2006-09-09 23:11 |
| 初级用户 Posts 35 Credits 98 | |
|
...... No one replies?
No one will? Moderator, give an answer ah |
|
| Floor19 fornever | Posted 2006-09-10 01:02 |
| 初级用户 Posts 35 Credits 98 | |
|
Could it be that really no one knows how to do it?!
|
|
| Floor20 namejm | Posted 2006-09-10 23:03 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
Originally posted by fornever at 2006-9-9 04:45: Although it is possible to input in the CMD command line, it will still cause errors when used in batch processing. Confused. |
|
| Floor21 3742668 | Posted 2006-09-10 23:43 |
| 荣誉版主 Posts 718 Credits 2,013 | |
|
Well,Assigning | to a variable in batch is not impossible:
Regarding filtering characters, the following code is written casually, hoping to be helpful to friends in need: It seems that |, ||, &, &&, " can be filtered out. You can test and change it yourself. |
|
| Floor22 namejm | Posted 2006-09-11 00:42 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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. |
|
| Floor23 fornever | Posted 2006-09-11 09:06 |
| 初级用户 Posts 35 Credits 98 | |
Originally posted by namejm at 2006-9-10 23:03: 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 ] |
|
| Floor24 fornever | Posted 2006-09-11 09:45 |
| 初级用户 Posts 35 Credits 98 | |
|
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! |
|
| Floor25 fornever | Posted 2006-09-11 09:49 |
| 初级用户 Posts 35 Credits 98 | |
|
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?
|
|
| Floor26 9527 | Posted 2006-09-11 20:57 |
| 银牌会员 Posts 438 Credits 1,185 From 北京 | |
|
@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... |
|
| Floor27 zyyuser | Posted 2006-09-12 23:08 |
| 新手上路 Posts 1 Credits 2 | |
|
%Var:a=b%
Replace all occurrences of "a" with "b" in the environment variable Var |
|
| Floor28 namejm | Posted 2006-09-13 10:10 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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:
[ Last edited by namejm on 2006-9-13 at 10:11 ] |
|
| Floor29 fornever | Posted 2006-09-15 10:20 |
| 初级用户 Posts 35 Credits 98 | |
|
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! |
|
| Floor30 yangxixing | Posted 2007-11-22 23:13 |
| 初级用户 Posts 16 Credits 25 | |
| Prev 1 2 3 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |