Windows 10 Batch: Concatenate and append contents of a file to a new file.

Pat Malone

New Member
Joined
Jun 6, 2019
Messages
8
One folder has many files that all contain some notes for a particular name.
The names are already in a txt document one line after another that I plan to edit into a batch program.
Tabbed over from the names is the filename that contains the info needing to be appended.


Example:

{Larry) ACX.txt
{Susan} GRB.txt
(Greg} KIT.txt
etc.........

The format of the first couple of lines is all I am needing to understand. I have about 2,000 lines that will append.
Caveats:
If it is possible, I would like to append the entire content of the txt file onto one line of the new txt file.
Additional Question:
The files that will be concatenated actually have a different extension. Do I have to rename all of the 2,000 files, or will the batch program be able to read them with a different extension? (This is not that important considering there are many scripts available to rename all the files.)

Thank you for your time and consideration to everyone in the forum, and especially anyone who can help....=)
 


Attachments

Solution
Assuming your note text files are all in the same place something like this would work in Powershell.

38101


Code:
$BaseDirectory = "C:\Users\Justin\Desktop\TestDirectory"
$NotesDirectory = "\Notes"

$CombinedNotesFile = "\CombinedNotes.txt"
$NoteList = "\List.txt"

$NoteFiles = Get-Content -Path "$($BaseDirectory)$($NoteList)"

foreach($NoteFile in $NoteFiles)
{
    $NoteContent = Get-Content -Path "$($BaseDirectory)$($NotesDirectory)\$NoteFile"
    $NoteName = $NoteFile.Replace(".txt"," - ")
    Out-File -InputObject $("$($NoteName)$NoteContent") -FilePath "$($BaseDirectory)$CombinedNotesFile" -Append
}
You may need to elaborate more on what you're trying to accomplish.
 


Ok. I can make it simpler and then elaborate from there.
I have a text document that lists the filenames of a bunch of other text documents.

Larry.txt
Greg.txt
Mary.txt
etc.

Within Larry.txt is a note: (Larry's dog bites.)
Within Greg.txt is a note: (Greg's mailbox is green.)
Within Mary.txt is a note: (Mary's house is yellow.)

What should I use to concatenate all the notes from the individual files into a single txt file that has all the notes...i.e.

Larry's dog bites.
Greg's mailbox is green.
Mary's house is yellow.

Once this is accomplished, it would be nice to be able to place the person's name at the beginning of each line before the note...i.e.

Larry: Larry's dog bites.
Greg: Greg's mailbox is green.
Mary: Mary's house is yellow.

I have been looking through the syntax of appending and piping information and am sure I will come up with an acceptable solution after awhile. But i know someone has the skills at hand to do this.
Thank you Neemobeer for taking the time to look at my post.
As always, I appreciate anyone who takes the time to help me with this.
 


Assuming your note text files are all in the same place something like this would work in Powershell.

38101


Code:
$BaseDirectory = "C:\Users\Justin\Desktop\TestDirectory"
$NotesDirectory = "\Notes"

$CombinedNotesFile = "\CombinedNotes.txt"
$NoteList = "\List.txt"

$NoteFiles = Get-Content -Path "$($BaseDirectory)$($NoteList)"

foreach($NoteFile in $NoteFiles)
{
    $NoteContent = Get-Content -Path "$($BaseDirectory)$($NotesDirectory)\$NoteFile"
    $NoteName = $NoteFile.Replace(".txt"," - ")
    Out-File -InputObject $("$($NoteName)$NoteContent") -FilePath "$($BaseDirectory)$CombinedNotesFile" -Append
}
 


Solution
This would work perfectly but, (I apologize in advance for this) the note names are on the same line as the person's name the note corresponds with. I attached the notefile. Is there a way to only collect the notefile up until the the file extension? I plan to then append the customers name to the combinednoteslist. Even if it appends the filename again that is fine. As long as I can ctrl+f the text document and search for the customer name and find the note associated. Also, if I have to rename all the files to txt I can do that and will do that upon request.
 


Attachments

I was able to change my files so that your code would work. It worked beautifully.I know this should be marked solved, but I am going to outline all the steps I took and the simple powershell codes I used to get this all to work. I have to append the names into my combinednotes file but I should be able to google an example of that. Is it against the rules of this forum to reward through paypal?
 


I suppose the appending was not as easy as I suspected. I believe that appending line 1 of Namelist.txt to line 1 of CombinedNotes.txt and then line 2 to line 2 etc. would use a very similar code to what you originally wrote. If you could help me with this portion of it as well I would appreciate it.
 


I can look when I get home. Basically the more structured the data the easier it is to parse.
Looking briefly at the txt file I don't think it will be too difficult.
I'm not against donations lol. They're not necessary though.

Powershell is pretty powerful so the code can be changed to suit almost any need/behavior.


You could change this line
$NoteName = $NoteFile.Replace(".txt"," - ")

to

$NoteName = $NoteFile.ToLower().Replace(".cst"," - ")

to accommodate the the note files .cst extension.

and

change foreach($NoteFile in $NoteFiles)

to

foreach($NoteFile in $NoteFiles[1..$($NoteFiles.Count-1))
to ignore the first line
 


Last edited:
I can look when I get home. Basically the more structured the data the easier it is to parse.
Looking briefly at the txt file I don't think it will be too difficult.
I'm not against donations lol. They're not necessary though.

Powershell is pretty powerful so the code can be changed to suit almost any need/behavior.


You could change this line
$NoteName = $NoteFile.Replace(".txt"," - ")

to

$NoteName = $NoteFile.ToLower().Replace(".cst"," - ")

to accommodate the the note files .cst extension.

and

change foreach($NoteFile in $NoteFiles)

to

foreach($NoteFile in $NoteFiles[1..$($NoteFiles.Count-1))
to ignore the first line
Thank you so much! The solution I came up with created extra junk data so this was very helpful.
Can i send you a paypal payment or is that not allowed? If it isn't can you direct me to donating to the site as a secondary choice.
Edit: I know it is not required but I like to give when I take. You helped me and while I am sure you would be better compensated in a corporate environment, I would like to give some.
 


Considering this solved. I will reformat the conversation tomorrow and mark it solved. Thanks Neemobeer!
 


Back
Top