0

代码:

$date = (Join-path C:\Users\<username>\Desktop\ (Get-Date).toString())
New-Item -Path c:\Users\<username>\Desktop -Name "" + $((Get-Date).toString()) + ".txt" -ItemType file
$reader = [System.IO.File]::OpenText("C:\Users\<username>\Desktop\test.csv")
try {
    for(;;) {
        $line = $reader.ReadLine()
        if ($line -eq $null) { break }
        # process the line
    Add-Content "C:\Users\<username>\Desktop\"($date.txt) -value $line
        $line
    }
}
finally {
    $reader.Close()
}

我已经尝试了十几种不同的方法,但我不知道该怎么做。我正在尝试制作一个包含日期作为名称一部分的文件(用于文档,因为我会定期做几次),并处理要更改的文件的每一行并将其放入新文件中。有谁知道如何让它工作?

编辑:好的,所以我通读了它,它几乎可以工作。不幸的是,我认为我没有正确调用这些对象。

我的代码:

$fileName = '{0:yyyy-MM-dd HH-mm-ss}.html' -f (Get-Date)
New-Item $fileName -itemtype file
Add-content $fileName "<html>"
Add-content $fileName "<head>"
Add-content $fileName "    <title>Call Logs</title>"
Add-content $fileName "<head>"
Add-content $fileName "<body>"
Add-content $fileName "    <div id='header'>"
Add-content $fileName "    </div>"
Add-content $fileName "    <div id='table'>"
Add-content $fileName "        </table>"
Add-content $fileName "        <tr>"
Add-content $fileName "            <th>Date</th>"
Add-content $fileName "            <th>Time</th>"
Add-content $fileName "            <th>Call Type</th>"
Add-content $fileName "            <th>Calling Number</th>"
Add-content $fileName "            <th>Calling Extension</th>"
Add-content $fileName "            <th>Calling Department</th>"
Add-content $fileName "            <th>Called Number</th>"
Add-content $fileName "            <th>Called Extension</th>"
Add-content $fileName "            <th>Called Department</th>"
Add-content $fileName "            <th>Call Connected</th>"
Add-content $fileName "            <th>Duration</th>"
Add-content $fileName "            <th>Queuing Time</th>"
Add-content $fileName "            <th>Account Code</th>"
Add-content $fileName "            <th>Carrier Code</th>"
Add-content $fileName "        </tr>"

import-csv -path 'C:\users\<username>\Desktop\test.csv' |
    foreach-object{
    $newRow = "        <tr><td>$_.Date</td><td>$_.Time</td><td>$_.'Call Type'</td><td>$_.'Calling Number'</td><td>$_.'Calling Extension'</td><td>$_.'Calling Department'</td><td>$_.'Called Number'</td><td>$_.'Called Extension'</td><td>$_.'Called Department'</td><td>$_.'Call Connected'</td><td>$_.Duration</td><td>$_.'Queuing Time'</td><td>$_.'Account Code'</td><td>$_.'Carrier Code'</td></tr>"
        return $newRow

    } |
    Add-content $fileName

现在我得到,每次调用 $ .Whatever,一切。例如,当我调用 $ .Date 时,我得到

@{Date=7/1/2012; Time=16:43:06; Call Type=Terminating; Calling Number=XXXXXXXXXX; Calling Extension=; Calling Department=; Called Number=YYYYYYYYYY; Called Extension=XXXX; Called Department=Sales Engineers; Call Connected=No; Duration=0:00:11; Queuing Time=; Account Code=; Carrier Code=}.Date
4

1 回答 1

3

你没有说什么不起作用,所以这里有一些通用的建议。如果您可以发布任何错误消息或无法正常工作的特定内容,我可以更具体。

正如评论者所说,(Get-Date).ToString()返回类似的东西7/10/2013 7:38:22 AM,它可能会或可能不会在文件名中工作。您需要对其进行格式化,以便您知道它可以在文件名中工作,例如$fileName = '{0:yyyy-MM-dd-HH-mm-ss}.txt' -f (Get-Date).

其次,PowerShell 有一个Import-Csv函数,它将读取 CSV 文件并将其转换为对象。然后,您可以处理每个对象,这些对象将具有 CSV 的每一列的属性,并在目标文件中返回您想要的字符串。

这是一个更新的脚本:

$fileName = '{0:yyyy-MM-dd-HH-mm-ss}.txt' -f (Get-Date)
$outputFile = Join-Path 'C:\Users\<username>\Desktop' $filename
New-Item $outputFile -ItemType File
Import-Csv -Path 'C:\Users\<username>\Desktop\test.csv' |
    ForEach-Object { 
        "        <tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td><td>{8}</td><td>{9}</td><td>{10}</td><td>{11}</td><td>{12}</td><td>{13}</td></tr>" -f `
            $_.Date,$_.Time,$_.'Call Type',$_.'Calling Number',$_.'Calling Extension',$_.'Calling Department',$_.'Called Number',$_.'Called Extension',$_.'Called Department',$_.'Call Connected',$_.Duration,$_.'Queuing Time',$_.'Account Code',$_.'Carrier Code'
    } |
    Add-Content $outputFile
于 2013-07-10T14:42:16.213 回答