简体   繁体   中英

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

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!

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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