0

I want to move a file if it exists into a folder with the date appended to the file when moved. I'm getting the error "The given path's format is not supported" though I have converted the date to a string, this is inside an event which triggers this code.

XAML:

<add key ="Directory" value ="C:\FILE WATCHER TEST FOLDER\"/>
<add key ="File name" value ="update1"/>
<add key="extension" value =".txt"/>
<add key ="Folder name" value ="archive updates\"/>

Code behind:

        DateTime date = DateTime.Parse("17/08/2012",ukCulture.DateTimeFormat);
        string theDate = Convert.ToString(date);

        string directory = ConfigurationManager.AppSettings["Directory"];
        string file = ConfigurationManager.AppSettings["File name"];
        string folder = ConfigurationManager.AppSettings["Folder name"];
        string extension = ConfigurationManager.AppSettings["extension"];

        string file_exe = file + extension;  

        string file_theDate = file + "-" + theDate;
        string file_theDate_exe = file_theDate + extension;

        string dir_fol = System.IO.Path.Combine(directory, folder);
        string dir_file_exe = System.IO.Path.Combine(directory, file_exe);
        string dir_file_theDate_exe = System.IO.Path.Combine(dir_fol, file_theDate_exe);

        if (File.Exists(dir_file_exe))
        {
            update.readNewFile();
            update.updatePaf();



            if (!Directory.Exists(dir_fol))
            {
                //create it move it
                System.IO.Directory.CreateDirectory(dir_fol);
                File.Move(dir_file_exe,dir_file_theDate_exe);


            }
            else
            {
                // move it
                if (File.Exists(dir_file_exe))
                {
                    File.Move(dir_file_exe, dir_file_theDate_exe);
                }
CODe
  • 2,253
  • 6
  • 36
  • 65
Jed I
  • 998
  • 3
  • 19
  • 37
  • What is `theDate`? It's probably the _format_ of the date which is incompatible with being in a filename. – Rawling Aug 20 '12 at 10:52
  • code is unclear. reduce your code to the minimum and include every constants (or configuration entries) for simple testing. – Stephan Schinkel Aug 20 '12 at 10:54
  • Check the values of the variables being sent to the move - post examples here.. – BugFinder Aug 20 '12 at 10:55
  • make sure you don't have any [reserved characters](http://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words) in your new filename. – default Aug 20 '12 at 11:01

2 Answers2

0
DateTime date = DateTime.Parse("17/08/2012",ukCulture.DateTimeFormat);

You can't have forward slashes in your path.

You could remove them (along with other illegal characters) before using the path, check out this i.e.: How to remove illegal characters from path and filenames?

Community
  • 1
  • 1
Gerald Versluis
  • 30,492
  • 6
  • 73
  • 100
0

Please use theDate.ToString({format as you need}) . May be format of date in your locale contains "bad" symbols(slashes).

Formats can be found at http://msdn.microsoft.com/en-us/library/az4se3k1.aspx