使用 <Nul set/p 保留空格 [英] Preserve spaces with <Nul set /p

查看:59
本文介绍了使用 <Nul set/p 保留空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建打字机效果,但 SET/P 正在剥离空格,因为我使用的是 win7.我还缺少其他方法或技巧吗?

I'm trying to create a typewriter effect but SET /P is stripping the spaces since I'm on win7. Is there another way or a trick I'm missing?

这是我到目前为止想出的代码:

Here is the code I've come up with so far:

@echo off
setlocal enabledelayedexpansion


set text=This is a test
call :strlen len text
for /l %%b in (0,1,%len%) do (
  set /a T=!random! %% 3
  ping -n !T! 127.0.0.1>nul
  <nul set /p=!text:~%%b,1!
)
exit /b

:strlen <resultVar> <stringVar>
setlocal EnableDelayedExpansion
set "s=!%~2!#" &    set "len=0"
for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if "!s:~%%P,1!" NEQ "" ( set /a "len+=%%P" & set "s=!s:~%%P!" ))
endlocal &set "%~1=%len%"
exit /b

推荐答案

如果输出严格针对屏幕,并且从不重定向到文件,那么有一个使用退格字符的简单解决方案.使用一些可打印字符作为每个 SET/P 输出的前缀,然后是退格符,然后是您想要的字符串.

If output is strictly for the screen, and never redirected to a file, then there is a simple solution using a backspace character. Prefix each SET /P output with some printable character, followed by a backspace, followed by your desired string.

@echo off
setlocal enabledelayedexpansion

:: Define BS to contain backspace character
for /f %%A in ('"prompt $H&for %%B in (1) do rem"') do set "BS=%%A"

set text=This is a test
call :strlen len text
for /l %%b in (0,1,%len%) do (
  set /a T=!random! %% 3
  ping -n !T! 127.0.0.1>nul
  <nul set /p=.!BS!!text:~%%b,1!
)
exit /b

:strlen <resultVar> <stringVar>
setlocal EnableDelayedExpansion
set "s=!%~2!#" &    set "len=0"
for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if "!s:~%%P,1!" NEQ "" ( set /a "len+=%%P" & set "s=!s:~%%P!" ))
endlocal &set "%~1=%len%"
exit /b

上述问题是点和退格字符将出现在任何重定向到文件的输出中.另一个可能的问题是,如果光标位于该行的最后一个字符位置,则该技术会失败,因为在点之后会自动发出换行符,然后退格无法返回到前一行.

The problem with the above is the dot and backspace characters will appear in any output that is redirected to a file. Another possible problem is the technique fails if the cursor is at the last character position on the line because a linefeed will automatically be issued after the dot, and then the backspace cannot back up to the prior line.

如果您必须将输出重定向到一个文件,或者如果您想要一个适用于所有情况的强大解决方案,那么 jeb 在 输出没有换行符的文本,即使有前导空格或 =.

If you must redirect output to a file, or if you want a robust solution that works in all cases, then jeb has a very clever solution at Output text without linefeed, even with leading space or =.

这篇关于使用 &lt;Nul set/p 保留空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