PowerShell script to export a list of file names only with no file extension then output to a text file separate for each directory -



PowerShell script to export a list of file names only with no file extension then output to a text file separate for each directory -

i need powershell script export list of file names no file extension output text file separate each sub-folder. need specify parent directory in script, powershell needs go off , create separate text file each sub folder using the name of sub folder text file name (with no spaces , lowercase). in each sub-folder based text file created, have list of file names contained in each sub-folder 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 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 more time on this, script added below previous attempt. seems close time, writing text file @ end not 100%, using out-file before leaving blank line @ bottom of each text file didn't want. why switched [system.io.file]::writealltext , [system.io.file]::appendalltext, each of these have idiosyncrasies don't need. in text file need list of files in 1 column 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 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) }

what chance show off richness of powershell , .net working together.

first off, easiest way total names of subfolders of folder line this:

$folders = (get-childitem -directory -recurse m:\music).fullname

-directory limits returned array of directoryinfo object. taking advantage of powershell's ability homecoming fullname property, can homecoming array of folder paths in 1 statement

we can create files listing songs in directory few more lines of code:

foreach ($folder in $folders) { $filebasenames = (get-childitem $folder\*.mp3).fullname | % {[system.io.path]::getfilenamewithoutextension($_)} $catalogfilename = (split-path $folder -leaf) -replace ' ','' if ($filebasenames) {set-content -path $folder\$catalogfilename.txt -value $filebasenames} }

we visit each subfolder , every subfolder we:

get array of mp3 file names (without file extension) in current directory. (i love .net's getfilenamewithoutextension method @ times this. create file name our catalog isolating current folder name (split-path -leaf) , sucking out spaces replacing each space empty string. finally if our list of songs isn't empty save off set-content, great way save list of anything.

it never ceases amaze me how powerfull , concise powershell is. line creating $filebasenames may little gnarly some, easy plenty thing turn foreach-object build easy ready multiline foreach clause.

powershell powershell-v2.0 powershell-v3.0 powershell-ise

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -