patterncsharpMinor
Fastest way to find and replace inside a large amount of files
Viewed 0 times
amountreplacewaylargefilesfastestfindandinside
Problem
Currently I am looping through dependencies of objects, and I am trying to replace GUID (unity object ID's) inside the files.
Right now I am using the following code, any suggestions for improvement ?
Right now I am using the following code, any suggestions for improvement ?
public class SendToThread
{
public int localID = 0;
public string oldID;
public string newID;
public List dependencyAssetPath = new List();
public string applicationPath;
}
public void Charlie()
{
//toUseData is a List
for (int i = 0 ; i = 1)
{
string[] lines = File.ReadAllLines(toUseData[i].applicationPath + toUseData[i].dependencyAssetPath[0]);
for (int e = 0; e < lines.Length; e++)
{
DebugVoid(lines.Length.ToString());
lines[e].Replace(toUseData[i].oldID.ToString(), toUseData[i].newID.ToString());
if (e == lines.Length - 1)
{
DebugVoid(toUseData[i].oldID.ToString() + " " + toUseData[i].newID.ToString());
File.WriteAllLines(toUseData[i].applicationPath + toUseData[i].dependencyAssetPath[i], lines);
}
}
toUseData[i].dependencyAssetPath.RemoveAt(0);
}
}
}Solution
Building on what Heslacher and Magus have said:
You should not make publically accessible non-readonly collections. So I've converted those to readonly collections. It didn't appear to change how you were accessing them anyway.
I've also swapped your outermost for loop for a foreach, which makes the code slightly cleaner and I've swapped your while loop for a foreach, because it appears to just be iterating over the collection. However this assumes that
is a bug and should instead be
If not, then the while loop should be used.
Resulting version:
You should not make publically accessible non-readonly collections. So I've converted those to readonly collections. It didn't appear to change how you were accessing them anyway.
I've also swapped your outermost for loop for a foreach, which makes the code slightly cleaner and I've swapped your while loop for a foreach, because it appears to just be iterating over the collection. However this assumes that
File.WriteAllLines(toUseData[i].applicationPath + toUseData[i].dependencyAssetPath[i], lines);is a bug and should instead be
File.WriteAllLines(toUseData[i].applicationPath + toUseData[i].dependencyAssetPath[0], lines);If not, then the while loop should be used.
Resulting version:
public class SendToThread
{
private int localId = 0;
public int LocalID
{
get{ return localId;}
set{ localId = value;}
}
public string OldID {get; set;}
public string NewID {get; set;}
private readonly List dependencyAssetPath = new List();
public List DependencyAssetPath
{
get{ return dependencyAssetPath;}
}
public string ApplicationPath {get; set;}
}
public void ReadAssets()
{
//toUseData is a List
foreach(var toUse in toUseData)
{
foreach(var assetPath in toUse.DependencyAssetPath)
{
string[] lines = File.ReadAllLines(toUse.ApplicationPath + assetPath);
for (int e = 0; e < lines.Length-1; e++)
{
DebugVoid(lines.Length.ToString());
lines[e].Replace(toUse.OldID, toUse.NewID);
}
DebugVoid(toUse.OldID + " " + toUse.NewID);
File.WriteAllLines(toUse.ApplicationPath + assetPath, lines);
}
toUse.dependencyAssetPath.Clear();
}
}Code Snippets
File.WriteAllLines(toUseData[i].applicationPath + toUseData[i].dependencyAssetPath[i], lines);File.WriteAllLines(toUseData[i].applicationPath + toUseData[i].dependencyAssetPath[0], lines);public class SendToThread
{
private int localId = 0;
public int LocalID
{
get{ return localId;}
set{ localId = value;}
}
public string OldID {get; set;}
public string NewID {get; set;}
private readonly List<string> dependencyAssetPath = new List<string>();
public List<string> DependencyAssetPath
{
get{ return dependencyAssetPath;}
}
public string ApplicationPath {get; set;}
}
public void ReadAssets()
{
//toUseData is a List<SendToThread>
foreach(var toUse in toUseData)
{
foreach(var assetPath in toUse.DependencyAssetPath)
{
string[] lines = File.ReadAllLines(toUse.ApplicationPath + assetPath);
for (int e = 0; e < lines.Length-1; e++)
{
DebugVoid(lines.Length.ToString());
lines[e].Replace(toUse.OldID, toUse.NewID);
}
DebugVoid(toUse.OldID + " " + toUse.NewID);
File.WriteAllLines(toUse.ApplicationPath + assetPath, lines);
}
toUse.dependencyAssetPath.Clear();
}
}Context
StackExchange Code Review Q#64540, answer score: 4
Revisions (0)
No revisions yet.