Windows 批处理将特殊字符视为输入错误
我希望所有非数字字符都被视为输入错误。字母字符是经过深思熟虑的,但不是某些特殊字符,如冒号(和其他字符) 你知道如何做到这一点吗Windows 批处理将特殊字符视为输入错误,windows,for-loop,batch-file,input,cmd,Windows,For Loop,Batch File,Input,Cmd,我希望所有非数字字符都被视为输入错误。字母字符是经过深思熟虑的,但不是某些特殊字符,如冒号(和其他字符) 你知道如何做到这一点吗 @echo off setlocal EnableDelayedExpansion set /a UserProfileNo=0 FOR /F "delims=" %%I IN ('dir /a:d /b "%SystemDrive%\Users\"') do ( set "UserProfileNam
@echo off
setlocal EnableDelayedExpansion
set /a UserProfileNo=0
FOR /F "delims=" %%I IN ('dir /a:d /b "%SystemDrive%\Users\"') do (
set "UserProfileName=%%~I"
set /a UserProfileNo+=1
set "UserProfileName[!UserProfileNo!]=!UserProfileName!"
)
FOR /L %%k IN (1, 1, %UserProfileNo%) DO echo %%~k !UserProfileName[%%~k]!
set /p choice=Enter a Profile Number:
FOR %%f IN (%choice%) DO if "!UserProfileName[%%~f]!" == "" (
echo Error in the input
goto end
)
echo NO Error in the input
:end
pause
exit
谢谢您可以使用findstr regex限制字符:
@Echo关闭
呼叫:输入rv[1]0123456789“输入号码”
调用:输入rv[2]a-zA-Z“输入字母数字字符串”
调用:输入rv[3]“a-zA-Z”“输入字母句子”
设置rv[
后藤:Eof
:输入[ReturnVar][CharacterString][prompt]
Setlocal EnableExtensions EnableDelayedExpansion
:重试
设置“输入=”
设置/P“输入=%~3:”
如果“!input!”==”转到:重试
Echo(“!input!”|%\uuuu APPDIR\uuuu%findstr.exe/RXC:“[\”][%~2]*[\”]”>nul | |转到:重试
Endlocal&设置“%~1=%input%”
退出/b0
为了执行此任务,我提供了的扩展
以下代码将列出所有标准本地用户,并允许您根据其数字索引从列表中选择一个。只接受有效的索引号(无其他普通字符),包括!
,,,%
,^
,&
,)
,=
,|
,
使用此方法,它不仅将输出限制为位于硬编码路径%SystemDrive%\Users
中的用户配置文件,也不依赖与帐户持有人姓名匹配的配置文件的目录名(如果用户更改其帐户名,则不会重命名其配置文件目录以反映该更改)
最后两行仅用于演示目的,只有在测试之后,您才能用自己的代码替换它们。它们的包含是为了演示有效的输入条目将创建两个新的局部变量,%User[Name]
,(输入索引号的%UserName%
)和%User[Profile]
,(输入索引号的%UserProfile%
)
请注意:上面的代码只能按照上面的代码运行和测试,没有遗漏、添加或修改。如果它不能按预期工作,在请求支持之前,我将接受使用%SystemRoot%\System32\
更改%\uuuuuuu AppDir%
的所有实例。谢谢。很遗憾,您的代码不排除字符!
和;
@A.Joson这两个问题很容易解决。@对代码进行修改,通过修改子字符串来删除这些字符。将Findstr替换添加到回答中遗憾的是,您的上一个代码不排除字符^
和&
@Echo Off&SetLocal EnableExtensions DisableDelayedExpansion
Set "i=0"&For /F "Delims==" %%G In ('"(Set User[) 2>NUL"')Do Set "%%G="
For /F "Delims==" %%G In ('%__AppDir__%wbem\WMIC.exe OS Call /?
^| %__AppDir__%find.exe "=="') Do Set "#=%%G"
For /F Tokens^=4Delims^=^" %%G In ('%__AppDir__%wbem\WMIC.exe UserAccount Where
"LocalAccount='TRUE'" Assoc:List /ResultRole:SID 2^>NUL'
)Do For /F Tokens^=1* %%H In ('%__AppDir__%wbem\WMIC.exe UserAccount Where
"Name='%%G'" Get SID /Value 2^>NUL^|%__AppDir__%find.exe "="'
)Do For %%I In (%%H)Do For /F "Tokens=1*Delims==" %%J In (
'%__AppDir__%wbem\WMIC.exe Path Win32_UserProfile Where
"SID='%%I' And Special!='TRUE' And LocalPath Is Not Null" Get LocalPath /Value
2^>NUL^|%__AppDir__%find.exe "="')Do For /F "Tokens=*" %%L In ("%%K")Do (
Set /A i+=1&SetLocal EnableDelayedExpansion&Echo !i!.%#:~-1%%%G
For %%M In (!i!)Do EndLocal&Set "User[%%M]Name=%%G"&Set "User[%%M]Path=%%L")
If Not Defined User[1]Name (Echo No standard users found, Press a key to end.
GoTo :EOF)
:Pick
Echo(&Set "#="
Set /P "#=Enter the number for your chosen Account Name>"
Set "#=%#:"=%"
(Set User[)|%__AppDir__%findstr.exe /BL "User[%#%]Name=">NUL||GoTo Pick
SetLocal EnableDelayedExpansion&For /F UseBackTokens^=1^,3Delims^=^" %%G In (
'"!User[%#%]Name!" "!User[%#%]Path!"')Do (EndLocal&Set "User[Name]=%%G"
Set "User[Profile]=%%H"&Set "#=")
(For /L %%G In (1,1,%i%)Do For %%H In (Name Path)Do Set User[%%G]%%H=)&Set "i="
Echo(&Echo You Selected %User[Name]% - [profile location is %User[Profile]%]
%__AppDir__%timeout.exe /T 5 /NoBreak>NUL