views:

332

answers:

5

I have created a screenshot program and all is working great. The only problem is, I am not sure how I can make it so the screenshots are saved with appending numbers.

Example: Screenshot 1, Screenshot 2, Screenshot 3, Screenshot 4, etc.

Obviously this could be applied to other files being saved. Any ideas? Thank you.

A: 

Since you are writing the files, one approach is to search the current save directory and find the newest file called ScreenshotXX, and use a regex to get the number from the name.

You could use the DirectoryInfo class along with the Regex class for this.

Alan
This could easily break. What if the user creates a file that has the name of one of the screenshot files? What if the user edits an old screenshot file? The solution needs to check for the existence of each file until it finds a "gap".
jnylen
@jnylen: What if the user creates a file that has the name of one of the screenshot files? >> You can't have two files with identical names.What if the user edits an old screenshot file?>> Editing a file won't matter, as it doesn't modify the create date.The solution needs to check for the existence of each file until it finds a "gap".>> True enough. However if it can't write the file (because it exists), it should increase the number until it can write. I don't think looking for a gap is a good idea, because if you have SS1, SS3,SS4, the newest screenshot will be SS2?
Alan
A: 

summary of solution

  1. Directory.GetFiles(dest);
  2. Sort files names
  3. analyze last file name , find the last number you used
  4. write next file with the next number appended to it.

would this work for you ?

Andrew Keith
+6  A: 

Here is a method I use frequently for this very case. Just pass in a string like "Screenshot" and it will find the lowest available filename in the format of "Screenshot [number]" (or just "Screenshot" if there aren't any already):

private string GetUniqueName(string name, string folderPath)
{
    string validatedName = name;
    int tries = 1;
    while (File.Exists(folderPath + validatedName))
    {
        validatedName = string.Format("{0} [{1}]", name, tries++);
    }
    return validatedName;
}

(Note: this is a slightly simplified version that doesn't take file extensions into account).

Rex M
Thanks! This is exactly what I need. Perfect how it checks for the lowest available filename, meant to put that in the question. Thank you very much sir.
Nate Shoffner
I would use `Path.Combine(folderPath, validatedName)` in place of `folderPath + validatedName`.
Sarah Vessels
+3  A: 

Is there a reason you're using numbers? Will the same folder be re-used later for another session? Should the numbers restart and replace existing files if the day is different?

These are the sorts of things to keep in mind. It's worth noting that OS X used to provides "Picture 1", "Picture 2" when doing screenshots, and thankfully in the new version it now uses "Screenshot taken on 2009-12-08 at 11.35.12" or something similar, allowing easier sorting by date, easily avoiding naming conflicts etc.

As posted in other suggestions you still need to do a check if the file already exists, and when you retry DateTime.Now will be different so the filename would be different. Of course you shouldn't get any conflicts unless the screenshots are in the same millisecond or the user is messing with the date/time (or daylight savings can mess you up too).

Timothy Walters
Currently, the files are saved using a timestamp. But some people have asked me if I could include an option that appends a number to the end of the file name if that's how they choose the file to be saved. I appreciate your suggestion.
Nate Shoffner
In that case you have two options, either loop using File.Exists checks with each successive number (quick and dirty) or get a file list and find the largest number and add 1. Depends what you want to do if someone has "Screenshot 1", "Screenshot 2", "Screenshot 33" assuming they deleted a bunch in-between, should the next be 3 or 34?
Timothy Walters
A: 

This is what i use:

string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); string newPath; List<string> files = new List<string>(Directory.GetFiles(path,"*.txt",SearchOption.TopDirectoryOnly)); for(int i=0; files.Contains(newPath=string.Format(path + @"\textfile{0}.txt",i)); i++) { Console.WriteLine("File:{0} exist skipping....", newPath); } File.WriteAllText(newPath,"dummy");

mpx