需要批处理文件分开;在文本文件中移动字符和数字

时间:2013-02-01 14:59:51

标签: batch-file

首先发布在这里,因为通常我只能使用谷歌找到我的解决方案。但这一次,不,不。首先,操作系统是Windows 7,它必须在批处理文件中完成 - 但是只要没有需要安装的软件,其他脚本就没有问题。

我得到的文件看起来总是这样,但价值永远不会相同。

Example\Example\Example\examplefile.ext
1 0
4 16 7 5 3 9
46215
0
20689
20656
20667
6125
33585
46213
10256
20661
40189
21650
49792
31776
2101
(blank line)
(blank line)

最后2行“(空白行)”,事实上是 - 空行

现在,我需要输出文件的样子是这样的:

<A:Example\Example\Example\examplefile.ext>
<B:1 0>
<C:4>
<D:16>
<E:7>
<F:5>
<G:3>
<H:9>
<I:46215>
<J:0>
<K:20689>
<L:20656>
<M:20667>
<N:6125>
<O:33585>
<P:46213>
<Q:10256>
<R:20661>
<S:40189>
<T:21650>
<U:49792>
<V:31776>
<X:2101>

那就是我需要输出的方式。所有帮助都会非常适用。

1 个答案:

答案 0 :(得分:1)

这将做你想要的。

备注

  1. 这仅支持最多包含26个项目的文件。 (A-Z)
  2. 第二行永远不会分开。
  3. 脚本

    @echo off
    setlocal EnableExtensions EnableDelayedExpansion
    >output.txt <nul set /p "="
    set "A=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    for /f "tokens=*" %%A in (Example\Example\Example\examplefile.ext) do (
        if "!A:~0,1!" equ "B" (
            echo ^<B:%%A^>>>output.txt
            set "A=!A:~1!"
        ) else (
            for %%B in (%%A) do (
                echo ^<!A:~0,1!:%%B^>>>output.txt
                set "A=!A:~1!"
            )
        )
    )
    endlocal