Parse a file name and export to a CSV file
I would like to scan a folder that contains JPG files and parse the file name into a CSV file. All the files have exact same convention separated by "-". (For example: 800x800-Visio-large.jpg) I would like CSV file to contain
3 separate columns; Resolution, Type and Size.
I tried and I'm having issue converting object to string since I don't want .jpg in the CSV file.
January 11th, 2014 10:33pm
Here's one way to get yourself started. The first command removes the file extension and the second command splits the file name at the hyphen. You can access each of the elements of the variable by using the proper index.
$Var = '800x800-Visio-large.jpg'.Split('\.')[-2]
$Var = $Var.Split('-')
Write-Output $Var[0]
Write-Output $Var[1]
Write-Output $Var[2]
I failed to mention that you could do this in a single line when used with parentheses (order of operation).
$Var = ('800x800-Visio-large.jpg'.Split('\.')[-2]).Split('-')
- Edited by
tommymaynard
Saturday, January 11, 2014 8:29 PM
Added info
January 11th, 2014 10:44pm
Here's one way to get yourself started. The first command removes the file extension and the second command splits the file name at the hyphen. You can access each of the elements of the variable by using the proper index.
$Var = '800x800-Visio-large.jpg'.Split('\.')[-2]
$Var = $Var.Split('-')
Write-Output $Var[0]
Write-Output $Var[1]
Write-Output $Var[2]
I failed to mention that you could do this in a single line when used with parentheses (order of operation).
$Var = ('800x800-Visio-large.jpg'.Split('\.')[-2]).Split('-')
- Edited by
tommymaynard
Saturday, January 11, 2014 8:29 PM
Added info
January 11th, 2014 10:44pm
$Folder = "C:\...\1\test-bilder"
$csvFile = "C:\...\2\mypictures.csv"
Get-ChildItem -Path "$Folder" -ErrorAction Stop | ForEach-Object {
$this = $_.BaseName.split('-')
[pscustomobject]@{
"resolution"=$this[0]
"type"=$this[1]
"size"=$this[2]
}
}|Export-Csv -NoTypeInformation -Path "$csvFile"
notepad $csvFile
- Proposed as answer by
romeo doncaMVP
Saturday, January 11, 2014 9:52 PM
January 12th, 2014 12:36am
$Folder = "C:\...\1\test-bilder"
$csvFile = "C:\...\2\mypictures.csv"
Get-ChildItem -Path "$Folder" -ErrorAction Stop | ForEach-Object {
$this = $_.BaseName.split('-')
[pscustomobject]@{
"resolution"=$this[0]
"type"=$this[1]
"size"=$this[2]
}
}|Export-Csv -NoTypeInformation -Path "$csvFile"
notepad $csvFile
- Proposed as answer by
romeo doncaMVP
Saturday, January 11, 2014 9:52 PM
- Marked as answer by
2B PowerShell Guru
17 hours 7 minutes ago
January 12th, 2014 12:36am
It should be noted that MTMM's script is written for PowerShell 3.0. You will have to use a different method to create your custom object if you use the example on an older version of PowerShell.
January 12th, 2014 1:31am
Thank you very much. It was very helpful.
January 13th, 2014 1:28pm
It worked like a charm. I would like to ask one more thing. Is there a way to add a filesize column to the CSV file? Obviously it's not part of the filename so it'll have to grabbed using .Length/KB but I'm not sure how I can incorporate
that into your script above. Thanks again.
January 13th, 2014 1:31pm
Try adding this to the [pscustomobject]:
"filesize"="$([int]($_.length/1KB))KB"
January 13th, 2014 3:56pm