none
Renaming a file name with creation time results in only the seconds getting written to file RRS feed

  • Question

  • I have finally being able to write the creation time and date to a file name I am trying to move. The problem I am having though is it only outputs the seconds including AM or PM to the file. I want it to output the creation time and date in long format. E.g “yyyy,mm,dd hh,mm,ss.”

    I also don’t want to have to explicitly state the file extension and for it to just make a copy of the file in the same format.

    The problem lies on this line in btnConvert_click. That's where it's only returning the sconds.

    _strfileName = Path.GetFileName(_strNewName);
    Here is the rest including snapshot of Auto's Window that displays an error.
    public partial class Form1 : Form
        {
            string[] _dropppedFiles;
            string _strTargetPath;
            string _strfileName = string.Empty;
            string _strDestFile = string.Empty;
            string _strNewName;
    
            public Form1()
            {
                InitializeComponent();
            }
            
            private string getFileName(string _strSourcePath)
            {
                return Path.GetFileName(_strSourcePath);
            }
    
            private string getDirectoryName(string _strSourcePath)
            {
                return Path.GetDirectoryName(_strSourcePath);
            }
    
            private void Form1_DragDrop(object sender, DragEventArgs e)
            {
                //TAKE dropped items and store in array.
                _dropppedFiles = (string[])e.Data.GetData(DataFormats.FileDrop);
    
                //LOOP through all droppped items and display them
                foreach (string file in _dropppedFiles)
                {
                    lstDragDrop.Items.Add(file);
                }
            }
    
            private void Form1_DragEnter(object sender, DragEventArgs e)
            {
                if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
                {
                    e.Effect = DragDropEffects.All;
                }
            }
    
            private void btnConvert_Click(object sender, EventArgs e)
            {
    
                _strTargetPath = txtFolderPath.Text;
                foreach (string file in _dropppedFiles)
                {
                    //IF filename doesn't exist in drag drop box.
                    if (!lstDragDrop.Items.Contains(_dropppedFiles.Length))
                    {
                        //GET Creation Date and time from file.
                        _strNewName = Convert.ToString(File.GetCreationTime(file));
                        //Get file path.
                        _strfileName = Path.GetFileName(_strNewName);
                        _strDestFile = Path.Combine(_strTargetPath, _strfileName);
                        File.Move(file, _strDestFile);                
                    }
                    
                    else
                    {
                        Console.WriteLine("Source path does not exist!");
                    }
                }
            }
        }
    Error of Image in debug window

    • Edited by SlenderMan564 Friday, November 16, 2018 4:57 AM I asked how to make a copy of a file with a new name. But I already found out and implemented the method involved.
    Thursday, November 15, 2018 5:45 AM

Answers

  • Try this:

    _strfileName = File.GetCreationTime( file ).ToString( "yyyy'-'MM'-'dd hh'\u0589'mm'\u0589'ss tt", CultureInfo.InvariantCulture );

    or this:

    _strfileName = File.GetCreationTime( file ).ToString( "yyyy'-'MM'-'dd hh'-'mm'-'ss tt", CultureInfo.InvariantCulture );

    You cannot use the regular ':' in file names because it is reserved for disks, such as "C:". The '/' has a special meaning too.




    • Edited by Viorel_MVP Thursday, November 15, 2018 6:31 AM
    • Marked as answer by SlenderMan564 Friday, November 16, 2018 4:01 AM
    Thursday, November 15, 2018 6:21 AM