-
June 11th, 2014, 10:57 AM
#1
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.
-
June 11th, 2014, 12:20 PM
#2
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...
-
June 11th, 2014, 12:22 PM
#3
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
-
June 11th, 2014, 12:23 PM
#4
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.
-
June 13th, 2014, 03:17 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|