windows - 在 Batch 中,我可以让一行显示的文本在换行时不拆分单词吗?

标签 windows batch-file

我正在编写一个相当复杂的批处理文件,用于创建 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/

相关文章:

windows - Windows XP 上的不可移动文件

windows - Windows 上 Haskell 的彩色外壳?

java - 在处理中找不到 android 模式

javascript - 如果父窗口关闭,如何关闭子窗口?

windows - 使用 32 位进程中的 64 位 COM 对象

batch-file - 如何使用cmd/批处理文件删除目录中名为x的所有文件夹

git - Msys Git merge 工具命令选项问题

list - bat 文件,用于创建文件列表以及与 bat 文件位于同一目录中的路径

java - 我如何将这个 bat 文件转换为 linux 加载 java 程序的 shell 脚本

batch-file - "&&"在这个批处理文件中做了什么?