Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am working on a Powershell script that will allow a user to define a large text file, have the script run, then save a portion of that large file to individual files with a unique file name which the script will grab and save to a defined directory. This is what I have so far.

$inputfile = Read-Host "Please type the FULL directory and file extenstion of the file you are wanting to split"

#contents of input file
$filecontent = Get-Content $inputfile

#Find the line numbers of each line containing specified text
$Lines = (Select-String -InputObject $filecontent -Pattern "Defined Pattern"| Select-Object LineNumber)

[int[]]$LineNumbers = @()
#remove the extra text from the beginning and end of each array element in the Lines array

foreach ($line in $Lines)
{
[string]$value = $line
$value = ($value.trim("@{LineNumber="))
$value = ($value.Substring(0,$value.length-1))
$LineNumbers += $value
}



#loop through array and write lines to new files
for ($i=0;$i -lt $LineNumbers.Count + 1;$i++) 
{

    $startLine = $LineNumbers[$i]
    $endLine = (($LineNumbers[$i + 1])-1)

    #Create the file to write into using the first word in the second line as the filename
    $AnimalIDLine = ($fileContent | Select-Object -Index ($startLine + 1))
    $AnimalID = ($AnimalIDLine -split '\s+')[0]

    New-Item -type file -Force ("C:\folder\folder" + $AnimalID + ".txt")

    #create a new .Net streamwriter object
    $stream = [System.IO.StreamWriter] ("C:\folder\folder" + $AnimalID + ".txt")

    #loop through start and end lines and write to file
    for ($h=$startLine; $h -le $endline; $h++)
        {
            #read the line
            $readLine = ($fileContent | Select-Object -Index $startLine)

            #write the line to the file
            $stream.WriteLine($readLine)

            $startLine++
        }
    $stream.close

}
$readline

The problem that I am having is when I run the script, it will only create 2 files, one that is blank with the incorrect filename(0.txt), and another that is blank with the correct filename(7HO12423.txt).

The message i am getting from Powershell when running the script is as follows(it stops running after it creates the two blank files).

-a---         6/19/2014   8:45 AM          0 7HO12423.txt                                                            

MemberType          : Method
OverloadDefinitions : {void Close()}
TypeNameOfValue     : System.Management.Automation.PSMethod
Value               : void Close()
Name                : Close
IsInstance          : True

-a---         6/19/2014   8:45 AM          0 .txt                                                                    

MemberType          : Method
OverloadDefinitions : {void Close()}
TypeNameOfValue     : System.Management.Automation.PSMethod
Value               : void Close()
Name                : Close
IsInstance          : True

In addition to fixing this issue and getting the script to run correctly, I would ideally also like for a user to define where the file gets saved instead of having it hard-coded into the script.

I am still fairly new at using Powershell, so any help would be appreciated. Please let me know if there is any additional information you require.

Thanks!

share|improve this question
    
Can you give an example of what the original file looks like and what the new files should look like? –  Tim Ferrill Jun 19 at 21:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.