Reputation: 3
Greeting. I am new to coding so these problems I am facing might be quite easy for some of you. I am trying to make a simple tool that takes an exported file and read the price data in the file. but first I need to be able to import the file that is currently in \Marketlogs. All of the files are .txt. I have gotten it to work off a set name but I have not found a way to import an unknown name. here is an example of a file name it might pull "Esoteria-Liquid Ozone-2019.05.04 015804"
I have been following along with IAmTimCorey youtube videos for the basic idea how to read the files but he is using a fixed file name for his videos
https://www.youtube.com/watch?v=cST5TT3OFyg&list=PLsBhi5lzz1f2-AbhurnGazN68UgG4dVwt&index=1
https://www.youtube.com/watch?v=yClSNQdVD7g&list=PLsBhi5lzz1f2-AbhurnGazN68UgG4dVwt&index=2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows;
namespace Eve_market
{
class Program
{
static void Main(string[] args)
{
/// MarketLog
// opens a file from MarketLogs
``` string filePath = @"E:\Users\Hamilton Norris\Documents\EVE\logs\Marketlogs\"; ```
// converts file to a readable csv from text with filteable deta
List<MarketLog> log = new List<MarketLog>();
List<string> lines = File.ReadAllLines(filePath).ToList();
foreach (string line in lines)
{
string[] enteries = line.Split(',');
MarketLog newMarketLog = new MarketLog();
newMarketLog.Price = enteries[0];
newMarketLog.VolReamining = enteries[1];
newMarketLog.TypeID = enteries[2];
newMarketLog.Range = enteries[3];
newMarketLog.OrderID = enteries[4];
newMarketLog.VolEntered = enteries[5];
newMarketLog.MinVolume = enteries[6];
newMarketLog.Bid = enteries[7]; // False = sell order true = buy order
newMarketLog.IssueDate = enteries[8];
newMarketLog.Bidurationd = enteries[9];
newMarketLog.StationID = enteries[10];
newMarketLog.RegionID = enteries[11];
newMarketLog.SolarSystemID = enteries[12];
newMarketLog.Jumps = enteries[13]; // 0 = same system
log.Add(newMarketLog);
}
foreach (var MarketLog in log)
{
Console.WriteLine($"{ MarketLog.Price } {MarketLog.VolReamining } {MarketLog.Bid} ");
}
// MarketLog End
Console.ReadLine();
}
}
}
Upvotes: 0
Views: 67
Reputation: 652
Please try following code: (get the directory and then get the files having matching file pattern. Then load one by one and use your code as usual to read files)
var fileNamePattern = "*.txt";
var dir = new DirectoryInfo(path);
IEnumerable<FileSystemInfo> files = dir.GetFileSystemInfos(fileNamePattern);
files
.ToList()
.ForEach(f=>
{
//Load each file using File system Info:
List<string> lines = File.ReadAllLines(f.FullName).ToList();
//............
});
Upvotes: 0