Что касается того, что вы ищете, это кажется немного не в порядке. Возьмите ваш код и настройте его только на go для основной c части получения файла и списка в файл CSV.
# Test .wav Files
Add-Type -AssemblyName presentationCore
$mediaPlayer = New-Object system.windows.media.mediaplayer
$myDirectory = "D:\Temp\ParentFolder"
$OUTPUT_CSV = "D:\Temp\ParentFolder\Test_Audio.csv"
<#
If you are after 2 wav files per folder, why are you only asking for the count here?
Each folder may have a different count and file name, right?
#>
# $numFiles = (Get-ChildItem -Path $myDirectory -Filter *.WAV).Count
foreach (
$Dir in Get-ChildItem -Path $myDirectory -Recurse -Filter '*.wav' |
select -ExpandProperty DirectoryName -Unique
)
{
$numFiles = (Get-ChildItem -Path $Dir -Filter *.WAV).Count
Read-host -prompt "
`nThe folder $Dir contains a total of $numFiles wav files.`n
Press ENTER to start the Testing`n
These .wav files will be played for
approximately 15 secs for validation. You may hear these play on your
laptop/computer (There may be a slight delay due to the network)
"
Get-ChildItem -Path $Dir |
Get-Random -Count 2
}
<#
Also, note that gets random will not always give you that could be depending on the number for total files you may have in a folder and when/where you call it.
Example:
Each of my folders only have 3 wav files.
# Results
The folder D:\Temp\ParentFolder contains a total of 3 wav files.
Press ENTER to start the Testing
These .wav files will be played for
approximately 15 secs for validation. You may hear these play on your
laptop/computer (There may be a slight delay due to the network)
:
Directory: D:\Temp\ParentFolder
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 12-Feb-20 12:37 ChildFolder
-a---- 28-Aug-07 13:08 2646058 ThumperLong.wav
The folder D:\Temp\ParentFolder\ChildFolder contains a total of 3 wav files.
Press ENTER to start the Testing
These .wav files will be played for
approximately 15 secs for validation. You may hear these play on your
laptop/computer (There may be a slight delay due to the network)
:
Directory: D:\Temp\ParentFolder\ChildFolder
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 23-Apr-99 18:22 668440 WELCOM98.WAV
d----- 12-Feb-20 12:37 GrandchildFolder
The folder D:\Temp\ParentFolder\ChildFolder\GrandchildFolder contains a total of 3 wav files.
Press ENTER to start the Testing
These .wav files will be played for
approximately 15 secs for validation. You may hear these play on your
laptop/computer (There may be a slight delay due to the network)
:
Directory: D:\Temp\ParentFolder\ChildFolder\GrandchildFolder
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 28-Aug-07 13:08 2646058 ThumperLong.wav
-a---- 28-Aug-07 13:05 683670 ThumperShort.wav
So, to avoid this, do this...
#>
foreach (
$Dir in Get-ChildItem -Path $myDirectory -Recurse -Filter '*.wav' |
select -ExpandProperty DirectoryName -Unique
)
{
$numFiles = (Get-ChildItem -Path $Dir -Filter *.WAV).Count
Read-host -prompt "
`nThe folder $Dir contains a total of $numFiles wav files.`n
Press ENTER to start the Testing`n
These .wav files will be played for
approximately 15 secs for validation. You may hear these play on your
laptop/computer (There may be a slight delay due to the network)
"
Get-ChildItem -Path $Dir -file |
Get-Random -Count 2
}
<#
# Results
The folder D:\Temp\ParentFolder contains a total of 3 wav files.
Press ENTER to start the Testing
These .wav files will be played for
approximately 15 secs for validation. You may hear these play on you
r
laptop/computer (There may be a slight delay due to the network)
:
Directory: D:\Temp\ParentFolder
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 23-Apr-99 18:22 668440 WELCOM98.WAV
-a---- 28-Aug-07 13:08 2646058 ThumperLong.wav
The folder D:\Temp\ParentFolder\ChildFolder contains a total of 3 wav files.
Press ENTER to start the Testing
These .wav files will be played for
approximately 15 secs for validation. You may hear these play on your
laptop/computer (There may be a slight delay due to the network)
:
Directory: D:\Temp\ParentFolder\ChildFolder
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 28-Aug-07 13:05 683670 ThumperShort.wav
-a---- 28-Aug-07 13:08 2646058 ThumperLong.wav
The folder D:\Temp\ParentFolder\ChildFolder\GrandchildFolder contains a total of
3 wav files.
Press ENTER to start the Testing
These .wav files will be played for
approximately 15 secs for validation. You may hear these play on you
r
laptop/computer (There may be a slight delay due to the network)
:
Directory: D:\Temp\ParentFolder\ChildFolder\GrandchildFolder
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 28-Aug-07 13:05 683670 ThumperShort.wav
-a---- 28-Aug-07 13:08 2646058 ThumperLong.wav
Since you are looping thru file, put the Export -Append in the loop, not outside. THat error you are gettings is because the file creation is not valid.
#>
foreach (
$Dir in Get-ChildItem -Path $myDirectory -Recurse -Filter '*.wav' |
select -ExpandProperty DirectoryName -Unique
)
{
$numFiles = (Get-ChildItem -Path $Dir -Filter *.WAV).Count
Read-host -prompt "
`nThe folder $Dir contains a total of $numFiles wav files.`n
Press ENTER to start the Testing`n
These .wav files will be played for
approximately 15 secs for validation. You may hear these play on your
laptop/computer (There may be a slight delay due to the network)
"
Get-ChildItem -Path $Dir |
Select-Object -Property Directory, PSChildName, FullName |
Get-Random -Count 2 |
Export-Csv -NoTypeInformation -Path $OUTPUT_CSV -Append
}
Import-Csv -Path $OUTPUT_CSV
# Results
<#
Directory PSChildName FullName
--------- ----------- --------
D:\Temp\ParentFolder ParentWELCOM98.WAV D:\Temp\ParentFolder\ParentWELCOM98.WAV
D:\Temp\ParentFolder ParentThumperLong.wav D:\Temp\ParentFolder\ParentThumperLong.wav
D:\Temp\ParentFolder\ChildFolder ChildThumperShort.wav D:\Temp\ParentFolder\ChildFolder\ChildThumperShort.wav
D:\Temp\ParentFolder\ChildFolder ChildThumperLong.wav D:\Temp\ParentFolder\ChildFolder\ChildThumperLong.wav
D:\Temp\ParentFolder\ChildFolder\GrandchildFolder GrandchildWELCOM98.WAV D:\Temp\ParentFolder\ChildFolder\GrandchildFolder\GrandchildWELCOM98.WAV
D:\Temp\ParentFolder\ChildFolder\GrandchildFolder GrandchildThumperShort.wav D:\Temp\ParentFolder\ChildFolder\GrandchildFolder\GrandchildThumperShort.wav
#>
"
`nTesting Complete and is saved here. The extract will automatically open now.
"
# Open file - csv in MS Excel
Start-Process -FilePath 'excel.exe' -ArgumentList $OUTPUT_CSV
Хотя я не совсем уверен, почему подробный текст подскажите ваши усилия, поскольку, если у них есть файл Excel, закрытие, которое завершает сценарий использования, и вы не показали код, который действительно что-то делает после этого. Итак, этот последний Read-Host избыточен.