Created
July 30, 2020 21:32
-
-
Save wonkee-kim/5d8046836282b0fd6468567818f446bf to your computer and use it in GitHub Desktop.
FileUtilities.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.IO; | |
using System.Text.RegularExpressions; | |
public class FileUtilities | |
{ | |
#region SAVE_LOAD_MOVE | |
public static void SaveFile(string data, string path, string name, bool isOverwrite=true) { | |
if(isOverwrite) { | |
SaveFile(data, path, name); | |
} else { | |
if(IsFileExist(path, name)) { | |
Debug.Log($"The File '{name}' is already in the '{path}'. (Timestamp added.)"); | |
string[] names = name.Split('.'); | |
SaveFile(data, path, names[0] + TimeStamp() + "." + names[1]); | |
} else { | |
SaveFile(data, path, name); | |
} | |
} | |
} | |
public static void SaveFile(string data, string path, string name) { | |
if(!IsFolderExist(path)) | |
Directory.CreateDirectory(path); | |
File.WriteAllText(path + "/" + name, data); | |
} | |
public static string ReadFile(string path, string name) { | |
string[] files = Directory.GetFiles(path); | |
for(int i = 0; i < files.Length; i++) { | |
if(files[i].EndsWith(name)) { | |
using(StreamReader streamReader = new StreamReader(files[i])) { | |
return streamReader.ReadToEnd(); | |
} | |
} | |
} | |
Debug.Log("The file doesn't exist."); | |
return null; | |
} | |
public static void WriteFile(string data, string path, string name) { | |
StreamWriter writer = new StreamWriter(path + "/" + name); | |
string[] lines = data.Split('\n'); | |
for(int i = 0; i < lines.Length; i++) { | |
if(i != lines.Length - 1) | |
writer.WriteLine(lines[i]); | |
else | |
writer.Write(lines[i]); | |
} | |
writer.Close(); | |
} | |
public static void MoveFile(string fromPath, string toPath, string name) { | |
if(!IsFileExist(fromPath, name)) { | |
Debug.Log($"The File '{name}' is not exist."); | |
return; | |
} | |
foreach(string file in Directory.GetFiles(fromPath)) { | |
//if(file.Contains(name)) { | |
if(file.EndsWith(name)) { | |
if(IsFolderEmpty(toPath)) | |
Directory.CreateDirectory(toPath); | |
if(IsFileExist(toPath, name)) { | |
Debug.Log($"The File '{name}' is already in the '{toPath}'. (Timestamp added.)"); | |
string[] names = name.Split('.'); | |
File.Move(file, toPath + "/" + names[0] + TimeStamp() + "." + names[1]); | |
} else { | |
File.Move(file, toPath + "/" + name); | |
} | |
} | |
} | |
} | |
#endregion | |
#region FILE_CHECK | |
public static bool IsFileExist(string path, string name) { | |
foreach(string file in Directory.GetFiles(path)) { | |
//if(file.Contains(name)) | |
if(file.EndsWith(name)) | |
return true; | |
} | |
return false; | |
} | |
public static bool IsFolderEmpty(string path) { | |
return Directory.GetFiles(path).Length == 0; | |
} | |
public static bool IsFolderExist(string path) { | |
return Directory.Exists(path); | |
} | |
public static string[] GetFileList(string path) { | |
return Directory.GetFiles(path); | |
} | |
#endregion | |
public static string TimeStamp() { | |
return "_" + System.DateTime.Now.Day.ToString() + "-" + System.DateTime.Now.Month.ToString() + "-" + System.DateTime.Now.Year.ToString() | |
+ "_hr" + System.DateTime.Now.TimeOfDay.Hours.ToString() + "_min" + System.DateTime.Now.TimeOfDay.Minutes.ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment