CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2008
    Posts
    4

    Rename files in a folder, then copy each file to its own folder

    C# newbie here...

    For simplicity, lets say I have a folder called TEST, which contains 3 files. Each file is named with the following naming convention: INVOICE_ABC_123.pdf, INVOICE_DEF_123.pdf, INVOICE_GHI_123.pdf.

    I would like to rename each file, stripping out INVOICE_, and replacing the trailing numeric values with todays date, resulting in files named like: ABC_6_11_14.pdf, DEF_6_11_14.pdf, GHI_6_11_14.pdf.

    I then want to loop through the files, creating a TEST2 folder which will contain a folder for each file, like: TEST2\ABC\xxx.pdf, TEST2\DEF\xxx.pdf, TEST2\GHI\xxx.pdf.


    Any help would be much appreciated.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Rename files in a folder, then copy each file to its own folder

    If you don't need the original folder, just copy/rename one file at a time, and place in new folder. Then delete all of the original files. The names can stay the same on those...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Rename files in a folder, then copy each file to its own folder

    Check out the system.IO namespace in msdn. For file/directory operations, check out http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Rename files in a folder, then copy each file to its own folder

    If you don't need to retain the renamed files in the original folder, you can use File.Move to rename them and move them all in one method call.

  5. #5
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    94

    Re: Rename files in a folder, then copy each file to its own folder

    additionally if you have to do work on those names algorithmic-ly
    you can use the method (also shown in his link above) Path.GetFileName

    string stringtoworkon = Path.GetFileName( somefilepath)

    then simply use
    string[] filenamepieces = String.Split(stringtoworkon, splitchars)

    you can pass string.Split(..,...) a character Array to break up your string
    using with whatever characters you wish to break it apart with

    char[] splitchars = new char[]{ '_','-'}; // chars are entered with ' not " quotes

    then you have each of those little parts you can process and check or change if required
    do a foreach loop on them if you like to build them all back up into a single string ect...
    that way working on a file name is equivalent to simply working on a string
    Last edited by willmotil; June 13th, 2014 at 03:31 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured