Author Topic: Trouble with console app to rename files  (Read 8123 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.