Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Inject CSV lines into HTML files -where am i going wrong?

Posted on 2015-01-13
1
Medium Priority
?
112 Views
Last Modified: 2015-01-13
Guys, what am i doing wrong here?
I have a huge CSV file , its not delimited properly, but what i want to do is to inject each from it to an HTML file with a random name.
the script below creates one one file, properly, but loops on it (over writing it) insted of creating multiple files per line.
Can you help?

$a = Get-Content .\BigCSVFILE.csv 
$1 = get-date -f yyyy-MM-dd-hh-mm-ss
$2 = ".html"
$random = -join(48..57+65..90+97..122 | ForEach-Object {[char]$_} | Get-Random -Count 6)
foreach ($i in $a) {echo $i > $1$random$2}

Open in new window

0
Comment
Question by:PleaseAnswer
1 Comment
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 2000 total points
ID: 40546340
Hi,
as far as I understand the issue is that the $random variable is initialized only once. What I do not understand is your requirement: do you just need a random file name?
Because you initialize everything outside the loop and therefore the file name is static.
If you switch your code to this
$a = Get-Content .\BigCSVFILE.csv
$1 = get-date -f yyyy-MM-dd-hh-mm-ss
$2 = ".html"
foreach ($i in $a) {
$random = -join(48..57+65..90+97..122 | ForEach-Object {[char]$_} | Get-Random -Count 6)
echo $i > $1$random$2
}

Open in new window

it will work.
HTH
Rainer
0

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Loops Section Overview
Screencast - Getting to Know the Pipeline

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question