Author Topic: Trouble with console app to rename files  (Read 8125 times)

0 Members and 1 Guest are viewing this topic.

Glenn R

  • Guest
Re: Trouble with console app to rename files
« Reply #15 on: March 11, 2008, 05:24:17 PM »
Chuck and Duh were right on the money and that was my guess from your stated problem Tim.

At the EOD (End Of the Day), all of Visual Studio's project templates and Options, really set options for the CSC.exe (C Sharp Compiler) behind the scenes. So, for instance, if you start up a project based on a WinForms template, it sets options that are passed to the CSC to tell it to compile the program as a Winforms exe...or console exe etc.

From what I know of #Develop, it does the same thing and the passes the correct arguments for your chosen application type to CSC.exe, to compile your program.

Cheers,
Glenn.

Glenn R

  • Guest
Re: Trouble with console app to rename files
« Reply #16 on: March 11, 2008, 05:30:16 PM »
BTW Tim, your code writing has improved. That posted sample looks very good to mine own eyes.

One thing I would change:

Code: [Select]
SeriesPrefix.Length.Equals(14)

to this:

Code: [Select]
SeriesPrefix.Length == 14

Looking very nice though. Keep at it - it's actually quite a lot of fun isn't it? :)

Cheers,
Glenn.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Trouble with console app to rename files
« Reply #17 on: March 11, 2008, 05:50:30 PM »
Thanks Glenn, for the info and the comments on coding.

Yes it is quite a bit of fun.  I find myself looking forward to chances to write some C# code more than I do Lisp nowadays.
Tim

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

Please think about donating if this post helped you.

Glenn R

  • Guest
Re: Trouble with console app to rename files
« Reply #18 on: March 11, 2008, 05:59:23 PM »
Heh. That reminds me of one of the many turning points for me, when I transitioned from lisp->VBA->C++->C#.

The next one you will find, is that you can code up something bigger, better, faster in C# than you can in Lisp  8-)

Cheers,
Glenn.

Glenn R

  • Guest
Re: Trouble with console app to rename files
« Reply #19 on: March 11, 2008, 06:03:31 PM »
Also, I forgot to mention:

