简体   繁体   English

从2个txt文件创建导入文件

[英]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. 我正在尝试导出我的Kunena论坛帖子,以将其导入到我们的新wp论坛服务器中。 For this I´ve created 2 files... One which contains the messages in the following format: 为此,我创建了2个文件...一个文件,其中包含以下格式的消息:

(`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: 另一个包含消息的文本:

(`mesid`, `message`)

From the first file I only need the "fields" id, parent, time, userid, subject and hits. 从第一个文件中,我只需要“字段” ID,父级,时间,用户ID,主题和匹配。 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... 由于有数百篇文章,而复制和粘贴的东西确实很耗时,我认为通过脚本执行此操作会容易得多...最好是通过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

}

You could try something like this to get the information you require: 您可以尝试执行以下操作来获取所需的信息:

$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
                    }

This will give the following output in the given file ( $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`)

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 . 您需要做的就是使用类似的方法来获取正确的消息并将其放入变量$message


Going on the assumption that your ID and MesID are the same you can use something like this for the $message varible: 假设您的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 being the path to your message file. $path2是您的消息文件的路径。


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
                    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM