none
Input a file name, rearrange the file name, save back to the same location RRS feed

  • Question

  • I have a number of Excel files which were saved with the following format:

    JODDMMYY(0890)Equipment.xlsx

    The format is a two letter code for a loaction, DD,MM,YY, Equipment information.

    This needs to be changed by switching around the MM and DD in the file names.

    The new file would look like:  JOMMDDYY(0890)Equipment.xlsx

    I looking to find the file, load the old file in a list, rearrange the file name and save it back to the orginal file location.

    Any help?

    CodeMess

    Wednesday, September 14, 2011 5:11 PM

Answers

  • I hope this helps.

     

                DirectoryInfo dir = new DirectoryInfo("d:\\test");
                var query = from c in dir.GetFiles("*.xlsx", SearchOption.TopDirectoryOnly)
                            orderby (c.Name.Substring(6, 2) + c.Name.Substring(2, 2) + c.Name.Substring(4, 2))
                            select c;
    
                Console.WriteLine(query.First());
    

     

    or orderby c.LastWriteTime

    Thursday, September 15, 2011 1:41 AM

All replies

  • I hope this helps.

     

                DirectoryInfo dir = new DirectoryInfo("d:\\test");
                var query = from c in dir.GetFiles("*.xlsx", SearchOption.TopDirectoryOnly)
                            orderby (c.Name.Substring(6, 2) + c.Name.Substring(2, 2) + c.Name.Substring(4, 2))
                            select c;
    
                Console.WriteLine(query.First());
    

     

    or orderby c.LastWriteTime

    Thursday, September 15, 2011 1:41 AM
  • A couple of follow up questions.

    It looks like the code would look into a specific location (drive and folder), this is not the case.  The files are in different folders in the same drive.  I need to be able to select the drive and folder which contains the files that need to be modified.  Once I am in the correct location, the process needs to complete the renaming process and then close so I could then select a new location (folder and files) to process.

    How can I use the code you wrote?  Is the code to be placed in an Excel Macro or in a VB application?

     


    Kristopher Simonian
    Thursday, September 22, 2011 9:05 PM
  • I Hope this helps.

    I'm not good at english. sorry.

    If you think I seem not to understand your question, please upload to your case with a picture for me to figure out your problems.

    I'm using C# and VSTO workbook project.

                FolderBrowserDialog fld = new FolderBrowserDialog();
                fld.ShowDialog();
                if(fld.SelectedPath != null)            
                {
                    DirectoryInfo dir = new DirectoryInfo(fld.SelectedPath);
                    foreach (var c in dir.GetFiles("jo*.xlsx", SearchOption.TopDirectoryOnly))
                    {
                        string file = c.Name;
    
                        string month = file.Substring(4, 2);
                        string day = file.Substring(2, 2);
    
                        file = file.Remove(2, 4).Insert(2, month).Insert(4, day);
    
                        File.Move(fld.SelectedPath + "\\" + c.Name, fld.SelectedPath + "\\" + file);
                    }
    
                }
    

    before

    after

    I think it seems to be a same Case : http://vsto.tistory.com/194

     


    http://vsto.tistory.com

    Friday, September 23, 2011 2:26 AM
  • I think some of the disconnect is the fact that I am using vb.net and you stated you are using C#.

    Since I am new to vb.net, it's hard for me to provide many more specifics than what I have provided.  It looks like the code you wrote would work for my application, if I can get the right coding for vb.net and not C#.

    Any help would be great.

    Codemess


    Kristopher Simonian
    Friday, September 23, 2011 8:00 PM