Create import file from 2 txt files

I´m trying to export my Kunena forum posts to import them into our new wp forum server. For this I´ve created 2 files… One which contains the messages in the following format:

(`id`, `parent`, `thread`, `catid`, `name`, `userid`, `email`, `subject`, `time`, `ip`, `topic_emoticon`, `locked`, `hold`, `ordering`, `hits`, `moved`, `modified_by`, `modified_time`, `modified_reason`)  

The other contains the text for the message:

Read More
(`mesid`, `message`)

From the first file I only need the “fields” id, parent, time, userid, subject and hits.
From the second i need the corresponding “field” message

Afterwards it should be formatted like this:

(`id`, `message`, `parent`, `time`, `userid`, `subject`, `hits`)

Since there are hundreds of posts and the copy & paste thing is really time consuming i thought it would be a lot easier to do this via a script… Preferably by PowerShell…

Hope you guys can help me out…

$outputFile = "C:logFile.txt"
$path = "C:kunena_messages.txt"
$path2 = "C:kunena_messages_text.txt"

get-content $path | % {$array = $_ -split ",","0"                 
                     $message = get-content $path2 | %{If($_ -match ($array[0].Trim() -replace "(","" )){
                                $msgArray = $_ -split ",","0"
                                $msgArray[1] -replace ")",""}}
                      $newString = $array[0].Trim()+","+$message +","+$array[1].Trim()+","+`
                                        $array[8].Trim()+","+$array[5].Trim()+","+$array[7].Trim()+","+`
                                        $array[14].Trim()+")" 
                      $newString | ac $outputFile

}

Related posts

Leave a Reply

1 comment

  1. You could try something like this to get the information you require:

    $outputFile = "C:logFile.txt"
    $path = "C:testtest.txt"
    get-content $path | %{$array = $_ -split ",","0"
                          $message ="This is a message"
                          $newString = "("+$array[0].Trim()+","+$message +","+$array[1].Trim()+","+`
                                            $array[8].Trim()+","+$array[5].Trim()+","+$array[7].Trim()+","+`
                                            $array[14].Trim()+")" 
                          $newString | ac $outputFile
                        }
    

    This will give the following output in the given file ($outputFile).

    ("`id`,`This is a message`,`parent`,`time`,`userid`,`subject`,`hits`)
    ("`id2`,`This is a message`,`parent2`,`time`,`userid`,`subject`,`hits`)
    ("`id3`,`This is a message`,`parent2`,`time`,`userid`,`subject`,`hits`)
    

    As you can see I have picked out all the parts you required apart from the message; as I’m not sure how you are linking the two. All you will need to do is use a similar method to get the correct message and put it into the variable $message.


    Going on the assumption that your ID and MesID are the same you can use something like this for the $message varible:

    $path2 = "C:Messagestest.txt"          
    $message = get-content $path2 | %{ $msgArray = $_ -split ",","0" -replace ")",""
                                       $msgArray = $msgArray -replace "(",""
                                       $m = $array[0].Trim() -replace "`"","" 
                                       If($msgArray[0].Trim() -eq $m){$msgArray[1]}
                                      }
    

    $path2 being the path to your message file.


    So all together it should look like this:

    $outputFile = "C:logFile.txt"
    $path = "C:kunena_messages.txt"
    $path2 = "C:kunena_messages_text.txt"
    
    get-content $path | %{$array = $_ -split ",","0"
                          $array = $array -replace "(","" `
                                          -replace ")","" `
                                          -replace "`"",""
                          $message = get-content $path2 | %{ $msgArray = $_ -split ",","0" -replace ")",""
                                                             $msgArray = $msgArray -replace "(",""
                                                             $m = $array[0].Trim() -replace "`"","" 
                                                             If($msgArray[0].Trim() -eq $m){$msgArray[1]}
                                                            }
                          $newString = "("+$array[0].Trim()+","+$message +","+$array[1].Trim()+","+`
                                            $array[8].Trim()+","+$array[5].Trim()+","+$array[7].Trim()+","+`
                                            $array[14].Trim()+")" 
                          $newString | ac $outputFile
                        }