批次:从插入.txt文件行到一个.txt文件 [英] Batch: insert lines from a .txt file into a .txt file

查看:135
本文介绍了批次:从插入.txt文件行到一个.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是完全新的批处理文件,并已通过简单的命令摆弄周围。我已经想通了如何移动文本到另一个文件中,找到一条线等,但我希望能够以文本的几行添加到已有的文本文件。以下是我迄今为止:

  @ECHO OFF
CD C:\\ Documents和Settings \\ SLZ1FH \\桌面\\新建文件夹
FOR / F令牌= *%% A IN(Examples.txt)DO(
  ECHO %%一
  IF %% A ==EX3 3(
    TYPE Line_to_add.txt>> Examples.txt
  )

如果Examples.txt包括:


  • 练习1 1

  • 练习2 2

  • EX3 3

  • EX4 4

  • EX5 5

和Line_to_add.txt包括:


  • 这是一个行

  • 这是另一条线路只是踢!

我想输出是:


  • 练习1 1

  • 练习2 2

  • EX3 3

  • 这是一个行

  • 这是另一条线路只是踢!

  • EX4 4

  • EX5 5

TIA:)

解决方案

  @ECHO OFF
FOR / F令牌= *%% A IN(Examples.txt)DO(
  ECHO %%一
  IF%% AEQUEX3(
    TYPE Line_to_add.txt
  )
)GT;> TEMP.TXT
移动/ Y TEMP.TXT Examples.txt


解决方案

您从来不屑于问一个问题,但我可以看到你的code一些问题。

1),因为作为字符串的一部分要比较的左括号时处理你的IF语句被打破了。您的括号之前,您必须至少有一个空格。

2)您的比较将永远不会找到一个匹配,因为引号包含的比较。你的价值观有空格,因此比较的双方应在引号括起来。

3)您正试图插入到现有文件中,无法正常工作。您code只会追加文本文件的末尾。您必须编写整个期望的结果到一个新的临时文件,然后将临时文件移动到原来的名称。

首先,我会忽略该文件的更新,并展示如何得到正确的输出到屏幕上。

  @ECHO OFF
FOR / F令牌= *%% A IN(Examples.txt)DO(
  ECHO %%一
  IF%% AEQUEX3 3(
    TYPE Line_to_add.txt
  )

它是那么简单,直接输出到一个文件,将文件移动到所需的名称。

  @ECHO OFF

  FOR / F令牌= *%% A IN(Examples.txt)DO(
    ECHO %%一
    IF%% AEQUEX3 3(
      TYPE Line_to_add.txt
    )
  )
)> TEMP.TXT
移动/ Y TEMP.TXT Examples.txt

学习批语法和技术,良好的资源:

1)每个命令都有联机帮助。只需将命令名称后添加 / 选项。例如, DIR /?

2),这里有一些网站具有良好的信息

http://www.dostips.com/

http://judago.webs.com/

http://www.robvanderwoude.com/batchfiles.php

I am completely new to Batch files and have been tinkering around with simple commands. I have figured out how to move text to another file, find a line etc., but I want to be able to add a few lines of text into an already existing text file. Here is what I have so far:

@ECHO OFF
CD C:\Documents and Settings\SLZ1FH\Desktop\New Folder
FOR /F "tokens=*" %%A IN (Examples.txt) DO (
  ECHO %%A
  IF %%A=="Ex3 3"(
    TYPE Line_to_add.txt >> Examples.txt
  )
)

if Examples.txt contains:

  • Ex1 1
  • Ex2 2
  • Ex3 3
  • Ex4 4
  • Ex5 5

and Line_to_add.txt contains:

  • This is a line
  • This is another line just for kicks!

I would like the output to be:

  • Ex1 1
  • Ex2 2
  • Ex3 3
  • This is a line
  • This is another line just for kicks!
  • Ex4 4
  • Ex5 5

tia :)

Solution

@ECHO OFF
FOR /F "tokens=*" %%A IN (Examples.txt) DO (
  ECHO %%A
  IF "%%A" EQU "Ex3" (
    TYPE Line_to_add.txt
  )
) >> temp.txt
move /y temp.txt Examples.txt

解决方案

You never bothered to ask a question, but I can see a few problems with your code.

1) Your IF statement is broken because the left paren is treated as part of the string to be compared. You must have at least one space before your paren.

2) Your comparison will never find a match because the quotes are included in the comparison. Your values have spaces, so both sides of the comparison should be enclosed in quotes.

3) You are attempting to insert into the existing file, which cannot work. Your code would simply append the text to the end of the file. You must write the entire desired result to a new temp file, and then move the temp file to the original name.

First I will ignore the update of the file and show how to get the correct output to the screen.

@ECHO OFF
FOR /F "tokens=*" %%A IN (Examples.txt) DO (
  ECHO %%A
  IF "%%A" EQU "Ex3 3" (
    TYPE Line_to_add.txt
  )
)

It is then simple to direct the output to a file and move the file to the desired name.

@ECHO OFF
(
  FOR /F "tokens=*" %%A IN (Examples.txt) DO (
    ECHO %%A
    IF "%%A" EQU "Ex3 3" (
      TYPE Line_to_add.txt
    )
  )
) >temp.txt
move /y temp.txt Examples.txt

Good resources for learning batch syntax and techniques:

1) Every command has online help. Simply add the /? option after the command name. For example, DIR /?

2) Here are some sites with good info

http://www.dostips.com/
http://judago.webs.com/
http://www.robvanderwoude.com/batchfiles.php

这篇关于批次:从插入.txt文件行到一个.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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