在批

时间:2015-10-16 04:16:08

标签: batch-file for-loop

所以我想为一个基于回合的RPG创建打字文本(批量),你可以让一个人控制几乎所有与玩家互动的东西。 我想要它,所以当每个人都转过来时,它会显示每个人都做了什么,但是以戏剧性的方式。 现在我们中的一些人知道

for %%i in (h e l l o) do (set /p a=%%i<nul & ping 0.0.0.0 -n 2.5>nul 2>&1)

但是我希望有一个变量来做这件事 set /p Write=<Write.txt 所以我可以像for %%i in (%write%) do (set /p a=%%i<nul & ping 0.0.0.0 -n 2.5>nul 2>&1)这样的东西 使它一次写一个字母。

谢谢!

4 个答案:

答案 0 :(得分:1)

下面的批处理文件使用了我从this post借来的有趣技巧,它通过cmd.exe的/ U开关将Ascii(1字节)字符转换为Unicode 2字节字符(在两者之间插入一个零字节)字符),然后通过find命令分割各行中的零字节:

@echo off
setlocal

echo Hello, World!> Write.txt

for /F %%a in ('echo prompt $H^| cmd') do set "BS=%%a"

for /F "delims=" %%i in ('cmd /D /U /C type Write.txt ^| find /V ""') do (
   set /P "=X%BS%%%i" < NUL
   ping localhost -n 2 > NUL
)

编辑添加了新版本

我修改了以前的代码,以正确的方式显示几行;它还使用JScript的睡眠方法,以便在每个字符之间使用可变延迟间隔,这一点导致输出以更戏剧性的方式出现。

@if (@CodeSection == @Batch) @then


@echo off

(
echo Hello, World!
echo The World is ending!
) > Write.txt

for /F %%a in ('echo prompt $H^| cmd') do set "BS=%%a"

for /F "delims=" %%a in (Write.txt) do (
   for /F "delims=" %%i in ('cmd /D /U /C set /P "=%%a"^<NUL ^| find /V ""') do (
      set /P "=X%BS%%%i" < NUL
      REM ping localhost -n 3 > NUL
      cscript //nologo //E:JScript "%~F0"
   )
   echo/
)
goto :EOF


@end


// JScript section: wait a random number of milliseconds between 0 and 300
WScript.Sleep(300*Math.random());

答案 1 :(得分:1)

这是Ghost typer的一个版本,它使用一个变量,以一种人可能输入的方式在屏幕上显示文字。

@echo off
:: Ghost typer - variable version

:: shows text in a variable on the screen in a way that a person might type it.
:: Do not use " in the text to be printed, replace them with '

:: Adjust speed variable - higher numbers increase typing speed - default=3
set speed=6

:: backspace variable is used in the printing routine
for /f %%a in ('"prompt $H&for %%b in (1) do rem"') do set "BS=%%a"

set "line=Your dog was eaten by a red winged dragon!"
call :begin
set "line=Two sloths ate all your chocolate!"
call :begin
set "line=A genie appears, and grants you 4 wishes...."
call :begin
set "line=How many woodchucks does it take?"
call :begin


pause>nul
goto :EOF

:begin
set num=0
:type
call set "letter=%%line:~%num%,1%%"
set "delay=%random%%random%%random%%random%%random%%random%%random%"
set "delay=%delay:~-6%"
if not "%letter%"=="" set /p "=a%bs%%letter%" <nul

for /L %%b in (1,%speed%,%delay%) do rem
if "%letter%"=="" echo.&goto :EOF
set /a num+=1
goto :type

答案 2 :(得分:0)

要逐个打印任意字符串,可以使用以下内容:

@echo off

rem define string to print:
set "WRITE=Hello world!"

setlocal EnableDelayedExpansion

for /L %%I in (0,1,8191) do (
    set "CHAR=!WRITE:~%%I,1!"
    rem if "!WRITE:~%%I,2!"=="!CHAR! " set "CHAR=!CHAR! "
    if "!CHAR!"=="" goto :SKIP
    < nul set /P "DUMMY=!CHAR!"
    > nul 2>&1 ping 127.0.0.1 -n 2
)
:SKIP

endlocal

正如您将注意到的,WRITE中字符串中的 SPACE 未打印,因为set /P不会在提示字符串中打印任何(前导)空格。要解决此问题,请移除rem循环内if前面的for。实际上,这会预览下一个字符并将其附加到当前字符,以防它是一个空格,因为set /P会打印尾随空格。

答案 3 :(得分:0)

@echo off
setlocal enabledelayedexpansion

for /f %%A in ('"prompt $H &echo on &for %%B in (1) do rem"') do set BS=%%A

set /P string=<write.txt
call :StrLen "%string%" _len1

for /L %%A in (0,1,%_len1%) do (
   set "substring=!string:~%%A,1!"
   set /p "=.%BS%!substring!" <nul
   > nul 2>&1 ping 127.0.0.1 -n 1
)
echo.
pause
GOTO :EOF

REM Subroutine to return the length of a string variable
:StrLen [string] [len-var]
  set "_str=A%~1" & set "_len=0"
  for /L %%A in (12,-1,0) do (set /a "_len|=1<<%%A" & for %%B in (!_len!) do if "!_str:~%%B,1!"=="" set /a "_len&=~1<<%%A")
  set /a %~2=%_len%
  exit /b