Idle Banter For non SV and non bike related chat (and the odd bit of humour - but if any post isn't suitable it'll get deleted real quick).![]() |
![]() |
|
Thread Tools |
![]() |
#1 |
Member
Mega Poster
Join Date: Mar 2004
Location: Not in Yorkshire. (Thank God)
Posts: 4,116
|
![]()
OK. to you. I am the anti christ. 10+ years of cutting Java code. You can say what you want about the pros and cons of a language I ai'nt listening . Nuff said on that. I know where there is butter on my bread.
I started off writing APL hieroglyphics in 1977 (Bit like latin, dead language. no need to resurrect). So a code monkey in any language can translate with a bit of time/effort to another. Well I have a little problem. It turns out that a number of (internal to the parent company) clients have been using a particular package to process salary information and for 12 years have not effectively managed the data they generate. First issue to their management they want control. Last week I got my hands on a copy of the application. I now know they need to back up a certain directory structure. But because ii is a full time job to run this particular application. they need to easily revert back to a certain date etc. I therefore had to provide my customer with a tool that will allow them in a (cough spit and wretch) environment to copy workstation (read what you want) windows directory structure to a server which would be robocopied into our corporate all seeing monolith. So don't laugh. I haven't actively cut any code for some years and never before of windows .net framework. Those of you Gates brainwash techy wise, please point out my errors. It is supposed to be a simple programme designed to be installed as a desktop icon. To copy a local directory (with a default location) to a network server, where the destination would be date stamped.What have I missed, what have I done wrong? bearing in mind this is my first finger in the windoze cherry Code is ( advise appreciated) :- Code:
using System; using System.IO; using System.Collections.Generic; using System.Text; namespace fernBackup { class Program { static String source =@"C:\FernTimesheet"; static String destination = "undefined"; static void Main(string[] args) { int status = ProcessArgs(args); if (ProcessArgs(args) == -1) return; if (CheckDirs(source, destination) == -1) return ; DateTime date = DateTime.Now; int year = date.Year; int month = date.Month; int day = date.Day; String dateString = year.ToString() + month.ToString("D2") + day.ToString("D2"); destination = destination + dateString; status = createDir(destination); DirectoryInfo di = new DirectoryInfo(source); WalkDirectoryTree(di); } static void WalkDirectoryTree(System.IO.DirectoryInfo root) { System.IO.FileInfo[] files = null; System.IO.DirectoryInfo[] subDirs = null; // First, process all the files directly under this folder try { files = root.GetFiles("*.*"); } catch (UnauthorizedAccessException e) { Console.WriteLine(e.Message); return; } catch (System.IO.DirectoryNotFoundException e) { Console.WriteLine(e.Message); return; } if (files != null) { foreach (System.IO.FileInfo fi in files) { String newdest = fi.FullName.Replace(source, destination); Console.WriteLine(fi.FullName + " -----> " + newdest); System.IO.File.Copy(fi.FullName, newdest, true); } subDirs = root.GetDirectories(); foreach (System.IO.DirectoryInfo dirInfo in subDirs) { createDir(dirInfo.FullName.Replace(source,destination)); WalkDirectoryTree(dirInfo); } } } static int createDir(String dir) { try { if (!System.IO.Directory.Exists(dir)) { System.IO.Directory.CreateDirectory(dir); return (0); } } catch (Exception ex) { Console.WriteLine("Error creating directory " + dir); Console.WriteLine(ex.Message); return (-1); } return (0); } static int CheckDirs(String source, String destination) { int status=0; String errmesg=""; if (!isValidDirectory(source)) { errmesg = source + " is not a valid directory \r"; Console.WriteLine(errmesg); status = -1; } if (!isValidDirectory(destination)) { errmesg = destination + " is not a valid directory \r"; Console.WriteLine(errmesg); status = -1; } return (status); } static Boolean isValidDirectory(String dirName) { DirectoryInfo di = new DirectoryInfo(dirName); try { // Determine whether the directory exists. if (di.Exists) { return (true); } else return (false); } catch { return (false); } } static int ProcessArgs(string[] args) { String errmesg = "Usage fernBackup [-s source fern directory] -d destination root directory"; int status = 0; if (args.Length == 0) { status = -1; } for (int i = 0; i < args.Length; i+=2) { try { if (args[i].ToString() == "-s") source = args[i + 1].ToString(); if (args[i].ToString() == "-d") destination = args[i + 1].ToString(); } catch{ status = -1; } } if (destination == "undefined" ) { errmesg = "You must define a backup destination root"; status = -1; } char[] backslash = "\\".ToCharArray(); if (destination.LastIndexOfAny(backslash) != destination.Length - 1) { errmesg = @"The destination root must end in a trailing \ character E.G. h:\fernbackups\"; status = -1; } if (status == 0) { return (status); } else { Console.WriteLine(errmesg); return (-1); } } } } Please give me your comments/feedback etc. Like I say. It is a new audience for me.
__________________
Not Grumpy, opinionated. |
![]() |
![]() |
![]() |
#2 |
Guest
Posts: n/a
|
![]()
Have you tried turning it off and on again? Ctrl Alt Del?
(Sorry) |
![]() |
![]() |
#3 |
Member
Mega Poster
Join Date: Jul 2006
Location: Sunny Croydonia
Posts: 6,124
|
![]()
Just gone up and down simultaneously in my opinon o you Tim - down for putting that evil on the web, up because, well looking at it, it seems fine.
But I dont write that stuff, so not 100%. Will email to a friend who does tho - just give me the nod that you dont mind that and I will get it over to him. David
__________________
Sent from my PC NOT using any Tapatalk type rubbish!! █╬╬╬╬(•)i¯i▀▀▀▀▀█Ξ███████████████████████████████) |
![]() |
![]() |
![]() |
#4 |
Member
Mega Poster
Join Date: Mar 2004
Location: Not in Yorkshire. (Thank God)
Posts: 4,116
|
![]()
I would rather ask here where there are a couple of Windows type and get friendly advice, than find a c# forum and get lambasted for not knowing the obvious.
Please ask your friend
__________________
Not Grumpy, opinionated. |
![]() |
![]() |
![]() |
|
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Telecoms types in the org | Mark_h | Thames Valley | 0 | 27-04-09 10:17 AM |
OK Windoze guru's (Excel Question) | timwilky | Idle Banter | 7 | 23-10-07 11:57 AM |
windoze software advice required | timwilky | Idle Banter | 8 | 24-11-06 04:23 PM |
Oil Types??.. Im confused! | valleyboy | Bikes - Talk & Issues | 8 | 01-05-06 11:01 PM |
Can I mix oil types ?? | valleyboy | Bikes - Talk & Issues | 11 | 28-11-05 06:17 PM |