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

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
Trouble with console app to rename files
« on: March 11, 2008, 01:17:22 PM »
I don't know what is wrong, and I don't understand why nothing will be printed to the command line.  I have added a lot of Console.WriteLine thinking that it would print out to the command line, but that doesn't seem to be working.  I'm lost and don't seem to be able to find out why.

Any help is appreciated.  Thanks.

Code: [Select]
using System;
using System.IO;
using System.Windows.Forms;

namespace WindosApps
{
/// <summary>
/// Description of RenameFiles_Console.
/// </summary>
public class RenameFiles_Console
{
private static void Main(string[] args)
{
try {
Console.WriteLine("Starting work.");
if ( (args == null) || ( !args.Length.Equals(3) ) ) {
Console.WriteLine("Arguements: <Path> <Series number> <Start sheet number>");
    return;
}
Console.WriteLine("Starting work.");
string DirPath = args[0] as string;
if ( !Directory.Exists(DirPath) ) {
Console.WriteLine("Directory does not exist.");
return;
}
Console.WriteLine("Done with directory.");
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;
}
Console.WriteLine("Done checking series number.");
int SheetNumber = Convert.ToInt16(args[2]);
if (SheetNumber <= 0) {
Console.WriteLine("Starting sheet number has to be greater than 0.");
return;
}
Console.WriteLine("Done checking sheet number.");
foreach (string str in Directory.GetFiles(DirPath, "*.dwg")) {
string tempNewPath = DirPath + "\\" + SeriesPrefix + "_" + SheetNumber.ToString() + ".dwg";
string tempOldPath = DirPath + "\\" + str;
File.Copy(tempOldPath, tempNewPath);
if ( File.Exists(tempNewPath) ) {
File.Delete(tempOldPath);
Console.WriteLine("Renamed " + tempOldPath + " -> " + tempNewPath);
}
else
Console.WriteLine("Could not rename file: " + tempOldPath);
++SheetNumber;
}
}
catch (System.Exception SysEx) { Console.WriteLine(SysEx.Message); }
finally {
Console.ReadLine();
}
return;
}
}
}
Tim

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

Please think about donating if this post helped you.

FengK

  • Guest
Re: Trouble with console app to rename files
« Reply #1 on: March 11, 2008, 01:25:43 PM »
Try commenting out this line?
using System.Windows.Forms;

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Trouble with console app to rename files
« Reply #2 on: March 11, 2008, 01:28:54 PM »
Did help any.  I had put that in because the error wouldn't print to the command line, so in one version I put it in a MessageBox and that would show, but nothing I do will print to the command line.
Tim

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

Please think about donating if this post helped you.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Trouble with console app to rename files
« Reply #3 on: March 11, 2008, 01:39:37 PM »
I dont know if its your using statements, but
Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace tim
{
    class Program
    {
        static void Main(string[] args)
        {

            try
            {
                Console.WriteLine("Starting work.");
                if ((args == null) || (!args.Length.Equals(3)))
                {
                    Console.WriteLine("Arguements: <Path> <Series number> <Start sheet number>");
                    return;
                }
                Console.WriteLine("Starting work.");
                string DirPath = args[0] as string;
                if (!Directory.Exists(DirPath))
                {
                    Console.WriteLine("Directory does not exist.");
                    return;
                }
                Console.WriteLine("Done with directory.");
                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;
                }
                Console.WriteLine("Done checking series number.");
                int SheetNumber = Convert.ToInt16(args[2]);
                if (SheetNumber <= 0)
                {
                    Console.WriteLine("Starting sheet number has to be greater than 0.");
                    return;
                }
                Console.WriteLine("Done checking sheet number.");
                foreach (string str in Directory.GetFiles(DirPath, "*.dwg"))
                {
                    string tempNewPath = DirPath + "\\" + SeriesPrefix + "_" + SheetNumber.ToString() + ".dwg";
                    string tempOldPath = DirPath + "\\" + str;
                    File.Copy(tempOldPath, tempNewPath);
                    if (File.Exists(tempNewPath))
                    {
                        File.Delete(tempOldPath);
                        Console.WriteLine("Renamed " + tempOldPath + " -> " + tempNewPath);
                    }
                    else
                        Console.WriteLine("Could not rename file: " + tempOldPath);
                    ++SheetNumber;
                }
            }
            catch (System.Exception SysEx) { Console.WriteLine(SysEx.Message); }
            finally
            {
                Console.ReadLine();
            }
            return;
        }
    }
}

worked for me and printed things to the console
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Trouble with console app to rename files
« Reply #4 on: March 11, 2008, 01:57:36 PM »
Thanks for trying Cmdr, but that didn't work for me.  I had some errors in my code, and once I fixed that the code worked, but it still wouldn't print to the command line.  I'm kind of lost still.  I guess I will have to do some more research on MSDN.  Here is the updated code that works.

