Rename image files on server directory?

You could look at string. Split Loop over all files in your directory string fileParts = oldFileName. Split('-') This will give you an array of two strings: fileParts0 -> "400" fileParts1 -> "60957.

Jpg using the first name in your list Your new filename then becomes: if (fileParts0. Equals(oldPrefix)) { newFileName = string. Format("(0)-(1)", newPrefix, fileParts1); } Then to rename the file: File.

Move(oldFileName, newFileName) To loop over the files in the directory: foreach (string oldFileName in Directory. GetFiles(pathToFiles, searchPattern)) { // Rename logic }.

You could look at string.Split. Loop over all files in your directory. String fileParts = oldFileName.

Split('-'); This will give you an array of two strings: fileParts0 -> "400" fileParts1 -> "60957. Jpg" using the first name in your list. Your new filename then becomes: if (fileParts0.

Equals(oldPrefix)) { newFileName = string. Format("(0)-(1)", newPrefix, fileParts1); } Then to rename the file: File. Move(oldFileName, newFileName); To loop over the files in the directory: foreach (string oldFileName in Directory.

GetFiles(pathToFiles, searchPattern)) { // Rename logic }.

Thanks Chris. What would my "foreach" statement look like if looping through the directory instead of the bulletedList. I've got code block in translating the logic from a List to the directory object.

Regards, – Doug Oct 21 '09 at 23:03 Although Andre's answer is just as relevant, using string. Split is more simple for this particular situation. Chris gets the accepted answer.

Thanks to both of you guys. Regards, – Doug Oct 22 '09 at 16:03.

Actually you should iterate each of the files in the directory and rename one by one To determine the new file name, you may use something like: String newFileName = Regex. Replace("400-60957. Jpg", @"^(\d)+\-(\d)+", x=> "NewPrefix" + "-" + x.

Groups2. Value); To rename the file, you may use something like: File. Move(oldFileName, newFileName); If you are not familiar with regular expressions, you should check: http://www.radsoftware.com.au/articles/regexlearnsyntax.aspx And download this software to pratice: http://www.radsoftware.com.au/regexdesigner.

Thanks Andre - What is your reasoning using RegEx over String. Split? I could get either of these suggestions to work, but like to know more.... Regards, Doug – Doug Oct 21 '09 at 22:53 Split works fine, but Regex would give you more flexibility.In your case it's easy to split, but let's say you want to replace using a more complex pattern like: 012-123112-167-ab-128-bb.jpg.

Let's say you want to replace the first group of letters, that may be anywhere? What do you do? Regex fits better this case.

– André Pena Oct 21 '09 at 23:45 Good point. Which means I'll have to go back to the end users (company managers) to see what they foresee for future needs. Regards, – Doug Oct 22 '09 at 0:01.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions