好吧,伙计们,我基本上是想获取用户输入并将其输入到批处理中
我将使用退格键并回车,所以我无法使用选择
这就是我关注的原因此方法将字符捕获为 ascii
并使用这将 ascii 输出转换为字符
这里的问题是,当分开时它们都工作良好,但是当在同一个文件上时它们不起作用:/
这是我的完整批处理文件
@echo off
:: Load the charMacros if not already loaded. The macros are dependent on
:: the active code page, so if it has changed, then the macros are reloaded.
:: The macros are loaded prior to SETLOCAL so that the macros persist after
:: the script terminates
for /f "delims=" %%A in ('chcp') do if "%%A" neq "%#mapCodePage%" (set macrop=tst & goto chr)
:tst
:: Test the Str2Hex macro
set /p "=> Single Key Prompt? " <nul
PowerShell Exit($host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown').VirtualKeyCode);
set er=%errorlevel%
set "str=Hello world!"
%@Str2Hex% str hex
%@chr% %er% hw
echo asci=%er% char=%hw%
echo str=%str% hex=%hex%
pause
:chr
<!-- : Begin batch script
@echo off
:: charMacros.bat
::
:: This script installs macros that can be used to interconvert between
:: numeric extended ASCII codes and character values.
::
:: The script defines the following variables:
::
:: @Str2Hex - A macro used to convert a string into a string of hex digit
:: pairs representing the ASCII codes in the string.
::
:: @asc - A macro used to convert a character into the decimal ASCII code
::
:: @ascHex - A macro used to convert a character into the hex ASCII codde
::
:: @chr - A macro used to convert a numeric ASCII code into a character
::
:: #LF - A variable containing a line feed character
::
:: #CR - A variable containing a carriage return character
::
:: #charMap - A variable used by the @asc macro
::
:: #asciiMap - A variable used by the @chr macro
::
:: #mapCodePage - The CHCP setting at the time the maps were loaded
::
:: \n - used for specifiying the end of line in a macro definition
::
:: Originally developed and posted by Dave Benham (with help from DosTips users) at
:: http://www.dostips.com/forum/viewtopic.php?f=3&t=4284
if "!" == "" >&2 echo ERROR: Delayed expansion must be disabled when loading %~nx0&exit /b 1
:: Define a Carriage Return string, only useable as !#CR!
for /f %%a in ('copy /Z "%~dpf0" nul') do set "#CR=%%a"
:: Define a Line Feed (newline) string (normally only used as !#LF!)
set #LF=^
:: Above 2 blank lines are required - do not remove
:: Define a newline with line continuation
set ^"\n=^^^%#LF%%#LF%^%#LF%%#LF%^^"
:: Define character maps used to interconvert between extended ASCII codes
:: and characters.
set "#charMap="
for /f "delims=" %%A in ('cscript //nologo "%~f0?.wsf"') do (
if defined #charMap (set "#asciiMap=%%A") else set "#charMap= %%A"
)
for /f "delims=" %%A in ('chcp') do set "#mapCodePage=%%A"
:: %@Str2Hex% StrVar [RtnVar]
::
:: Converts the string within StrVar into a string of extended ASCII codes,
:: with each code represented as a pair of hexadecimal digits. The length of
:: the result will always be exactly twice the length of the original string.
::
:: Any character within the string that is not in the currently loaded code
:: page will be represented as 00.
::
:: The result is stored in RtnVar, or ECHOed if RtnVar is not specified.
::
:: The macro is safe to "call" regardless whether delayed expansion
:: is enabled or not.
::
:: StrVar = The name of a variable that contains the string
:: to be converted
::
:: RtnVar = The name of the variable used to store the result.
::
set @Str2Hex=for %%# in (1 2) do if %%#==2 (%\n%
for /f "eol= tokens=1,2 delims=, " %%a in ("!#args!") do (endlocal%\n%
setlocal enableDelayedExpansion%\n%
if defined %%~a (%\n%
set "str=!%%~a!"%\n%
set "s=!%%~a!"%\n%
set "len=0"%\n%
for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (%\n%
if "!s:~%%P,1!" neq "" (%\n%
set /a "len+=%%P"%\n%
set "s=!s:~%%P!"%\n%
)%\n%
)%\n%
set "rtn="%\n%
for /l %%N in (0 1 !len!) do (%\n%
set "chr=!str:~%%N,1!"%\n%
set "hex="%\n%
if "!chr!"=="=" set hex=3D%\n%
if "!chr!"=="^!" set hex=21%\n%
if "!chr!"=="!#lf!" set hex=0A%\n%
if not defined hex for /f delims^^=^^ eol^^= %%c in ("!chr!!#CR!") do (%\n%
set "test=!#asciiMap:*#%%c=!"%\n%
if not "%%c"=="!test:~0,1!" set "test=!test:*#%%c=!"%\n%
if "%%c"=="!test:~-0,1!" (set "hex=!test:~1,2!") else set "hex=00"%\n%
)%\n%
set "rtn=!rtn!!hex!"%\n%
)%\n%
for %%v in (!rtn!) do endlocal^&if "%%~b" neq "" (set "%%~b=%%v") else echo(%%v%\n%
) else endlocal%\n%
set "#args=")) else setlocal enableDelayedExpansion^&set #args=,
:: %@asc% StrVar Position [RtnVar]
::
:: Converts a character into the extended ASCII code value.
:: The result is stored in RtnVar, or ECHOed if RtnVar is not specified.
:: A value of -1 is returned if the character is not in the currently loaded
:: code page. The macro is safe to "call" regardless whether delayed expansion
:: is enabled or not.
::
:: StrVar = The name of a variable that contains the character
:: to be converted
::
:: Position = The position of the character within the string
:: to be converted. 0 based.
::
:: RtnVar = The name of the variable used to store the result.
::
set @asc=for %%# in (1 2) do if %%#==2 (%\n%
for /f "eol= tokens=1-3 delims=, " %%a in ("!#args!") do (endlocal%\n%
setlocal enableDelayedExpansion%\n%
if defined %%~a (%\n%
set "str=!%%~a!"%\n%
set /a "n=%%~b" 2^>nul%\n%
for %%N in (!n!) do set "chr=!str:~%%N,1!"%\n%
if defined chr (%\n%
set "rtn="%\n%
if "!chr!"=="=" set rtn=61%\n%
if "!chr!"=="^!" set rtn=33%\n%
if "!chr!"=="!#lf!" set rtn=10%\n%
if not defined rtn for /f delims^^=^^ eol^^= %%c in ("!chr!!#CR!") do (%\n%
set "test=!#asciiMap:*#%%c=!"%\n%
if not "%%c"=="!test:~0,1!" set "test=!test:*#%%c=!"%\n%
if "%%c"=="!test:~-0,1!" (set /a "rtn=0x!test:~1,2!") else set "rtn=-1"%\n%
)%\n%
)%\n%
for %%v in (!rtn!) do endlocal^&if "%%~c" neq "" (set "%%~c=%%v") else echo(%%v%\n%
) else endlocal%\n%
set "#args=")) else setlocal enableDelayedExpansion^&set #args=,
:: %@chr% AsciiCode [RtnVar]
::
:: Converts an extended ASCII code into the corresponding character.
:: The result is stored in RtnVar, or ECHOed if RtnVar is not specified.
:: The macro supports value 1 - 255. The value 0 is not supported.
:: The macro is safe to "call" regardless whether delayed expansion is
:: enabled or not.
::
:: AsciiCode - Any value from 1 to 255. The value can be expressed as any
:: numeric expression supported by SET /A.
::
:: RtnVar - The name of the variable used to store the result
::
set @chr=for %%# in (1 2) do if %%#==2 (%\n%
for /f "eol= tokens=1,2 delims=, " %%a in ("!#args!") do (endlocal%\n%
setlocal%\n%
set "NotDelayed=!"%\n%
setlocal EnableDelayedExpansion%\n%
set "n=0"%\n%
set /a "n=%%~a"%\n%
if !n! gtr 255 set "n=0"%\n%
if !n! gtr 0 (%\n%
if !n! equ 10 (%\n%
for %%C in ("!#LF!") do (%\n%
endlocal^&endlocal%\n%
if "%%~b" neq "" (set "%%~b=%%~C") else echo(%%~C%\n%
)%\n%
) else (%\n%
for %%N in (!n!) do set "c=!#charMap:~%%N,1!"%\n%
if "!c!" equ "^!" if not defined NotDelayed set "c=^^^!"%\n%
for /f delims^^=^^ eol^^= %%C in ("!c!!#CR!") do (%\n%
endlocal^&endlocal%\n%
if "%%~b" neq "" (set "%%~b=%%C") else echo(%%C%\n%
)%\n%
)%\n%
) else endlocal^&endlocal%\n%
set "#args=")) else setlocal enableDelayedExpansion^&set #args=,
:: %@ascHex% StrVar Position [RtnVar]
::
:: Converts a character into the extended ASCII code hex value.
:: The result is stored in RtnVar, or ECHOed if RtnVar is not specified.
:: A value of -1 is returned if the character is not in the currently loaded
:: code page. The macro is safe to "call" regardless whether delayed expansion
:: is enabled or not.
::
:: StrVar = The name of a variable that contains the character
:: to be converted
::
:: Position = The position of the character within the string
:: to be converted. 0 based.
::
:: RtnVar = The name of the variable used to store the result.
::
set @ascHex=for %%# in (1 2) do if %%#==2 (%\n%
for /f "eol= tokens=1-3 delims=, " %%a in ("!#args!") do (endlocal%\n%
setlocal enableDelayedExpansion%\n%
if defined %%~a (%\n%
set "str=!%%~a!"%\n%
set /a "n=%%~b" 2^>nul%\n%
for %%N in (!n!) do set "chr=!str:~%%N,1!"%\n%
if defined chr (%\n%
set "rtn="%\n%
if "!chr!"=="=" set rtn=3D%\n%
if "!chr!"=="^!" set rtn=21%\n%
if "!chr!"=="!#lf!" set rtn=0A%\n%
if not defined rtn for /f delims^^=^^ eol^^= %%c in ("!chr!!#CR!") do (%\n%
set "test=!#asciiMap:*#%%c=!"%\n%
if not "%%c"=="!test:~0,1!" set "test=!test:*#%%c=!"%\n%
if "%%c"=="!test:~-0,1!" (set "rtn=!test:~1,2!") else set "rtn=-1"%\n%
)%\n%
)%\n%
for %%v in (!rtn!) do endlocal^&if "%%~c" neq "" (set "%%~c=%%v") else echo(%%v%\n%
) else endlocal%\n%
set "#args=")) else setlocal enableDelayedExpansion^&set #args=,
cls
goto %macrop%
----- Begin wsf script --->
<job><script language="VBScript">
for i=1 to 255
if i=10 then WScript.Stdout.Write " " else WScript.Stdout.Write chr(i)
next
WScript.Stdout.Write chr(10)+"#"
for i=1 to 255
if i<>10 then WScript.Stdout.Write chr(i)+chr(i)+right("0"+hex(i),2)+"#"
next
</script></job>
在第 6 行如果我将 goto chr 更改为 call charmacros.bat 它可以工作
在第 9 行,如果我删除它<nul
可以工作,但由于它需要你按回车键,它只会检测回车键
那么代码在哪里中断?/为什么<nul
将 ascii 转换为 char 会中断?
答案1
原始 charMacros.bat 脚本是混合批处理/WSF(VBS)脚本,旨在作为独立脚本。
您当然可以将它与其他代码结合起来,但必须遵循混合批处理/WSF 脚本的规则。
代码中的重定向使 WSF 解析器感到困惑。
将下面这行移到顶部,问题就解决了:
<!-- : Begin batch script
你应该读Liviu 对批处理/WSF 混合技术的原始描述和/或阅读我的批处理/ WSF 解决方案是否可以在批处理文件中嵌入并执行 VBScript 而不使用临时文件?