Elusive Error while renaming file.

Trying to rename a file but unable to do so.

Page 1 of 1

8 Replies - 1754 Views - Last Post: 21 November 2009 - 06:12 AM Rate Topic: -----

#1 brennydoogles  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 59
  • Joined: 23-June 09

Elusive Error while renaming file.

Posted 20 November 2009 - 12:51 PM

Hello all, I am writing a program that will rename files to their creation date+time for the easy organization of webcam videos used in a local business. I have the logic all set up so that as soon as a file is created or moved into the webcam capture folder, an event is triggered which should rename the file based upon its creation time. I am able to get all of the information from the file and trigger the events, but I cannot for the life of me rename the files. I am attempting to move the file to the same folder with a different name, but I get the following error when I do:
"The given path's format is not supported".
using System;
using System.IO;
using System.Windows.Forms;

namespace WebcamCaptureRenamerGUI
{
	public partial class Form1 : Form
	{
		FileSystemWatcher dir;
		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			dir = new FileSystemWatcher();
			dir.IncludeSubdirectories = false;
			dir.Path = "C:\Users\brendon\Desktop\Test";
			dir.Filter = "*.*";
			dir.Created += new FileSystemEventHandler(FileCreated);
			dir.EnableRaisingEvents = true;
		}

		protected void FileCreated(object sender, FileSystemEventArgs e)
		{
			FileInfo info = new FileInfo(e.FullPath);
			String fileExtension = info.Name.Split('.')[1];
			String[] time = info.CreationTime.ToString().Split(' ');
			String creationTime = String.Format("{0}_{1}", time[0].Replace('/','-'), time[1]);
			String newFile = String.Format("{0}.{1}", creationTime, fileExtension);
			newFile = Path.Combine(info.DirectoryName, newFile);
			MessageBox.Show(String.Format("{0}   {1}", e.FullPath, newFile));
			try
			{
				info.MoveTo(newFile);
			}
			catch (Exception error)
			{
				MessageBox.Show(error.Message);
			}
		}
	}
}



Any Ideas where I am going wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: Elusive Error while renaming file.

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6033
  • View blog
  • Posts: 23,416
  • Joined: 23-August 08

Re: Elusive Error while renaming file.

Posted 20 November 2009 - 01:10 PM

How does this work?

dir.Path = "C:\Users\brendon\Desktop\Test";


All those \s should be treated as escape sequences, shouldn't they?

Anyway, what is the filename/path that is producing this error?
Was This Post Helpful? 0
  • +
  • -

#3 brennydoogles  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 59
  • Joined: 23-June 09

Re: Elusive Error while renaming file.

Posted 20 November 2009 - 03:40 PM

View PostJackOfAllTrades, on 20 Nov, 2009 - 12:10 PM, said:

How does this work?

dir.Path = "C:\Users\brendon\Desktop\Test";


All those \s should be treated as escape sequences, shouldn't they?

Anyway, what is the filename/path that is producing this error?


Well, I have no idea what happened, but in my code that line reads
dir.Path = @"C:\Users\brendon\Desktop\Test";


and it seems to work fine. That may have been a copy/paste error or editing error. As for the filename/path producing the error, it is the same directory as above, with the filename of Testing.txt as the original filename, and 11-19-2009_5:48:27.txt as the new file name. The program is giving the error at the line
info.MoveTo(newFile);

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6033
  • View blog
  • Posts: 23,416
  • Joined: 23-August 08

Re: Elusive Error while renaming file.

Posted 20 November 2009 - 04:19 PM

I don't think those : chars are going to pass muster.
Was This Post Helpful? 0
  • +
  • -

#5 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Elusive Error while renaming file.

Posted 20 November 2009 - 05:22 PM

Use any character in the current code page for a name, including Unicode characters and characters in the extended character set (128255), except for the following:

The following reserved characters:
< > : " / \ | ? *

Integer value zero, sometimes referred to as the ASCII NUL character.
Characters whose integer representations are in the range from 1 through 31, except for streams where these characters are allowed. For more information about file streams, see File Streams.
Any other character that the target file system does not allow.

http://msdn.microsof...247(VS.85).aspx
Was This Post Helpful? 0
  • +
  • -

#6 indrora  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 100
  • Joined: 25-July 08

Re: Elusive Error while renaming file.

Posted 20 November 2009 - 07:45 PM

The more pressing issue is why is this person relying on a) local time formatting being the same and B) doing shiploads of special stuff.

Why not just use this:
FileInfo info = new FileInfo(e.FullPath);
newFilePath = Path.Combine(info.DirectoryName,
       string.Format("{0}-{1}-{2}_{3}:{4}:{5}{6}",
              info.CreationTime.Year,
              info.CreationTime.Month,
              info.CreationTime.Day,
              info.CreationTime.Hour,
              info.CreationTime.Minute,
              info.CreationTime.Second,
              info. Extension)
       );
info.CopyTo(newFilePath);



Reaons this is more efficient:
a) If you ever let this code roam, what happens when you encounter a date format like "23:44:23 2.4.09"?
B) it actually becomes more efficient in IL. Instead of creating a bunch of variables to keep things listed in and then copying them around, that already happens because you're given all the information in the original FileInfo.
Was This Post Helpful? 0
  • +
  • -

#7 brennydoogles  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 59
  • Joined: 23-June 09

Re: Elusive Error while renaming file.

Posted 20 November 2009 - 08:57 PM

Interesting. It seems that the : was causing all of the mischief after all. Anyone have any ideas on how to represent a time of day without the : character? Since we may have multiple files created in one day/hour, but certainly not in the same minute, the timestamp+date was designed to maintain file name uniqueness while also providing the ability to search the files easily.

As for why I chose to write the code the way I did previously I can sum it up in one word: Ignorance. I am still a student, and have quite a bit to learn about programming, and specifically .net (I started out in Java). Any other code optimization tips would be greatly appreciated!
Was This Post Helpful? 0
  • +
  • -

#8 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Elusive Error while renaming file.

Posted 21 November 2009 - 12:42 AM

When I use datetime to name files, I just run all the information together: Log20091120103851.txt
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6033
  • View blog
  • Posts: 23,416
  • Joined: 23-August 08

Re: Elusive Error while renaming file.

Posted 21 November 2009 - 06:12 AM

Something like this:
FileInfo info = new FileInfo(e.FullPath);
String newFileName = Path.GetFileNameWithoutExtension(info.Name) +
    info.CreationTime.ToString("yyyyMMddHHmmss") + 
    info.Extension;
String newFile = Path.Combine(info.DirectoryName, newFileName)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1