0

I'd like to combine two files together using the copy command as follows. This is simple enough using

copy /b base.txt + file1.txt combined_file1.txt

however the first part of the file (base.txt) will remain constant and the second half will be from a folder full of different file (such as file1.txt, file2.txt, file3.txt etc)

The output file should be the variable filename with combined_ added at the front.

I'd like a batch file that I can place inside the directory of files and have it automatically add base.txt to the front of all the other variable files.

  • Is Powershell and option? if so this one-liner might start you off: Get-ChildItem "Files" -Filter *.txt | Foreach-Object { (gc base.txt) + (gc ("files\"+$_.Name) ) | out-file -filepath ("files\combined_"+$_.Name)} base.txt is in the root with a files folder containing the files. – HelpingHand Dec 17 '19 at 21:27
  • The way this place works, is that you post the script that you wrote, that doesn't work, and we tell you what's wrong with it. Maybe. – Michael Harvey Dec 17 '19 at 21:28
1

Here is a powershell script that will do it. Just adjust the base path and the combined folder and the files folder.

$baseFilename = "c:\temp\base.txt"
$addonFolderLocation = "c:\temp\files"
$combinedFolderLocation = "c:\temp\combined\"

#Get all files in addon folder location
$addonFilenames = Get-ChildItem $addonFolderLocation

foreach ($addonFilename in $addonFilenames)
{
    cat $baseFilename, $addonFilename.fullname | sc "$combinedFolderLocation combined_ $addonFilename"
}

edit: added combined_ before filename in combined output

  • 1
    That's absolutely perfect, just tried it and it does exactly what I needed. I Googled this for quite some time and couldn't find any other post which addressed my question. Many thanks for your kind help. – Ab_Phoenix Dec 17 '19 at 23:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.