I have been writing a program to backup files (attached).
I've tried to add a new option that restores the last backup of all the files in a directory:

static void RestoreDirBackup(string filename) {
        string file = Path.GetFullPath(filename);

        string[] backups = Directory.GetFiles(BackupLocation + file);

	if(!Directory.Exists(file)){
		Directory.CreateDirectory(file);
	}	
			
	string lastfile = backups[0];
	string newestfile = backups[0];
	long newestdate = long.Parse(Path.GetExtension(backups[0]));

			
	List<string> lastrev = new List<string>();
			
	for(int y=0; y<backups.Length; y++){
		string f = backups[y];
		if(f == lastfile){
			long thisdate = long.Parse(Path.GetExtension(f));
			if(thisdate > newestdate){
				newestdate = thisdate;
				newestfile = f;
			}
		}else{
			lastrev.Add(newestfile);
			
			newestdate = long.Parse(Path.GetExtension(f));
			newestfile = f;
		}
	}	
					
	foreach(string f in lastrev){
		File.Copy(f, file+Path.GetFileName(f), true);
        }
}

When I try to run it it gives this error:

[matio@myhost Debug]$ mono Chronojohn.exe dir ~/test2

Unhandled Exception: System.FormatException: Input string was not in the correct format
  at System.Int64.Parse (System.String s) [0x00000] in <filename unknown>:0 
  at Chronojohn.MainClass.RestoreDirBackup (System.String filename) [0x00000] in <filename unknown>:0 
  at Chronojohn.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0

What's wrong with the input of long.Parse?

Recommended Answers

All 2 Replies

I assume the files have numeric extensions?

Path.GetExtension(fileName) is going to give you the extension, including the period. You're going to want to strip off the first character of the result before going to long.Parse().

I assume the files have numeric extensions?

Path.GetExtension(fileName) is going to give you the extension, including the period. You're going to want to strip off the first character of the result before going to long.Parse().

Thanks, I solved it using:

long.Parse(Path.GetExtension(f).Substring(1));
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.