1

我正在尝试导出我的 Kunena 论坛帖子,以将它们导入我们新的 wp 论坛服务器。为此,我创建了 2 个文件...一个包含以下格式的消息:

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

另一个包含消息的文本:

(`mesid`, `message`)

从第一个文件中,我只需要“字段”ID、父级、时间、用户 ID、主题和命中。从第二个我需要相应的“字段”消息

之后它的格式应该是这样的:

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

由于有数百个帖子,并且复制和粘贴的事情真的很耗时,我认为通过脚本执行此操作会容易得多......最好是通过 PowerShell ......

希望大家能帮帮我...

$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

}
4

1 回答 1

0

您可以尝试这样的事情来获取您需要的信息:

$outputFile = "C:\logFile.txt"
$path = "C:\test\test.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
                    }

这将在给定文件 ( $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`)

如您所见,除了消息之外,我已经选择了您需要的所有部分;因为我不确定您如何将两者联系起来。您需要做的就是使用类似的方法来获取正确的消息并将其放入变量$message中。


假设您的 ID 和 MesID 相同,您可以对$message变量使用以下内容:

$path2 = "C:\Messages\test.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作为您的消息文件的路径。


所以总的来说它应该是这样的:

$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
                    }
于 2013-08-02T11:02:21.390 回答