C# Rulz and Lisp droolz.   :angel:

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Trouble with console app to rename files
« Reply #20 on: March 11, 2008, 07:31:09 PM »
Here is the code that works the way I want it to.  I might clean it up when I have time, but for now this does what I want it to do, and fast.  I had 193 drawings that had to be renamed to match our standard, and this baby did it in less than one second.  My lisp couldn't even handle the sorting of names.
Code: [Select]
/*
 * Created by SharpDevelop.
 * User: Tim Willey
 * Date: 3/11/2008
 * Time: 12:42 PM
 *
 * Confirmation of how I would have to do the sorting (like Windows Explorer)
 *   http://www.codeproject.com/KB/recipes/csnsort.aspx
 *
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.IO;
using System.Collections;

namespace TestConsoleApps
{
class Program
{
public static void Main(string[] args)
{
try {
if ( (args == null) || ( !args.Length.Equals(3) ) ) {
Console.WriteLine("Arguements: <Path> <Series number> <Start sheet number>");
    return;
}
string DirPath = args[0] as string;
if ( !Directory.Exists(DirPath) ) {
Console.WriteLine("Directory does not exist.");
return;
}
string SeriesPrefix = args[1] as string;
if ( !SeriesPrefix.Length.Equals(14) ) {
Console.WriteLine("Series number is not the correct length: <12-2xxx-xxxx-x>");
return;
}
int SheetNumber = Convert.ToInt16(args[2]);
if (SheetNumber <= 0) {
Console.WriteLine("Starting sheet number has to be greater than 0.");
return;
}
using (StreamWriter sw = new StreamWriter(args[0] + "\\" + "DrawingConversion.txt")) {
string[] FullPathAr = Directory.GetFiles(DirPath, "*.dwg");
MyStringCompare1 msc = new MyStringCompare1();
Array.Sort(FullPathAr, msc);
foreach (string str in FullPathAr) {
string strSheetNumber = SheetNumber.ToString();
if (SheetNumber < 10)
strSheetNumber = "00" + strSheetNumber;
else if (SheetNumber < 100)
strSheetNumber = "0" + strSheetNumber;
string tempNewPath = DirPath + "\\" + SeriesPrefix + "_" + strSheetNumber + ".dwg";
File.Copy(str, tempNewPath);
if ( File.Exists(tempNewPath) ) {
File.Delete(str);
sw.WriteLine("Renamed " + str + " -> " + tempNewPath);
}
else
sw.WriteLine("Could not rename file: " + str);
++SheetNumber;
}
}
}
catch {}
return;
}
class MyStringCompare {

public MyStringCompare () {}

public static int Compare (object obj1, object obj2) {
string str1 = (string)obj1, str2 = (string)obj2;
int i1 = 0,
i2 = 0,
CompareResult,
l1 = str1.Length,
l2 = str2.Length,
tempI1,
tempI2;
string s1, s2;
bool b1, b2;

while (true) {
b1 = Char.IsDigit(str1, i1);
b2 = Char.IsDigit(str2, i2);
if ( !b1 && b2 )
return -1;
if ( b1 && !b2 )
return 1;
if (b1 && b2) {
FindLastDigit(str1, ref i1, out s1);
FindLastDigit(str2, ref i2, out s2);
tempI1 = Convert.ToInt32(s1);
tempI2 = Convert.ToInt32(s2);
if ( tempI1.Equals(tempI2) )
CompareResult = 0;
else if (tempI1 < tempI2)
CompareResult = -1;
else
CompareResult = 1;
if ( !CompareResult.Equals(0) )
return CompareResult;
}
else {
FindLastLetter(str1, ref i1, out s1);
FindLastLetter(str2, ref i2, out s2);
CompareResult = string.Compare(s1, s2);
if ( !CompareResult.Equals(0) )
return CompareResult;
}
if (l1 <= i1) {
if (l2 <= i2) {
return 0;
}
else {
return -1;
}
}
if (l2 <= i2) {
if (l1 < i1) {
return 0;
}
else {
return 1;
}
}
}
}
private static void FindLastLetter (string MainStr, ref int i, out string OutStr) {
int StartPos = i;
int StrLen = MainStr.Length;
++i;
while ( i < StrLen && !Char.IsDigit(MainStr, i) )
++i;
OutStr = MainStr.Substring(StartPos, i - StartPos);
}
private static void FindLastDigit (string MainStr, ref int i, out string OutStr) {
int StartPos = i;
int StrLen = MainStr.Length;
++i;
while ( i < StrLen && Char.IsDigit(MainStr, i) )
++i;
OutStr = MainStr.Substring(StartPos, i - StartPos);
}
}
public class MyStringCompare1 : IComparer {
public MyStringCompare1 () {}
public int Compare (object x, object y) {
return MyStringCompare.Compare( x,y );
}
}
}
}
Example of a couple of the renamed files.
Code: [Select]
Renamed c:\test\test\EM3060030 (Z-ZDIST.8).DWG -> c:\test\test\12-2697-7156-9_177.dwg
Renamed c:\test\test\EM3060030 (Z-ZDIST.9).DWG -> c:\test\test\12-2697-7156-9_178.dwg
Renamed c:\test\test\EM3060030 (Z-ZDIST.10).DWG -> c:\test\test\12-2697-7156-9_179.dwg
Renamed c:\test\test\EM3060030 (Z-ZDIST.11).DWG -> c:\test\test\12-2697-7156-9_180.dwg
Renamed c:\test\test\EM3060030 (Z-ZDIST.12).DWG -> c:\test\test\12-2697-7156-9_181.dwg
Renamed c:\test\test\EM3060030 (Z-ZDIST.13).DWG -> c:\test\test\12-2697-7156-9_182.dwg

One thing I would change:

Code: [Select]
SeriesPrefix.Length.Equals(14)

to this:

Code: [Select]
SeriesPrefix.Length == 14

I like the way the .Equals() looks better.   :-)
Tim

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

Please think about donating if this post helped you.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Trouble with console app to rename files
« Reply #21 on: March 11, 2008, 10:30:35 PM »

looks nice Tim.

Glenn,
is droolz a local vocab' word over there  :-)

aside:
The Mercury style shows the code without scroll bars ... just a tip.




kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

FengK

  • Guest
Re: Trouble with console app to rename files
« Reply #22 on: March 11, 2008, 10:58:09 PM »
I just selected class as the template.  I don't see a selection for console app.  Attached is the new file dialog from SharpDevelop.

Tim, did you click the "Windows Applications" node on the left?

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Trouble with console app to rename files
« Reply #23 on: March 12, 2008, 02:37:04 AM »
I just selected class as the template.  I don't see a selection for console app.  Attached is the new file dialog from SharpDevelop.

Tim, did you click the "Windows Applications" node on the left?
I saw that after Cmdr posted, see quote below.  I didn't know that I had to start a new project, as I just started a new file.  I got it to work though.  Thanks for the help Kelie.

What version of SD do you have? this site shows SD with a console app option.  If you go to File->New->Solution do you get a console app option
Tim

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

Please think about donating if this post helped you.

Glenn R

  • Guest
Re: Trouble with console app to rename files
« Reply #24 on: March 12, 2008, 05:02:53 AM »

Glenn,
is droolz a local vocab' word over there  :-)

aside:
The Mercury style shows the code without scroll bars ... just a tip.


No, it's not local Kerry...something I heard in OZ.
I am already using Mercury as per your suggestion a while a go - much better without scroll bars.

Cheers,
Glenn.