Question

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. Each text file contains only one line of text such as, "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. 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. 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. 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. 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. 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. Any ideas would help.

Was it helpful?

Solution 3

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"

OTHER TIPS

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

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. 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. (Remember %%A in a batch and %A when typed).

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. 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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top