I need a PowerShell script to export a list of file names only with no file extension then output to a text file separate for each sub-folder. I need to specify a parent directory in the script, then PowerShell needs to go off and create a separate text file for each sub folder using the the name of the sub folder for the text file name (with no spaces and lowercase). Then in each sub-folder based text file created, have a list of file names that are contained in each sub-folder with no file extension.
$files = Get-ChildItem -Path "M:\Music" -Recurse `
| Where-Object {`
$_.DirectoryName -notlike "*Other\Children" -and `
$_.DirectoryName -notlike "*Other\Numbers" -and `
$_.Extension -eq ".mp3"}
#Now loop through all the subfolders
$folder = $files.PSisContainer
ForEach ($Folder in $files)
{
$ParentS = ($_.Fullname).split("\")
$Parent = $ParentS[@($ParentS.Length - 2)]
Select-Object BaseName > C:\Users\me\Documents\$parent.txt
}
OK, spent some more time on this, script is added below to the previous attempt. Seems I am very close this time, however writing to the text file at the end is not 100%, I was using Out-File before which was leaving a blank line at the bottom of each text file which I didn't want. Why I switched to [system.io.file]::WriteAllText and [system.io.file]::AppendAllText, however each of these have their idiosyncrasies which don't do what I need. In the Text file I need the list of files in one column with no blank lines.
$files = Get-ChildItem -Path "M:\Music" -Recurse `
| Where-Object {`
$_.DirectoryName -notlike "*Other\Children" -and `
$_.DirectoryName -notlike "*Other\Numbers" -and `
$_.Extension -eq ".mp3"}
#Now loop through all the subfolders
$folder = $files.Directory
ForEach ($Folder in $files)
{
$ParentS = ($folder.Fullname).split("\")
$ParentT = $ParentS[(@($ParentS.Length - 2))]
$Parent = $ParentT.replace(' ','')
[system.io.file]::WriteAllText("C:\Users\me\Documents\$parent.txt", $folder.BaseName, [System.Text.Encoding]::Unicode)
}