我正在编写一个相当复杂的批处理文件,用于创建 D&D 角色。其中一部分涉及显示长句子,在指定的窗口宽度之后,程序会将单词分开并将切掉的部分添加到下一行。有没有办法让它将要拆分的单词推到下一行?
最佳答案
下面的批处理文件是一个基本的文字换行程序。
@echo off
setlocal EnableDelayedExpansion
rem Get the window width
for /F "skip=4 tokens=2 delims=:" %%a in ('mode con') do set /A width=%%a-1 & goto continue
:continue
rem Read the file given by first param and show its contents with no word split
set "output="
rem For each line in input file
for /F "delims=" %%a in (%1) do (
rem For each word in input line
for %%b in (%%a) do (
rem Add the new word
set "newOutput=!output! %%b"
rem If new word don't exceed window width
if "!newOutput:~%width%,1!" equ "" (
rem Keep it
set "output=!newOutput!"
) else (
rem Show the output before the new word
echo !output!
rem and store the new word
set "output=%%b"
)
)
)
rem Show the last output, if any
if defined output echo !output!
您可以修改此批处理文件以满足您的特定需求。请注意,此程序可能会遇到特殊批处理字符的问题,但这些问题可能会得到解决。
关于windows - 在 Batch 中,我可以让一行显示的文本在换行时不拆分单词吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20943733/