Code: [Select]
using System;
using System.IO;
using System.Windows.Forms;
using System.Collections.Generic;
//using System.Linq;
using System.Text;

namespace WindosApps
{
/// <summary>
/// Description of RenameFiles_Console.
/// </summary>
public class RenameFiles_Console
{
private static void Main(string[] args)
{
try {
Console.WriteLine("Starting work.");
if ( (args == null) || ( !args.Length.Equals(3) ) ) {
Console.WriteLine("Arguements: <Path> <Series number> <Start sheet number>");
    return;
}
//MessageBox.Show("1");
Console.WriteLine("Starting work.");
string DirPath = args[0] as string;
if ( !Directory.Exists(DirPath) ) {
Console.WriteLine("Directory does not exist.");
return;
}
//MessageBox.Show("2");
Console.WriteLine("Done with directory.");
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;
}
//MessageBox.Show("3");
Console.WriteLine("Done checking series number.");
int SheetNumber = Convert.ToInt16(args[2]);
if (SheetNumber <= 0) {
Console.WriteLine("Starting sheet number has to be greater than 0.");
return;
}
Console.WriteLine("Done checking sheet number.");
foreach (string str in Directory.GetFiles(DirPath, "*.dwg")) {
string strSheetNumber = SheetNumber.ToString();
if (SheetNumber < 10)
strSheetNumber = "00" + strSheetNumber;
else if (SheetNumber < 100)
strSheetNumber = "0" + strSheetNumber;
string tempNewPath = DirPath + "\\" + SeriesPrefix + "_" + strSheetNumber + ".dwg";
//MessageBox.Show(tempOldPath + "\n" + tempNewPath);
File.Copy(str, tempNewPath);
if ( File.Exists(tempNewPath) ) {
File.Delete(str);
Console.WriteLine("Renamed " + str + " -> " + tempNewPath);
}
else
Console.WriteLine("Could not rename file: " + str);
++SheetNumber;
}
}
catch (System.Exception SysEx) { Console.WriteLine(SysEx.Message); }
finally {
//Console.ReadLine();
}
return;
}
}
}
Tim

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

Please think about donating if this post helped you.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Trouble with console app to rename files
« Reply #5 on: March 11, 2008, 02:20:37 PM »
Command line or Console line?  I am assuming you know this is a console app and has nothing to do with Autocad, but the use of Command line jumped out at me.  If you knew this already, just ignore me
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Trouble with console app to rename files
« Reply #6 on: March 11, 2008, 02:26:08 PM »
Command line or Console line?  I am assuming you know this is a console app and has nothing to do with Autocad, but the use of Command line jumped out at me.  If you knew this already, just ignore me
I guess I'm just used to typing command line.  You are correct that I mean console line.  I didn't find anything on my search of MSDN, but maybe I will do a more thorough search when I have more time.  Thanks for trying.
Tim

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

Please think about donating if this post helped you.

Chuck Gabriel

  • Guest
Re: Trouble with console app to rename files
« Reply #7 on: March 11, 2008, 02:34:57 PM »
When you started the project, what project type did you select?

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Trouble with console app to rename files
« Reply #8 on: March 11, 2008, 02:51:29 PM »
good question, I assumed that because of the use of Console.Writeline, he had started a console app.  When I copied and pasted, I started a console app and it worked for me. (Well it didn't really work, but it did print to console line)
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Trouble with console app to rename files
« Reply #9 on: March 11, 2008, 03:31:52 PM »
When you started the project, what project type did you select?
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

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

Please think about donating if this post helped you.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Trouble with console app to rename files
« Reply #10 on: March 11, 2008, 03:33:51 PM »
Well that would be the problem, I dont see any way to create a console app from that Pic
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Trouble with console app to rename files
« Reply #11 on: March 11, 2008, 03:36:16 PM »
Well that would be the problem, I dont see any way to create a console app from that Pic
I guess I can search the SD web site, and forums, to see if I can find a way to do it then, if you persons think that is the problem.  I'll let you know what I find.
Tim

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

Please think about donating if this post helped you.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Trouble with console app to rename files
« Reply #12 on: March 11, 2008, 03:38:06 PM »
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
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Trouble with console app to rename files
« Reply #13 on: March 11, 2008, 03:39:21 PM »
I dont use SD, so I wont be of much help.  Sorry
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Trouble with console app to rename files
« Reply #14 on: March 11, 2008, 03:49:20 PM »
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
I see that option in my version.  Didn't know I had to make a whole new solution (project) just to use an application in the console.  I will see if that works..... Be right back......

THAT WAS THE PROBLEM!!  Thank you!  Now I know how to make console apps that will work.
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 #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.