简体   繁体   English

使用批处理将某个文件夹中文本文件中包含的所有数据编译为一个文件

[英]compile all data contained in text files in a certain folder into one file with batch scipt

I am having trouble trying to get one of my batch programs to work properly. 我在尝试使我的批处理程序之一正常工作时遇到麻烦。 There is a certain point in the program where I need it to take only the first line of the text files in a folder and compile them into a single text file. 程序中有一个特定的点,我需要它仅将文件夹中文本文件的第一行提取为一个文本文件。

Example: c:\\examplefolder contains 5 text files named- atg.txt, brady.txt, cobra.txt, delta.txt , and exos.txt. 示例: c:\\examplefolder包含5个文本文件,分别名为atg.txt, brady.txt, cobra.txt, delta.txt和exos.txt。 Each text file contains only one line of text such as, "ATG......7" . 每个文本文件仅包含一行文本,例如"ATG......7" I need to compile all of the contents of these text files into a single text file. 我需要将这些文本文件的所有内容编译为一个文本文件。

Background info: I have tried a FOR line given in a similar question, but it gives me REPEATS. 背景信息:我已经尝试过在类似问题中给出的FOR行,但是它使我重复。 It tends to duplicate text files, which I obviously don't want. 它倾向于复制文本文件,这显然是我不想要的。 I have also tried: TYPE c:\\examplefolder\\*.txt >> c:\\examplefolder\\output.txt, but this gives duplicates as well. 我也尝试过:TYPE c:\\ examplefolder \\ *。txt >> c:\\ examplefolder \\ output.txt,但这也提供了重复项。 It appears to happen when the names of the text files tend to be chronological. 当文本文件的名称倾向于按时间顺序排列时,似乎会发生这种情况。 So if I have Atg.txt and Brady.txt in the same folder and use one of the commands above, it duplicates the contents of the files. 因此,如果我在同一文件夹中有Atg.txt和Brady.txt,并使用上面的命令之一,它将复制文件的内容。 It appears to pass over the files twice. 它似乎两次通过文件。

My program example: 我的程序示例:

REM ASKING FOR USER INPUT AND CREATING TEXT FILES...

Set /p brandname=Enter brand name:

Set /p count=Enter the current count for %brandname%:

Echo %brandname%.....%count% >> c:\examplefolder\%brandname%.txt


:viewingoutputfile

REM DELETING OUTPUT FILE AND CREATING A NEW ONE...

If exist c:\examplefolder\output.txt del /q c:\examplefolder\output.txt

FOR %%I in (c:\examplefolder\\*.txt) do echo %%I >> c:\examplefolder\output.txt

Type c:\output.txt

Pause

Once the folder builds up it's contents over time you can imagine that there are quite a number of files with different names. 一旦建立了文件夹,它的内容就会随着时间的流逝而变化,您可以想象到有很多文件的名称不同。 Let's just say I have only 2 text files named atg.txt and brady.txt. 假设我只有2个名为atg.txt和brady.txt的文本文件。 My output file contains this: 我的输出文件包含以下内容:

Atg.....2

Brady.....4

Atg......2

Brady.....4

Now, obviously I want it to read: 现在,显然我希望它显示为:

Atg.....2

Brady.....4

But that's not the case. 但是事实并非如此。 If someone can find a solution to this predicament I would greatly appreciate it. 如果有人能找到解决这种困境的办法,我将不胜感激。 By the way I even tried adding %random% in front of the file name so that the name of the file would be: 3241atg.txt instead of just atg.txt, but this gets the same duplicating problem. 顺便说一句,我什至尝试在文件名前添加%random%,以便文件名为:3241atg.txt,而不只是atg.txt,但这引起了同样的复制问题。 I'm completely stumped. 我完全迷住了。

By the way; 顺便说说; if someone can provide me with a batch script that works without duplication that would be great but, if you know a vbs script that could get the job done, I could just start it from the batch program. 如果有人可以为我提供一个无需重复就可以工作的批处理脚本,那会很好,但是,如果您知道可以完成工作的vbs脚本,那么我可以从批处理程序中启动它。 Any ideas would help. 任何想法都会有所帮助。

