Author Topic: Windows - Command line and Dialog driven command  (Read 23084 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
Windows - Command line and Dialog driven command
« on: September 26, 2008, 12:24:07 PM »
What would be the best way to set up a routine that I wanted to be driven by the command line, and have a dialog version.  From what I have gathered, it looks like you have to choose one or the other, so would that mean that I would have to just write the code, and then copy the guts of it into the other style?  Thought I would get some other insight.  Thanks.

I'm off to do some more research.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: Windows - Command line and Dialog driven command
« Reply #1 on: September 26, 2008, 01:54:13 PM »
More information required. For instance, when you say 'command line', are you talking AutoCAD or system?
Me

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Windows - Command line and Dialog driven command
« Reply #2 on: September 26, 2008, 02:03:20 PM »
This will be run outside of Acad.  It is a back-up routine that I'm writting.  I want to be able to call it from the command line, and be able to call it with a GUI.  I was figuring that if I can call it from the command line, then I can run it every night at work to make sure I have the current files to work from.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Windows - Command line and Dialog driven command
« Reply #3 on: September 26, 2008, 05:33:47 PM »
I wrote the code so it could be easily called either way.  I just haven't found a way to do it yet.  I don't want to have a dll that is used by two exe files.  I'm still trying to see what I can find.  Here is the, simple, code.

Code: [Select]
void Back_up ( string toPath, string fromPath ) {
   
    DirectoryInfo tempDi;
   
    if ( string.IsNullOrEmpty( toPath ) ) return;
    if ( string.IsNullOrEmpty( fromPath ) ) return;
   
    int fromDirLen = fromPath.Length;
   
    tempDi = new DirectoryInfo( toPath );
    if ( !tempDi.Exists ) return;
    tempDi = new DirectoryInfo( fromPath );
    if ( !tempDi.Exists ) return;
   
    FileInfo[] FiAr = tempDi.GetFiles(
                                        ( string.IsNullOrEmpty( SchPatTbox.Text ) ? "*.*" : SchPatTbox.Text ),
                                        SearchOption.AllDirectories
                                    );
   
    StringBuilder sb = new StringBuilder( "BackupLog v0.1" );
    sb.AppendLine( System.Environment.NewLine );
    sb.AppendLine( "Legend:" );
    sb.AppendLine( "N- : New file" );
    sb.AppendLine( "O- : Overwrote existing file" );
    sb.AppendLine( "I- : Ignored file");
    sb.AppendLine( System.Environment.NewLine );
    sb.AppendLine( "Begin report:" );
    foreach ( FileInfo fi in FiAr ) {
       
        string tempPath = fi.FullName;
        tempPath = toPath + tempPath.Substring( fromDirLen );
        FileInfo tempFi = new FileInfo( tempPath );
        tempDi = tempFi.Directory;
       
        if ( !tempDi.Exists ) {
            tempDi.Create();
            fi.CopyTo( tempPath );
            sb.AppendLine( "N- " + tempPath );
        }
        else if ( !tempFi.Exists ) {
            fi.CopyTo( tempPath );
            sb.AppendLine( "N- " + tempPath );
        }
        else if ( IsLaterThan( fi.LastWriteTime, tempFi.LastWriteTime ) ) {
            tempFi.Delete();
            fi.CopyTo( tempPath );
            sb.AppendLine( "O- " + tempPath );
        }
        else
            sb.AppendLine( "I- " + tempPath );
    }
    using ( StreamWriter sw = new StreamWriter( toPath + "\\BackUpLog.log" ) ) {
        sw.Write( sb.ToString() );
    }
}

bool IsLaterThan ( DateTime thisDate, DateTime thatDate ) {
   
    if ( !thisDate.Year.Equals( thatDate.Year ) ) {
        if ( thisDate.Year > thatDate.Year )
            return true;
        return false;
    }
   
    if ( !thisDate.Month.Equals( thatDate.Month ) ) {
        if ( thisDate.Month > thatDate.Month )
            return true;
        return false;
    }
   
    if ( !thisDate.Day.Equals( thatDate.Day ) ) {
        if ( thisDate.Day > thatDate.Day )
            return true;
        return false;
    }
   
    if ( !thisDate.Hour.Equals( thatDate.Hour ) ) {
        if ( thisDate.Hour > thatDate.Hour )
            return true;
        return false;
    }
   
    if ( !thisDate.Minute.Equals( thatDate.Minute ) ) {
        if ( thisDate.Minute > thatDate.Minute )
            return true;
        return false;
    }
   
    if ( !thisDate.Second.Equals( thatDate.Second ) ) {
        if ( thisDate.Second > thatDate.Second )
            return true;
        return false;
    }
   
    if ( !thisDate.Millisecond.Equals( thatDate.Millisecond ) ) {
        if ( thisDate.Millisecond > thatDate.Millisecond )
            return true;
        return false;
    }
   
    return false;
}
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Windows - Command line and Dialog driven command
« Reply #4 on: September 29, 2008, 02:52:12 PM »
Okay; I got it to work.  I had some coding backwards, so it was coping from the 'to' directory instead of from the 'from' directory.  Here is code ( not the form code, but that could added it wanted ).

Code: [Select]
public static void Back_up ( string fromPath, string toPath, string SrchPat ) {

DirectoryInfo tempDi;

if ( string.IsNullOrEmpty( toPath ) ) return;
if ( string.IsNullOrEmpty( fromPath ) ) return;

int fromDirLen = fromPath.Length;

tempDi = new DirectoryInfo( toPath );
if ( !tempDi.Exists ) return;
tempDi = new DirectoryInfo( fromPath );
if ( !tempDi.Exists ) return;

FileInfo[] FiAr = tempDi.GetFiles( SrchPat, SearchOption.AllDirectories );

StringBuilder sb = new StringBuilder( "BackupLog v0.1" );
sb.AppendLine( System.Environment.NewLine );
sb.AppendLine( "Legend:" );
sb.AppendLine( "N- : New file" );
sb.AppendLine( "O- : Overwrote existing file" );
sb.AppendLine( "I- : Ignored file");
sb.AppendLine( System.Environment.NewLine );
sb.AppendLine( "Begin report:" );
foreach ( FileInfo fi in FiAr ) {

string tempPath = fi.FullName;
tempPath = toPath + tempPath.Substring( fromDirLen );
FileInfo tempFi = new FileInfo( tempPath );
tempDi = tempFi.Directory;

if ( !tempDi.Exists ) {
tempDi.Create();
fi.CopyTo( tempPath );
sb.AppendLine( "N- " + tempPath );
}
else if ( !tempFi.Exists ) {
fi.CopyTo( tempPath );
sb.AppendLine( "N- " + tempPath );
}
else if ( IsLaterThan( fi.LastWriteTime, tempFi.LastWriteTime ) ) {
tempFi.Delete();
fi.CopyTo( tempPath );
sb.AppendLine( "O- " + tempPath );
}
else
sb.AppendLine( "I- " + tempPath );
}
using ( StreamWriter sw = new StreamWriter( toPath + "\\BackUpLog.log" ) ) {
sw.Write( sb.ToString() );
}
}
Code: [Select]
public static bool IsLaterThan ( DateTime thisDate, DateTime thatDate ) {

if ( !thisDate.Year.Equals( thatDate.Year ) ) {
if ( thisDate.Year > thatDate.Year )
return true;
return false;
}

if ( !thisDate.Month.Equals( thatDate.Month ) ) {
if ( thisDate.Month > thatDate.Month )
return true;
return false;
}

if ( !thisDate.Day.Equals( thatDate.Day ) ) {
if ( thisDate.Day > thatDate.Day )
return true;
return false;
}

if ( !thisDate.Hour.Equals( thatDate.Hour ) ) {
if ( thisDate.Hour > thatDate.Hour )
return true;
return false;
}

if ( !thisDate.Minute.Equals( thatDate.Minute ) ) {
if ( thisDate.Minute > thatDate.Minute )
return true;
return false;
}

if ( !thisDate.Second.Equals( thatDate.Second ) ) {
if ( thisDate.Second > thatDate.Second )
return true;
return false;
}

if ( !thisDate.Millisecond.Equals( thatDate.Millisecond ) ) {
if ( thisDate.Millisecond > thatDate.Millisecond )
return true;
return false;
}

return false;
}

And how it knows what to do when its called.
Code: [Select]
switch ( args.Length ) {
case 0:
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
break;
case 2:
MainForm.Back_up( args[0], args[1], "*.*" );
break;
case 3:
MainForm.Back_up( args[0], args[1], args[2] );
break;
default:
Console.WriteLine( "Arguments: <Copy from path> <Copy to path> <Search pattern ( optional )>" );
break;
}
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: Windows - Command line and Dialog driven command
« Reply #5 on: September 30, 2008, 06:20:58 PM »
Ahh...Tim...what's the purpose of your IsLaterThan() function?
Me

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: Windows - Command line and Dialog driven command
« Reply #6 on: September 30, 2008, 06:38:16 PM »
Also, have you thought of using xcopy, robocopy or SyncToy 2.0?
Me

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Windows - Command line and Dialog driven command
« Reply #7 on: September 30, 2008, 07:15:00 PM »
Ahh...Tim...what's the purpose of your IsLaterThan() function?
Just to make sure that the file that is going to be copied over is later than the one that is there now.  If it isn't a newer version, then don't copy it.  This is a one way copy, not syncing.

Also, have you thought of using xcopy, robocopy or SyncToy 2.0?
Nope.  I knew there were things out there, but I wanted to see how hard it was to code something up.  Plus I didn't know if there was a dialog version of things, as I didn't search the net for one.  :roll:  I'll have a look at these.  Thanks Glenn.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

TonyT

  • Guest
Re: Windows - Command line and Dialog driven command
« Reply #8 on: September 30, 2008, 11:01:47 PM »

Ahh...Tim...what's the purpose of your IsLaterThan() function?

Just to make sure that the file that is going to be copied over is later than the one that is there now.  If it isn't a newer version, then don't copy it.  This is a one way copy, not syncing.


Hi Tim.

I think what Glenn was getting at is, why write a function that is essentially a reinvention of the DateTime's overloaded > (is-greater-than) operator ?

Those who are new to OOP and C# often find themselves reinventing things that already exist, merely because they did not know they were there. So, when one is just starting out, they can probably avoid reinventing many things that already exist if they were to just spend some time becoming more familiar with some of the basic principles and concepts of an OOP language and C# in particular, like operators and operator overloading.

In a nutshell, operator overloading allows a class to supply the code that is used to perform comparisons when instances of the class appear on both sides of a langauge operator like >, ==, <, and so on (e.g., as in 'Today > Yesterday', where Today and Yesterday are both DateTimes).

In other words, all of that code in your IsLaterThan() method is functionally equivalent to this:

Code: [Select]

public bool IsLaterThan( DateTime thisDate, thatDate b )
{
    return thisDate > thatDate;
}


And I think a little common sense allows us to realize that a DateTime is 'later than' another DateTime, if it is greater than the other DateTime.

The > operator overload for the DateTime class simply compares its internal double (which represents the date and time) to the internal double of the other DateTime operand.


tjr

  • Guest
Re: Windows - Command line and Dialog driven command
« Reply #9 on: October 01, 2008, 12:27:53 AM »
Tim:

While TonyT's explanation is correct, it doesn't provide the correct solution to your problem. When doing a comparison of a local copy to a remote/backup copy the only correct what to do it is compare a SHA1 hash of the local file to a SHA1 hash of the remote file. If they are different then the local copy needs to be moved to the remote location.

tjr

  • Guest
Re: Windows - Command line and Dialog driven command
« Reply #10 on: October 01, 2008, 01:25:37 AM »
Those who are new to OOP and C# often find themselves reinventing things that already exist, merely because they did not know they were there. So, when one is just starting out, they can probably avoid reinventing many things that already exist if they were to just spend some time becoming more familiar with some of the basic principles and concepts of an OOP language and C# in particular, like operators and operator overloading.
Logical operators are considered OOP? I submit not.

MickD

  • King Gator
  • Posts: 3520
  • (x-in)->[process]->(y-out)
Re: Windows - Command line and Dialog driven command
« Reply #11 on: October 01, 2008, 03:42:23 AM »
Logical operators are considered OOP? I submit not.

Not quite Tim, Tony meant that you can overload logical operators, syntactic sugar they call it in C++ world. Either way it's just compiler gymnastics.

For example, the '==' can be overloaded (I think, but it's just an example), all it does though is let you use the '==' instead of calling say a static method that compares 2 of the same objects which may be called MyClass::Compare(this, someOtherClassOfThisType).

Either way you still have to write the comparison code, it's just short hand but can be confusing to the new comer to oop languages.
Forth is like the Tao: it is a Way, and is realized when followed.
Its fragility is its strength; its simplicity is its direction - Michael Ham

"The E in Javascript stands for 'easy'." Florin Pop tweet

Chuck Gabriel

  • Guest
Re: Windows - Command line and Dialog driven command
« Reply #12 on: October 01, 2008, 07:11:29 AM »
The definition of the > operator is determined by the types that are being compared.  Why wouldn't you call that OOP?

beck's bolero

  • Retired
  • Needs a day job
  • Posts: 7672
  • AKA Daniel
Re: Windows - Command line and Dialog driven command
« Reply #13 on: October 01, 2008, 07:18:50 AM »
An example might be Vector2d operator *

Code: [Select]
public static Vector2d operator *(Matrix2d a, Vector2d b)
public static Vector2d operator *(Vector2d a, double factor)
public static Vector2d operator *(double factor, Vector2d a)
Retired

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Windows - Command line and Dialog driven command
« Reply #14 on: October 01, 2008, 10:58:03 AM »
Thanks everybody.  I will look into this more when my brain is ready to digest it.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.