PowerShell read csv file and create files accordingly
up vote
0
down vote
favorite
Basically, I have a CSV file with a lot of columns. Let's say it looks like this:
_username, platform_
username1, platformX1
username2, platformY
username3, platformX2
username4, platformX2
..., ...
I would like to write a script that goes through the file and for each platform, it creates a file with the specific username in a different folder, so I would have:
platformX1username1.file
platformYusername2.file
platformX2username3.file, username4.file
etc etc...
I know I should use foreach with an if placed somewhere, but powershell is new for me, and I don't really know how to start it.
powershell
add a comment |
up vote
0
down vote
favorite
Basically, I have a CSV file with a lot of columns. Let's say it looks like this:
_username, platform_
username1, platformX1
username2, platformY
username3, platformX2
username4, platformX2
..., ...
I would like to write a script that goes through the file and for each platform, it creates a file with the specific username in a different folder, so I would have:
platformX1username1.file
platformYusername2.file
platformX2username3.file, username4.file
etc etc...
I know I should use foreach with an if placed somewhere, but powershell is new for me, and I don't really know how to start it.
powershell
1
Please format your code and sample input/output properly.
– mklement0
Nov 21 at 19:40
Your queston may have an answer here
– Walter Mitty
Nov 22 at 3:57
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Basically, I have a CSV file with a lot of columns. Let's say it looks like this:
_username, platform_
username1, platformX1
username2, platformY
username3, platformX2
username4, platformX2
..., ...
I would like to write a script that goes through the file and for each platform, it creates a file with the specific username in a different folder, so I would have:
platformX1username1.file
platformYusername2.file
platformX2username3.file, username4.file
etc etc...
I know I should use foreach with an if placed somewhere, but powershell is new for me, and I don't really know how to start it.
powershell
Basically, I have a CSV file with a lot of columns. Let's say it looks like this:
_username, platform_
username1, platformX1
username2, platformY
username3, platformX2
username4, platformX2
..., ...
I would like to write a script that goes through the file and for each platform, it creates a file with the specific username in a different folder, so I would have:
platformX1username1.file
platformYusername2.file
platformX2username3.file, username4.file
etc etc...
I know I should use foreach with an if placed somewhere, but powershell is new for me, and I don't really know how to start it.
powershell
powershell
edited Nov 21 at 19:55
kit
1,1082516
1,1082516
asked Nov 21 at 19:33
raten
1
1
1
Please format your code and sample input/output properly.
– mklement0
Nov 21 at 19:40
Your queston may have an answer here
– Walter Mitty
Nov 22 at 3:57
add a comment |
1
Please format your code and sample input/output properly.
– mklement0
Nov 21 at 19:40
Your queston may have an answer here
– Walter Mitty
Nov 22 at 3:57
1
1
Please format your code and sample input/output properly.
– mklement0
Nov 21 at 19:40
Please format your code and sample input/output properly.
– mklement0
Nov 21 at 19:40
Your queston may have an answer here
– Walter Mitty
Nov 22 at 3:57
Your queston may have an answer here
– Walter Mitty
Nov 22 at 3:57
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
Here's something similar tweaked to what you want to do.
This created files of a specific size in your directory
$data = get-content "C:DatamasteFile.csv"
$drcty = "C:Data"
foreach($line in $data)
{
$a,$b = $line.Split("{,}")
$parent = $drcty+""+$a+""
$filename = $parent + $b.TRIM() +".txt"
write-host $filename
New-Item -ItemType directory -Path $parent
fsutil file createnew $filename 2000
}
add a comment |
up vote
0
down vote
this seems to do what you want. [grin]
# fake reading in a CSV file
# in real life, use Import-CSV
$InStuff = @'
_UserName, Platform_
username1, platformX1
username2, platformY
username3, platformX2
username4, platformX2
'@ | ConvertFrom-Csv
$DestDir = $env:TEMP
$Extension = 'txt'
foreach ($IS_Item in $InStuff)
{
$TargetPath = Join-Path -Path $DestDir -ChildPath $IS_Item.Platform_
if (-not (Test-Path -LiteralPath $TargetPath))
{
# "$Null = " will supress unwanted output from New-Item
$Null = New-Item -Path $TargetPath -ItemType Directory
}
$FileName = $IS_Item._UserName, $Extension -join '.'
$FullFileName = Join-Path -Path $TargetPath -ChildPath $FileName
if (-not (Test-Path -LiteralPath $FullFileName))
{
# "$Null = " will supress unwanted output from New-Item
$Null = New-Item -Path $FullFileName -ItemType File
}
}
i think what it does is apparent, but i know i get ahead of where i otta be at times. so, if there are any questions, please feel free to ask ... [grin]
add a comment |
up vote
0
down vote
This worked for me.
$ErrorActionPreference = "Stop"
#Path of the CSV file resides
$csvData = Import-Csv -Path "$env:USERPROFILEDesktopdata.csv"
$DestinationFolder = "C:Stuffs"
foreach($record in $csvData)
{
$destPlatformFolder = $DestinationFolder + "" + $record.platform_
if(-not(Test-Path -Path $destPlatformFolder)){
New-Item -Path $destPlatformFolder -ItemType Directory -Force | Out-Null
}
$destinationFile = $destPlatformFolder + "" + $record._username
New-Item -Path $destinationFile -ItemType File -Force | Out-Null
}
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Here's something similar tweaked to what you want to do.
This created files of a specific size in your directory
$data = get-content "C:DatamasteFile.csv"
$drcty = "C:Data"
foreach($line in $data)
{
$a,$b = $line.Split("{,}")
$parent = $drcty+""+$a+""
$filename = $parent + $b.TRIM() +".txt"
write-host $filename
New-Item -ItemType directory -Path $parent
fsutil file createnew $filename 2000
}
add a comment |
up vote
0
down vote
Here's something similar tweaked to what you want to do.
This created files of a specific size in your directory
$data = get-content "C:DatamasteFile.csv"
$drcty = "C:Data"
foreach($line in $data)
{
$a,$b = $line.Split("{,}")
$parent = $drcty+""+$a+""
$filename = $parent + $b.TRIM() +".txt"
write-host $filename
New-Item -ItemType directory -Path $parent
fsutil file createnew $filename 2000
}
add a comment |
up vote
0
down vote
up vote
0
down vote
Here's something similar tweaked to what you want to do.
This created files of a specific size in your directory
$data = get-content "C:DatamasteFile.csv"
$drcty = "C:Data"
foreach($line in $data)
{
$a,$b = $line.Split("{,}")
$parent = $drcty+""+$a+""
$filename = $parent + $b.TRIM() +".txt"
write-host $filename
New-Item -ItemType directory -Path $parent
fsutil file createnew $filename 2000
}
Here's something similar tweaked to what you want to do.
This created files of a specific size in your directory
$data = get-content "C:DatamasteFile.csv"
$drcty = "C:Data"
foreach($line in $data)
{
$a,$b = $line.Split("{,}")
$parent = $drcty+""+$a+""
$filename = $parent + $b.TRIM() +".txt"
write-host $filename
New-Item -ItemType directory -Path $parent
fsutil file createnew $filename 2000
}
answered Nov 21 at 20:03
Ivhani Maselesele
212
212
add a comment |
add a comment |
up vote
0
down vote
this seems to do what you want. [grin]
# fake reading in a CSV file
# in real life, use Import-CSV
$InStuff = @'
_UserName, Platform_
username1, platformX1
username2, platformY
username3, platformX2
username4, platformX2
'@ | ConvertFrom-Csv
$DestDir = $env:TEMP
$Extension = 'txt'
foreach ($IS_Item in $InStuff)
{
$TargetPath = Join-Path -Path $DestDir -ChildPath $IS_Item.Platform_
if (-not (Test-Path -LiteralPath $TargetPath))
{
# "$Null = " will supress unwanted output from New-Item
$Null = New-Item -Path $TargetPath -ItemType Directory
}
$FileName = $IS_Item._UserName, $Extension -join '.'
$FullFileName = Join-Path -Path $TargetPath -ChildPath $FileName
if (-not (Test-Path -LiteralPath $FullFileName))
{
# "$Null = " will supress unwanted output from New-Item
$Null = New-Item -Path $FullFileName -ItemType File
}
}
i think what it does is apparent, but i know i get ahead of where i otta be at times. so, if there are any questions, please feel free to ask ... [grin]
add a comment |
up vote
0
down vote
this seems to do what you want. [grin]
# fake reading in a CSV file
# in real life, use Import-CSV
$InStuff = @'
_UserName, Platform_
username1, platformX1
username2, platformY
username3, platformX2
username4, platformX2
'@ | ConvertFrom-Csv
$DestDir = $env:TEMP
$Extension = 'txt'
foreach ($IS_Item in $InStuff)
{
$TargetPath = Join-Path -Path $DestDir -ChildPath $IS_Item.Platform_
if (-not (Test-Path -LiteralPath $TargetPath))
{
# "$Null = " will supress unwanted output from New-Item
$Null = New-Item -Path $TargetPath -ItemType Directory
}
$FileName = $IS_Item._UserName, $Extension -join '.'
$FullFileName = Join-Path -Path $TargetPath -ChildPath $FileName
if (-not (Test-Path -LiteralPath $FullFileName))
{
# "$Null = " will supress unwanted output from New-Item
$Null = New-Item -Path $FullFileName -ItemType File
}
}
i think what it does is apparent, but i know i get ahead of where i otta be at times. so, if there are any questions, please feel free to ask ... [grin]
add a comment |
up vote
0
down vote
up vote
0
down vote
this seems to do what you want. [grin]
# fake reading in a CSV file
# in real life, use Import-CSV
$InStuff = @'
_UserName, Platform_
username1, platformX1
username2, platformY
username3, platformX2
username4, platformX2
'@ | ConvertFrom-Csv
$DestDir = $env:TEMP
$Extension = 'txt'
foreach ($IS_Item in $InStuff)
{
$TargetPath = Join-Path -Path $DestDir -ChildPath $IS_Item.Platform_
if (-not (Test-Path -LiteralPath $TargetPath))
{
# "$Null = " will supress unwanted output from New-Item
$Null = New-Item -Path $TargetPath -ItemType Directory
}
$FileName = $IS_Item._UserName, $Extension -join '.'
$FullFileName = Join-Path -Path $TargetPath -ChildPath $FileName
if (-not (Test-Path -LiteralPath $FullFileName))
{
# "$Null = " will supress unwanted output from New-Item
$Null = New-Item -Path $FullFileName -ItemType File
}
}
i think what it does is apparent, but i know i get ahead of where i otta be at times. so, if there are any questions, please feel free to ask ... [grin]
this seems to do what you want. [grin]
# fake reading in a CSV file
# in real life, use Import-CSV
$InStuff = @'
_UserName, Platform_
username1, platformX1
username2, platformY
username3, platformX2
username4, platformX2
'@ | ConvertFrom-Csv
$DestDir = $env:TEMP
$Extension = 'txt'
foreach ($IS_Item in $InStuff)
{
$TargetPath = Join-Path -Path $DestDir -ChildPath $IS_Item.Platform_
if (-not (Test-Path -LiteralPath $TargetPath))
{
# "$Null = " will supress unwanted output from New-Item
$Null = New-Item -Path $TargetPath -ItemType Directory
}
$FileName = $IS_Item._UserName, $Extension -join '.'
$FullFileName = Join-Path -Path $TargetPath -ChildPath $FileName
if (-not (Test-Path -LiteralPath $FullFileName))
{
# "$Null = " will supress unwanted output from New-Item
$Null = New-Item -Path $FullFileName -ItemType File
}
}
i think what it does is apparent, but i know i get ahead of where i otta be at times. so, if there are any questions, please feel free to ask ... [grin]
answered Nov 21 at 20:34
Lee_Dailey
1,15776
1,15776
add a comment |
add a comment |
up vote
0
down vote
This worked for me.
$ErrorActionPreference = "Stop"
#Path of the CSV file resides
$csvData = Import-Csv -Path "$env:USERPROFILEDesktopdata.csv"
$DestinationFolder = "C:Stuffs"
foreach($record in $csvData)
{
$destPlatformFolder = $DestinationFolder + "" + $record.platform_
if(-not(Test-Path -Path $destPlatformFolder)){
New-Item -Path $destPlatformFolder -ItemType Directory -Force | Out-Null
}
$destinationFile = $destPlatformFolder + "" + $record._username
New-Item -Path $destinationFile -ItemType File -Force | Out-Null
}
add a comment |
up vote
0
down vote
This worked for me.
$ErrorActionPreference = "Stop"
#Path of the CSV file resides
$csvData = Import-Csv -Path "$env:USERPROFILEDesktopdata.csv"
$DestinationFolder = "C:Stuffs"
foreach($record in $csvData)
{
$destPlatformFolder = $DestinationFolder + "" + $record.platform_
if(-not(Test-Path -Path $destPlatformFolder)){
New-Item -Path $destPlatformFolder -ItemType Directory -Force | Out-Null
}
$destinationFile = $destPlatformFolder + "" + $record._username
New-Item -Path $destinationFile -ItemType File -Force | Out-Null
}
add a comment |
up vote
0
down vote
up vote
0
down vote
This worked for me.
$ErrorActionPreference = "Stop"
#Path of the CSV file resides
$csvData = Import-Csv -Path "$env:USERPROFILEDesktopdata.csv"
$DestinationFolder = "C:Stuffs"
foreach($record in $csvData)
{
$destPlatformFolder = $DestinationFolder + "" + $record.platform_
if(-not(Test-Path -Path $destPlatformFolder)){
New-Item -Path $destPlatformFolder -ItemType Directory -Force | Out-Null
}
$destinationFile = $destPlatformFolder + "" + $record._username
New-Item -Path $destinationFile -ItemType File -Force | Out-Null
}
This worked for me.
$ErrorActionPreference = "Stop"
#Path of the CSV file resides
$csvData = Import-Csv -Path "$env:USERPROFILEDesktopdata.csv"
$DestinationFolder = "C:Stuffs"
foreach($record in $csvData)
{
$destPlatformFolder = $DestinationFolder + "" + $record.platform_
if(-not(Test-Path -Path $destPlatformFolder)){
New-Item -Path $destPlatformFolder -ItemType Directory -Force | Out-Null
}
$destinationFile = $destPlatformFolder + "" + $record._username
New-Item -Path $destinationFile -ItemType File -Force | Out-Null
}
answered Nov 22 at 3:50
Basavaraju B K
3318
3318
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419348%2fpowershell-read-csv-file-and-create-files-accordingly%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Please format your code and sample input/output properly.
– mklement0
Nov 21 at 19:40
Your queston may have an answer here
– Walter Mitty
Nov 22 at 3:57