Try this 尝试这个

@echo off
setlocal EnableDelayedExpansion
for %%a in (*.txt) do (
set /p $line1=<"%%a"
echo !$line1!>>output.tmp
)
ren output.tmp output.txt

You will have all first line off the .txt files in your folder in the file output.txt 您将在文件output.txt中的文件夹中的.txt文件中output.txt所有第一行。

Put your output file in a seperate directory. 将输出文件放在单独的目录中。 Everything you do is causing cmd to create and delete temporary files over and over in making your output file. 您所做的一切都会导致cmd在制作输出文件时一遍又一遍地创建和删除临时文件。 Move all that away from the files you are processing. 将所有内容从正在处理的文件移开。

Or build a list of what do do, so you have a snapshot of files to be processed, then do the list. 或建立一个做什么的清单,以便您有要处理的文件的快照,然后执行清单。 Use dir /b. 使用目录/ b。 (Remember %%A in a batch and %A when typed). (记住批次中的%% A,键入时记住%A)。

for /f %A in ('dir /b *.txt') do echo type %A ^>^>output.txt>>output.bat
output.bat

I'll post a VBScript solution, just because I see you've tagged it. 我将发布一个VBScript解决方案,只是因为我看到您已对其进行了标记。 Here's the input side: 这是输入端:

WScript.StdOut.Write "Enter brand name: "
brandname = WScript.StdIn.ReadLine()

WScript.StdOut.Write "Enter the current count for " & brandname & ": "
count = WScript.StdIn.ReadLine()

With CreateObject("Scripting.FileSystemObject").CreateTextFile("c:\examplefolder\" & brandname & ".txt", True)
    .WriteLine brandname & "....." & count
    .Close
End With

And here's how you could combine them into a single file: 这是将它们组合到单个文件中的方法:

With CreateObject("Scripting.FileSystemObject")
    For Each File In .GetFolder("c:\examplefolder").Files
        s = s & .OpenTextFile(File.Path).ReadAll()
    Next
    .CreateTextFile("c:\examplefolder\output.txt", True).Write(s).Close
End With

The reason you got duplicates is because the output file has the same extension and was also being processed. 重复的原因是因为输出文件具有相同的扩展名,并且也正在处理中。

This will work: 这将起作用:

TYPE "c:\examplefolder\*.txt" >> "c:\examplefolder\output.tmp"
ren "c:\examplefolder\output.tmp" "output.txt"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 批处理文件 Scipt 在 if 语句中执行一个 go 中的所有指令 - Batch file Scipt Executing all instructions in one go in if statement 创建一个批处理文件,该文件读取当前目录中包含的所有文本文件 - Create a batch file that reads all the text files contained in current directory 创建批处理文件以将根文件夹中包含的所有内容(包括文件夹和文件)复制到另一个文件夹 - Create batch file for copy all the contained in root folder (including folder and files) to another folder 批处理文件,删除文件夹中所有已存在10天以上的所有文本文件(某些文件除外) - Batch file to delete all text files in a folder over 10 days old except certain ones 通过批处理脚本在一个文件夹中执行所有文本文件 - Executing all text files in one folder by a batch script 批处理脚本可将文件夹中的所有文件列出为文本文件,而无需重复 - batch script to list all the files in a folder to a text file without duplicating BATCH&gt;运行.exe文件,其中的路径包含在文本文件中 - BATCH> run .exe files which paths are contained in a text file 批处理文件以删除文本列表中包含相对名称的文件? - Batch file to delete files with relative name contained in text list? 如何使用Batch Scipt以特定顺序在文本文件中打印输出 - How to print the output in text file in specific order using Batch scipt 用于将不同文件夹中的多个文本文件合并到另一个不同文件夹中的一个文本文件的循环批处理文件 - Looping batch file for Merging several text files from different folders to one text file in another different folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM