Create A Separate New File For Each Line of Text

Back to Articles

Create A Separate New File For Each Line of Text

This procedure will work on any file that is text based. This includes .bat, ps1, .rtf, etc. It does not have to be a plain .txt file only. It will not work with encoded files when the text within cannot be natively read. These file types include .exe, .docx, .pdf, etc. It may still extract the lines, but the output will be encrypted code, not the actual text within. Moving on….

The example text file we’ll be using is named ‘Source.txt’. It contains a list of 9 fastboot commands to run in CMD. I want to create a separate text file for each command in the list. Here’s a screenshot.

 

Here’s how it’s done.


1. Open the folder that contains the source file.

 

2. With nothing selected, hold shift, then right-click in the window. Select ‘Open Powershell window here’ from the context menu.

 

3. In Powershell, type or paste the command below.

$counter = 1
Get-Content “myfile.txt” | foreach {
Set-Content -Path “$counter.txt” -Value $_
$counter++
}

4. Make sure to replace “myfile.txt” with the name and extension of YOUR source file. DO NOT REMOVE THE QUOTES. You can also change the file type that Powershell will output by editing the extension for  “$counter.txt”. For example, to “$counter.bat”.

When ready, hit ENTER.

 

If done correctly, you won’t get any messages. The path will reappear, awaiting the next command.

 

5. Wait for it! In a few seconds Powershell will create as many new text files as required, in the same folder as the source. Each containing a single line from the source. The names of the files represent what line it was pulled from. Sweet!

Here’s an updated screenshot.

 

6. In Powershell, type ‘exit’ and press ENTER to close the window.

 

7. Give yourself a standing ovation and a hand clap….. You KNOW you want to. 🙂

And we’re done.

If you want to bulk rename the new .txt files according to a line of their own text check this guide out.

Leave a Reply

Your email address will NOT be published. Required fields are marked with an *

Back to Articles