Here's a C# solution that reads MP3 files from a specified folder, filters them by numeric filename range, and stores their bytes in order:
using System;
using System.Collections.Generic;
using System.IO;
public class AudioFileProcessor
{
public static List<byte[]> GetAudioFilesInOrder(string audioFolder, int start, int stop)
{
// Get all MP3 files in the folder
string[] allFiles = Directory.GetFiles(audioFolder, "*.mp3");
// Collect valid files with numeric filenames within the range
var validFiles = new List<(int Number, string FilePath)>();
foreach (var filePath in allFiles)
{
// Extract filename without extension
string fileName = Path.GetFileNameWithoutExtension(filePath);
// Try to parse the filename as integer
if (int.TryParse(fileName, out int number))
{
// Check if within the desired range
if (number >= start && number <= stop)
{
validFiles.Add((number, filePath));
}
}
}
// Sort files numerically
validFiles.Sort((a, b) => a.Number.CompareTo(b.Number));
// Read each file's bytes and store in list
List<byte[]> audioFiles = new List<byte[]>();
foreach (var file in validFiles)
{
audioFiles.Add(File.ReadAllBytes(file.FilePath));
}
return audioFiles;
}
}
Usage Example:
class Program
{
static void Main()
{
string audioFolder = @"C:\Your\Audio\Folder";
int start = 100;
int stop = 200;
List<byte[]> audioFiles = AudioFileProcessor.GetAudioFilesInOrder(audioFolder, start, stop);
// Now audioFiles contains the bytes of files from 100.mp3 to 200.mp3 in order
}
}
Key Features:
-
File Filtering:
- Only processes files with numeric filenames (e.g., "123.mp3")
- Skips non-numeric filenames (e.g., "abc.mp3" or "123abc.mp3")
-
Range Handling:
- Includes files where the numeric part is between
start and stop (inclusive)
- Works even if
start > stop (returns empty list)
-
Ordering:
- Numerically sorts files based on their numeric filename component
- Ensures the output list maintains the correct sequential order
-
Error Handling:
- Silently ignores invalid filenames (non-numeric names)
- No error handling for file access issues (add try-catch blocks if needed)
Notes:
- This assumes filenames are in the format
[number].mp3 (e.g., "1.mp3", "100.mp3")
- Leading zeros in filenames (e.g., "001.mp3") are handled correctly
- The method returns an empty list if no valid files are found in the specified range