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?

Edited 6 Years Ago by pymatio: n/a

Attachments
using System;
using System.IO;
using System.Collections.Generic;

namespace Chronojohn
{
	class MainClass
	{
		static string BackupLocation = "/home/"+Environment.UserName+"/Desktop/backup";
		static long LastRunTime = 0;
		
		public static void Main(string[] args)
		{
			if (File.Exists(".chronojohn")) {
                string lastrun = File.ReadAllText(".chronojohn");
                LastRunTime = long.Parse(lastrun);
			}
        
			switch (args.Length) {
			case 0:                 
                PrintUsage();
				break;
			case 1:
                CheckDirectory(args[0]);
                File.WriteAllText(".chronojohn", DateTime.Now.ToFileTime().ToString());
                break;
			case 2:
                if (args[0] == "list") {
					ListBackups(args[1]);
                }else if(args[0] == "dir"){
					RestoreDirBackup(args[1]);
				}else{
					PrintUsage();
				}
				
				break;
				
			case 3:
				if (args[0] == "restore") {
					RestoreBackup(args[1], int.Parse(args[2]));
				}else if(args[0] == "revert"){
					RevertBackup(args[1], int.Parse(args[2]));
				}else{
					PrintUsage();
				}
        
				break;
			}
		}
		
		static void CheckDirectory(string directory) {
	        string[] files = Directory.GetFiles(directory);
	        string[] dirs = Directory.GetDirectories(directory);
	        
	        foreach(string file in files) {
				string copy_parent = BackupLocation + Path.GetDirectoryName(file);
				string copy_file = BackupLocation + file;
				long date = File.GetLastWriteTime(file).ToFileTime();
				
				bool file_exists = false;

				if (Directory.Exists(copy_parent)) {
					string search_filter = Path.GetFileName(file) + ".*";
					string[] contents = Directory.GetFiles(copy_parent, search_filter);
					
					if (contents.Length > 0) {
						file_exists = true;
					}
				}

				if (!file_exists || date > LastRunTime) {
					if (!Directory.Exists(copy_parent)) {
						Directory.CreateDirectory(copy_parent);                                 
					}

					File.Copy(file, copy_file + "." + date, true);
				}
	        }
	        
	        foreach(string dir in dirs) {
	                CheckDirectory(dir);
	        }
		}
		
		static void PrintUsage() {
			Console.WriteLine("");
			Console.WriteLine("Chronojohn: a backup tool H. G. Wells would have been proud of.");
			Console.WriteLine("");
			Console.WriteLine("\tadd /some/directory \t\tScan directory and backup changed files");
			Console.WriteLine("\tlist /some/file \t\tList all backups of a given file");
			Console.WriteLine("\trestore /some/file <ver>\tRestore file to specific version");
			Console.WriteLine("\trevert /some/file <ver>\t\tRestore file to specific version and delete all other versions");
			Console.WriteLine("");
		}
		
		static void ListBackups(string filename) {
			if(Path.GetFileName(filename).Length == 0){
				string dir = Path.GetFullPath(filename);
				string[] files = Directory.GetFiles(dir);
				string[] dirs = Directory.GetDirectories(dir);
				
				foreach(string f in files){
					string searchfilter = Path.GetFileName(f) + ".*";
					string[] backup = Directory.GetFiles(Path.GetDirectoryName(BackupLocation + f), searchfilter);
					Console.WriteLine(f);
					for (int i = 0; i < backup.Length; i++) {
						string ext = Path.GetExtension(backup[i]).Substring(1);
						long backup_date = long.Parse(ext);
						string write_time = DateTime.FromFileTime(backup_date).ToString();
						
						Console.WriteLine(string.Format("  {0}: {1}", i + 1, write_time));
					}  
				}
				
				foreach(string d in dirs){
					//ListBackups(d);
				}
				
				return;
			}
					
				
			string file = Path.GetFullPath(filename);
			string copy_parent = Path.GetDirectoryName(BackupLocation + file);
			string search_filter = Path.GetFileName(file) + ".*";
			
			string[] backups = Directory.GetFiles(copy_parent, search_filter);
        
			for (int i = 0; i < backups.Length; i++) {
				string ext = Path.GetExtension(backups[i]).Substring(1);
				long backup_date = long.Parse(ext);
				string write_time = DateTime.FromFileTime(backup_date).ToString();
				
				Console.WriteLine(string.Format("{0}: {1}", i + 1, write_time));
			}                       
		}
		
		static void RestoreBackup(string filename, int version) {
			string file = Path.GetFullPath(filename);
			string copy_parent = Path.GetDirectoryName(BackupLocation + file);

			string[] backups = Directory.GetFiles(copy_parent, Path.GetFileName(file) + ".*");

			if (backups.Length < version) {
				Console.WriteLine( string.Format("Can't restore version {0} - maximum is {1}", version, backups.Length));
				return;
			}

			string chosen = backups[version - 1];

			File.Copy(chosen, file, true);

			File.SetLastWriteTime(file, File.GetLastWriteTime(chosen));
		}
		
		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);
			}
		}
		
		static void RevertBackup(string filename, int version) {
			string file = Path.GetFullPath(filename);
			string copy_parent = Path.GetDirectoryName(BackupLocation + file);

			string[] backups = Directory.GetFiles(copy_parent, Path.GetFileName(file) + ".*");

			if (backups.Length < version) {
				Console.WriteLine( string.Format("Can't restore version {0} - maximum is {1}", version, backups.Length));
				return;
			}

			string chosen = "";
			for(int i=0; i<backups.Length; i++){
				if(i==version-1){
					chosen = backups[i];
				}else{
					File.Delete(backups[i]);
				}
			}

			File.Copy(chosen, file, true);

			File.SetLastWriteTime(file, File.GetLastWriteTime(chosen));
		}		
	}
}

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().

Edited 6 Years Ago by apegram: n/a

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));
This article has been dead for over six months. Start a new discussion instead.