I want to extract Guid from this below string array and want output like this:
Output:Log.txt,Logging.txt
string[] str = new string[] { "1250a2d5-cd40-4bcc-a979-9d6f2cd62b9fLog.txt", "bdb31966-e3c4-4344-b02c-305c0eb0fa0aLogging.txt" }; //file name are Log.txt and Logging.txt
List<string> list= new List<string>();
foreach (var item in str)
{
if (!string.IsNullOrEmpty(item))
{
list.Add(); // here i want to store Log.txt and Logging.txt
}
}
How to do this??
var str = new string[] { "1250a2d5-cd40-4bcc-a979-9d6f2cd62b9fLog.txt", "bdb31966-e3c4-4344-b02c-305c0eb0fa0aLogging.txt" }; //file name are Log.txt and Logging.txt
var arr = str.Select(s=>s.Substring(36)).Where(s=>s.Any()).ToArray();
You can see it in action here: https://dotnetfiddle.net/eHy6Fo
Related
I have the following code which will return the filename of all files within my directory and sub-directories of documents. I would now want to compare files which have identical filenames and see if their size is the same. I am not sure on how I would be able to add duplicate file names to a class and to then compare the filesize of these.
Below is the code I have working so far
private static void ListAllDuplicateFiles()
{
string[] files = Directory.GetFiles(rootPath, "*.*", SearchOption.AllDirectories);
List<FileDetail> fileDetails = new List<FileDetail>();
foreach (string file in files)
{
FileInfo fileInfo = new FileInfo(file);
FileDetail fileDetail = new FileDetail(fileInfo);
fileDetails.Add(fileDetail);
}
foreach (FileDetail fileDetail in fileDetails)
{
Console.WriteLine(fileDetail.Filename);
}
Console.ReadLine();
}
You can create a list of string to store the names that are duplicate, then use the list of string to do the comparison that you want
private static void ListAllDuplicateFiles()
{
string[] files = Directory.GetFiles(rootPath, "*.*", SearchOption.AllDirectories);
List<string> duplicates = new List<string>();
List<FileDetail> fileDetails = new List<FileDetail>();
foreach (string file in files)
{
FileInfo fileInfo = new FileInfo(file);
FileDetail fileDetail = new FileDetail(fileInfo);
fileDetails.Add(fileDetail);
if(fileDetails.Select(f=>f.Filename).Contains(file))
{
duplicates.Add(file);
}
}
foreach(string duplicate in duplicates)
{
//Do what you want with the list of duplicated file details
var duplicateFileDetails = fileDetails.Where(f=>f.Filename == duplicate).ToList();
}
foreach (FileDetail fileDetail in fileDetails)
{
Console.WriteLine(fileDetail.Filename);
}
Console.ReadLine();
}
Was given this by a coworker but I need just file names:
private List<string> getWavFileList()
{
string path = #"c\test automation\wave files";
string[] files = Directory.GetFiles(path, "*.wav");
List<string> list = new List<string>(files);
return list;
}
The output list contains the path and extension and I need the file name only. I was working on my own method but can't get it to compile:
private List<string> getWavFileList()
{
StringBuilder builder = new StringBuilder();
string path = #"c\test automation\wave files";
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] smFiles = di.GetFiles("*.wav");
foreach (FileInfo fi in smFiles)
{
builder.Append(Path.GetFileNameWithoutExtension(fi.Name));
builder.Append(", ");
}
string files = builder.ToString();
List list = new List<string>(files);
return list;
I'd suggest modifying to something like the following;
private List<string> getWavFileList()
{
string path = #"c:\test automation\wave files";
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] smFiles = di.GetFiles("*.wav");
List<string> list = new List<string>(smFiles.Select(f => Path.GetFileNameWithoutExtension(f.Name)));
return list;
}
In the first solution replace this line
List<string> list = new List<string>(files);
with this:
return files.Select(Path.GetFileNameWithoutExtension).ToList();
This requires using of System.Linq.
I don't know why you are concatenating strings with comma, i thought you wanted a list:
private List<string> getWavFileList()
{
return Directory.EnumerateFiles(#"c\test automation\wave files", "*.wav")
.Select(System.IO.Path.GetFileNameWithoutExtension)
.ToList();
}
I'm trying to read a text file, which contains proxys into a 2 dimensional array.
The text file looks like the following:
00.00.00.00:80
00.00.00.00:80
00.00.00.00:80
00.00.00.00:80
00.00.00.00:80
How could I seperate the ip from the port?`
So my array would look like the following:
[00.00.00.00][80]
Current code:
public void readProxyList(string FileName)
{
using (StreamReader sr = new StreamReader(FileName, Encoding.Default))
{
string text = sr.ReadToEnd();
string[] lines = text.Split('\r');
foreach (string s in lines)
{
}
}
}
If you are not expecting the file to be too large you could use File.ReadAllLines to read in each line. Then to split, just use String.Split with ':' as your token.
Example:
var lines = File.ReadAllLines(FileName));
var array = new string[lines.Length,2];
for(int i=0; i < lines.Length; i++)
{
var temp = lines[i].Split(':');
array[i,0] = temp[0];
array[i,1] = temp[1];
}
Edit
If you expect that the file may be large, instead of using ReadAllLines you can use File.ReadLines. This method returns an IEnumerable<string> and will not read the whole file at once. In this case, I would probably opt away from the 2d array and make a simple class (call it IpAndPort or something like that) and create a list of those.
Example:
public sealed class IpAndPort
{
public string Ip { get; private set; }
public string Port { get; private set; }
public IpAndPort (string ip, string port)
{
Ip = ip;
Port = port;
}
}
var list = new List<IpAndPort>();
foreach(var line in File.ReadLines(FileName))
{
var temp = line.Split(':');
list.Add(new IpAndPort(temp[0], temp[1]);
}
Try this:
public IEnumerable<string> GetProxyList(string FileName)
{
string[] allLines = File.ReadAllLines(FileName);
var result = new List<string>(allLines.Length);
foreach (string line in allLines)
{
var splittedLine = line.Split(':');
result.Add($"[{splittedLine[0]}][{splittedLine[1]}]");
}
return result;
}
I am trying to do a foreach loop where it would select each string in my array and add it to the path which is a string and stores the path for the image, this is what I have so far:
string[] Images = new string[] { "Star_00001.png", "Star_00002.png", "Star_00003.png" };
string Path = "Assets/Images/";
if (LevelUp)
{
foreach()
{
}
}
As you can see I want the foreach loop to go through each string in the Images array, for each string in the Images array I want it to be added to the path string so the end result for example would be "Assets/Images/Star_00001.png"
Does anyone know how I should do this?
A foreach loop has the syntax:
foreach(T ident in collection) {
with T the type of the elements, ident the name of the variable and collection an object that supports the IEnumerable interface.
So you can implement:
string[] images = new string[] { "Star_00001.png", "Star_00002.png", "Star_00003.png" };
string path = "Assets/Images/";
if (LevelUp) {
foreach(string file in images) {
string thepath = Path.Combine(path,file);
//do something with file or thepath, like
Console.WriteLine(thepath);
}
}
A final note is that C#'s consensus is that variables start with a lowercase and type names with an uppercase.
It is not recommended to generate file paths by using string concatenation. The recommended way is to use Path.Combine which is provided in System.IO. Consider the example below:
string[] Images = new string[] { "Star_00001.png", "Star_00002.png", "Star_00003.png" };
string path = "Assets/Images/";
if (LevelUp)
{
foreach (string s in Images)
{
// You can store result in an array or sth depending on what you
// are trying to achieve
string result = Path.Combine(path, s);
}
}
string[] Images = new string[] { "Star_00001.png", "Star_00002.png", "Star_00003.png" };
string path = "Assets/Images/";
if (LevelUp)
Images = Images.Select(image => Path.Combine(path, image)).ToArray();
I have list of string and each of that strings in list look like this: sim_pin: 1234. List have 24 strings, and I wanna to get each of that strings, separate string where separator will be : ( : and space), and save to list only that part who is right from separator.
EDIT:
Here is my code
string url = #"E:\Sims.log";
public static IEnumerable<DiverGate> GetData(string url)
{
Stream stream = File.Open(url, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using (StreamReader sr = new StreamReader(stream))
{
string str = sr.ReadToEnd();
string[] lines = Regex.Split(str, "\r\n");
List<string> lista = new List<string>();
foreach (string line in lines)
{
lista.Add(line);
}
List<string> header = lista.GetRange(0, 23);
//I stop here and im out of idea
}
}
Something like this should work:
where input is your original list of strings
List<string> output = new List<string>();
input.ForEach(x=> output.Add(x.Split(new[] {": "},StringSplitOptions.None).Last()));
var List1 = new List<string>{"sim_pin: 1234", "sim_pin: 2345", "sim_pin: 3456"};
var List2 = new List<string>();
foreach (var s in List1) {
var ns = s.Split(':')[1].TrimStart(' ');
List2.Add(ns);
}
try this code:
for ( int i =0; i< yourList.Count(); i++) {
string s = yourList[i];
int i = s.indexOf(":");
s = s.Substring (i);
yourList.Insert(i, s);
}