Powershell script to convert .csv to .html and handling multiple files

Deadly 提交于 2021-01-29 07:05:51

问题


I have to write a script that takes the content of multiple .csv files and write them into one .html file. The goal ist to see all filenames as buttons and be able to expand buttons to see the content.

My problem is that I don't know how to give the data-target and div id the correct name for each file.

I guess you figured out by now that my scripting skills are... very very limited but here is my try:

$in = "C:\clientlist\*"
$out = 'C:\clientlist\test.html'

$head = @"
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
"@
$Pre = @"
<div class="container">
<h2></h2>
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#demo">Filename</button>
<div id="demo" class="collapse">
"@
$Post = @"
</p></div>
"@
$Head | Out-File -filepath $out
$List = Get-ChildItem $in -Filter *.csv
$User =  $List |
ForEach-Object {Import-Csv $_ | Select-Object | convertto-html -fragment -precontent $pre -postcontent $post}
$User | Out-File -filepath $out -append

Any help would be highly appreciated and if you need any further information, just tell me!


回答1:


You're close!
What you need to do is define the $Pre string inside the ForEach-Object loop. The Select-Object you do in between there has no use unless you want to select a subset of the CSV data to output.

Something like this should do it:

$Head | Out-File -filepath $out -Encoding utf8
$List = Get-ChildItem $in -Filter *.csv -File | ForEach-Object {
    $name = $_.BaseName
    $id = '{0}_{1}' -f $name, (Get-Date).Ticks  # to make this id unique, append the Ticks to the name

    $Pre = @"
<div class="container">
<h2></h2>
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#$id">$name</button>
<div id="$id" class="collapse">
"@
    $Post = '</p></div>'
    Import-Csv -Path $_.FullName | ConvertTo-Html -Fragment -PreContent $pre -PostContent $post | 
    Out-File -FilePath $out -Encoding utf8 -Append
}

An alternative could be to create your $Pre as template including placeholders {0} and {1}, and fill in the name and id inside the ForEach loop using the -f Format operator:

$Head | Out-File -filepath $out -Encoding utf8

$Pre = @'
<div class="container">
<h2></h2>
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#{0}">{1}</button>
<div id="{0}" class="collapse">
'@
$Post = '</p></div>'

$List = Get-ChildItem $in -Filter *.csv -File | ForEach-Object {
    $name = $_.BaseName
    $id = '{0}_{1}' -f $name, (Get-Date).Ticks  # to make this id unique, append the Ticks to the name
    # use the $Pre template to fill in the placeholders
    $precontent = $Pre -f $id, $name

    Import-Csv -Path $_.FullName | ConvertTo-Html -Fragment -PreContent $precontent -PostContent $post | 
    Out-File -FilePath $out -Encoding utf8 -Append
}


来源:https://stackoverflow.com/questions/62871747/powershell-script-to-convert-csv-to-html-and-handling-multiple-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!