1

I want to create a simple program that can access all the files in the folder, all of which being the same type of file, and then use a for each to rename all of them. The new name will be IMG### with the ### being incremental. I know how to do this. How do I reference all the images to be used in a for each?

Example:

For Each X in Folder {
    rename stuff here for x
 }

All I need to know is how to reference file in the spot of x.

Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
  • 1
    [Directory.GetFiles Method](https://msdn.microsoft.com/en-us/library/07wt70x2%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396) – 001 Apr 27 '18 at 15:10
  • 1
    https://stackoverflow.com/questions/11861151/find-all-files-in-a-folder?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – thesystem Apr 27 '18 at 15:11
  • Could you clarify on the question? Are you saying you would like to remember the names/paths of the IMG### files you move the original files to so that you can use them later? – NeilMacMullen Apr 27 '18 at 15:15
  • More specifically you would need the [`Directory.GetFiles(string, string)`](https://msdn.microsoft.com/en-us/library/wz42302f(v=vs.110).aspx) overload. The first parameter is the folder path, the second is the search pattern (here is where you restrict the file type) – maccettura Apr 27 '18 at 15:18

1 Answers1

2

You can go about it along this line of thought:

var files = Directory.GetFiles(path, "*.", SearchOption.AllDirectories);

foreach (string item in files)
{
    //You can rename by moving the files into their 'new names'. 
    //The names are full paths
    File.Move(oldName, newName);
}
Adem Catamak
  • 1,987
  • 2
  • 17
  • 25
Felix Too
  • 11,614
  • 5
  • 24
  • 25
  • You might want to flesh it out and include some stuff on System.IO.Path members like GetFileName, to help parse the 'item' in your foreach loop. – Kevin Apr 27 '18 at 16:37