PowerShell script to export a list of file names only without a file extension and then output to a text file separate for each directory

I need a PowerShell script to export a list of file names only without a file extension, and then to output to a text file, separate for each subfolder. I need to specify the parent directory in the script, then PowerShell needs to be disabled and create a separate text file for each subfolder using the subfolder name for the text file name (without spaces or lowercase letters). Then, in each created text file based on subfolders, there is a list of file names that are contained in each subfolder without a 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
}

, , script . , , 100%, Out-File, , . [system.io.file]:: WriteAllText [system.io.file]:: AppendAllText, , , . .

$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)
}
+4
1

Powershell .NET, .

-, - :

$folders = (Get-ChildItem -Directory -Recurse M:\Music).FullName

-Directory , DirectoryInfo. Powershell FullName,

, , :

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}
}

:

  • MP3 ( ) . ( .NET GetFileNameWithoutExtension .
  • , (Split-Path -Leaf) , .
  • , , Set-Content, .

, Powershell. , $fileBaseNames, , foreach-object foreach.

+11

Source: https://habr.com/ru/post/1545060/


All Articles