A little background on my intent:
The purpose of the first function, GetFileNames, is to read through directories in a main folder that contains digital information on a number of facilities, and return the list of files with their BaseName.
The second function, SplitFileNames, takes the output of GetFileNames, and splits the name into 3 parts using the underscore as a delimiter. The names of the files are all structured like this; SITECODE_FACNUM_FILETYPE, so for example, DKFX_00099_MAP. Each of the 3 parts form individual columns that I then import into a database in Access.
Also, I've never used Powershell before this, and the experience I've had so far is basically a combination of reverse engineering and splicing code from a number of sources, and obviously some of my own writing.
My questions/respectful requests are:
I'm almost certain there's got to be a better way to do what I'm trying to accomplish, and that I just don't have a solid enough understanding with the information I've gone through to make it happen. So I would definitely appreciate any recommendations at all for improvement.
I also need the hyperlink information contained in FullName as column as well, but unfortunately I could never get it to work correctly since I have to split only the BaseName up into 3 pieces.
Thank you!
$targetPath = "C:\Users\mattm\Google Drive\TestDatabase\"
$outputPath_1 = "C:\Users\mattm\Google Drive\Powershell Scripts\Facilities Database Scanner\outputScan_1.csv"
$outputPath_2 = "C:\Users\mattm\Google Drive\Powershell Scripts\Facilities Database Scanner\outputScan_2.csv"
$delimPath = "_"
Function GetFileNames([string]$path, [string]$outputFile) {
$list = Get-ChildItem $path -Recurse | where {!$_.PSIsContainer}
$list | Select-Object BaseName | Export-Csv -NoTypeInformation $outputFile
}
GetFileNames $targetPath $outputPath_1
Function SplitFileNames([string]$inputFile, [string]$outputFile) {
$inputData = Get-Content $inputFile | select -Skip 1
$array = @()
$outArray = @()
$inputData | Foreach{
$elements = $_.split($delimPath)
$array += ,@($elements[0], $elements[1], $elements[2])
}
Foreach($value in $array){
$outArray += New-Object PSObject -Property @{
'SiteCode' = $value[0]
'FacilityNumber' = $value[1]
'FileTypeCode' = $value[2]
}
}
$outArray | Select-Object "SiteCode","FacilityNumber","FileTypeCode" | ConvertTo-Csv -NoTypeInformation | % {$_ -replace '"',""} | Out-File $outputFile -fo -en ascii
}
SplitFileNames $outputPath_1 $outputPath_2