批量打印特定文本行到文件 [英] Batch to Print specific lines of text to a file

查看:65
本文介绍了批量打印特定文本行到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先让我说我对此很陌生,然后我在该站点上找到了一些我拼凑的小代码.

Let me start off by saying I am very new to this, and what little code I have cobbled together I found on this site.

最后,我需要一批在运行时将在父目录中获取每个文件夹名称的批处理.并将其复制到名为label1,label2等的文本文件中.

In the end I need a batch that when ran will grab each folder name in a parent dir. and copy it to a text file named label1, label2, ect.

我首先从文本文件中的目录列表中拉线.我可以从这篇文章中使用Seth的代码来将最后一行回显到文件 Windows批处理文件以回显特定的行号

I started with pulling the lines from a directory list in a text file. I can get it to echo the last line to a file using Seth's code from this post Windows Batch file to echo a specific line number

我进行了一些修改以尝试将其放入循环中,但现在我什么也没得到.

I made some modifications to try to put it in a loop and now I get nothing out.

如果有人可以帮助我,将不胜感激.到目前为止,这是我的代码.

If anyone can help me it would be much appreciated. Here is my code so far.

    set /a "x=1"
    set /a "lines=91"
    :while1
        if %x% leq %lines% (
        for /f "tokens=*" %%a in ('findstr /n .* "Y:\Test\foldernametest.txt"') do (
      set "FullLine=%%a"
      for /f "tokens=1* delims=:" %%b in ("%%a") do (
        setlocal enabledelayedexpansion
        set "LineData=!FullLine:*:=!"
        if "%%b" equ "%1" echo(!LineData!
        echo title=!linedata! > Lable%x%.dat
        set /a "x= x+1"
        endlocal
        goto :while1
        )
    )

推荐答案

setlocal enabledelayedexpansion
set /a x=1
set /a lines=91
:while1
    if %x% leq %lines% (
    for /f "tokens=*" %%a in ('findstr /n .* "Y:\Test\foldernametest.txt"') do (
    for /f "tokens=1* delims=:" %%b in ("%%a") do (
      if "%%b" equ "%x%" (
        echo(%%c
        echo title=%%c > Lable%x%.dat
        set /a x= x+1
        goto while1
      )
    )
)
endlocal

我有理由相信这会奏效,

I'm reasonably sure this will work, as would

setlocal enabledelayedexpansion
set /a x=1
set /a lines=91
:while1
    if %x% leq %lines% (
    for /f "tokens=1* delims=:" %%a in ('findstr /n .* "Y:\Test\foldernametest.txt"') do if %%a==%x% (
    echo(%%b
    echo title=%%b > Lable%x%.dat
    set /a x= x+1
    goto while1
    )
)
endlocal

您的代码存在的问题是endlocal终止了setlocal,并且撤消了自setlocal以来发生的所有环境更改-环境恢复到setlocal时的状态被执行.

The issue with your code is that endlocal terminates a setlocal and all of the environment changes that have taken place since the setlocal are backed out - the environment is restored to what it was when the setlocal was executed.

其结果是,在您的代码中,您要递增x(变量的大名),然后在执行endlocal时取消增量.

The consequence is that with your code, you are incrementing x (a grand name for a variable) and then the increment is backed out when the endlocal is executed.

所以-将整个例程放在setlocal/endlocal括号中.这还有其他优点-例如,如果您在@echo off之后立即执行setlocal,那么当例程终止时,环境将返回其原始状态-随着越来越多的批处理,它不会累积更改(通常不添加变量)运行.

So - put the entire routine in a setlocal/endlocal bracket. This has other advanteages - like if you execute a setlocal immediately after @echo off, then when the routine terminates, the environment is returned to its original state - it does not accumulate changes (normally additions of variables) as more and more batches are run.

我进行的其他一些更改是修饰.

Some of the other changes I've made are cosmetic. the quotes in a set /a are superfluous and so is the colon in a goto (with the sole exception of goto :eof)

您遇到的另一个问题是%1(意思是例程的第一个参数"),您可能要说%x%".

Another problem you have was %1 (meaning "the first parameter to the routine") where you probably meant "%x%".

在第一个代码段中,将findstr的输出分配给%%a,内部的for将该findstr的部分分配给定界符之前的%%b,并将其分配给%%c之后的部分. .您显然想选择与%x%相等的行%%b,以便代码进行比较,如果相等,则将%%c(行的其余部分)和title=%%c输出到由Lable制成的文件和行号. (您拼写了错误的label);然后递增x并重试.

In the first code fragment, the output of the findstr is assigned to %%a and the inner for assigns that part of the findstr before the delimiter to %%b and that after to %%c. You evidently want to pick the line %%b equal to %x% so the code makes the comparison and if equal, outputs %%c (rest of line) and title=%%c to the file made from Lable and the line number. (You've spelled label incorrectly); then increments x and tries again.

第二段代码是第一段的简化.从文件中读取该行并编号,然后直接在冒号上分割; %%a获取数字,%%b该行的其余部分,因此,如果%%a与数字%x%相同,则我们要执行某些操作(无需引号,因为%%a是简单的数字字符串)和x也将是数字,因为它从未分配给包含分隔符的字符串).

The second piece of code is a simplification of the first. The line is read from the file and numbered, then split directly on the colon; %%a gets the number, %%b the rest of the line, so if %%a is the same as the number %x% then we want to do something (no quotes required, since %%a is a simple numeric string and x will also be numeric because it's never assigned to a string containing separators or empty).

要做的事情是从文件中回显行(在%%b中,增加行号并重新开始...

The thing-to-be-done is to echo the line from the file (in %%b, bump the line number and start again...

这篇关于批量打印特定文本行到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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