MLyons10

I have a listbox, that I am able to add files to. I would like to then when the user clicks a button have the application rename the files and save them in a specific directory that I set in code. The file names would be set in code as well.

For example, the user selects "mypicture.jpg" and "mypicture2.jpg". When they click the button I would like the application to rename these pictures "Pic01" and "Pic02" and I would like this to happen in the order that the images were added to the listbox. I have been trying to find some information about doing something like this online, but haven't had much luck. Can this be done

Any help and information is greatly appreciated.

Thanks,


Re: Visual C# Express Edition How do I save files in a listbox?

IsshouFuuraibou

You would do something like:

Code Snippet

for( int i = 0; i < listBox1.Items.Count; i++ )
{
string SourceFile = listBox1.Items[i].ToString();
string DestFile = string.Format("Pic{0:00}", i + 1);
System.IO.File.Move(SourceFile, DestFile);
}



inside the button, make sure your listBox1 is not set to sort alphabetically. (the Sorted property should be false before adding files)





Re: Visual C# Express Edition How do I save files in a listbox?

MLyons10

Thank you very much, I will play around with this and see if I can get it working in my application. Where would I specify the folder that I would like these files to be saved in Also, will this code retain the file extensions, or do I need to enter if it is a .jpg, etc.

Any help and information is greatly appreciated.

Thanks Again,




Re: Visual C# Express Edition How do I save files in a listbox?

IsshouFuuraibou


Code Snippet


string path = @"C:\Some Path To\Somewhere\";
for( int i = 0; i < listBox1.Items.Count; i++ )
{
string SourceFile = listBox1.Items[i].ToString();
string DestFile = string.Format("{0}Pic{1:00}",
path, i + 1);
System.IO.File.Move(SourceFile, DestFile);
}








Re: Visual C# Express Edition How do I save files in a listbox?

MLyons10

Hi, thank you again for your help, this is working great. One more question I had was, how do I set it up so that when it saves the file it adds the appropriate file extension (.jpg, .bmp, .pdf, etc.). Presently it is not doing that, and I'm not sure how to make it assign the appropriate extension.

Any help and information is greatly appreciated.

Thanks Again,





Re: Visual C# Express Edition How do I save files in a listbox?

MLyons10

I'm sorry, but one final question (In addition to the one above). I forgot about another file that is going to be in the directory. Am I able to start the counting at Pic02 I tried to edit the code, and the file naming went up by tens...lol

I thought I would change:

string DestFile = string.Format("{0}P{1:000}", path, i + 1);

To:

string DestFile = string.Format("{0}P{1:001}", path, i + 1);

But I guess that wasn't right...

Any help and information is greatly appreciated.

Thanks Again,




Re: Visual C# Express Edition How do I save files in a listbox?

jrboddie

Why not try:

System.IO.File.Copy(SourceFile, DestFile);

instead of

System.IO.File.Move(SourceFile, DestFile);





Re: Visual C# Express Edition How do I save files in a listbox?

MLyons10

Thanks, I submitted that question by mistake. I saw that and made the change. Now it's working fine in that respect. I edited my post above to show what I meant to post...

I apologize for the confusion

Thanks Again,





Re: Visual C# Express Edition How do I save files in a listbox?

IsshouFuuraibou

You can use copy, if you want to leave the source file in place.

If you want to maintain extension the easiest way is to get a FileInfo from the first file, and add the extention to the end of the string format:

Code Snippet

string path = @"C:\Some Path To\Somewhere\";
for( int i = 0; i < listBox1.Items.Count; i++ )
{
string SourceFile = listBox1.Items[i].ToString();
System.IO.FileInfo Source = new System.IO.FileInfo(SourceFile);
string DestFile = string.Format("{0}Pic{1:00}{2}", // show two digits (01... 12, etc)
path, // Path ending with \
i + 1, // List index starts from 0, if you want to adjust the starting filename, adjust the + 1
Source.Extension); // extension include the period (bitmap file would return .bmp)
System.IO.File.Move(SourceFile, DestFile);

/* Use either Move or Copy depending on what functionality you want. Copy has an option for overwriting or not */
}


I've elaborated on how the name gets developed.





Re: Visual C# Express Edition How do I save files in a listbox?

MLyons10

Thank you very much for your help. I tried to get it to copy the file extension, but that is still not working for whatever reason. I am not getting any errors or anything.

Here is what I have:

private void button2_Click(object sender, EventArgs e)
{
string path = @"c:\projects\" + textBox19.Text + @"\";
for (int i = 0; i < browseList1.Items.Count; i++)
{
string SourceFile = browseList1.ItemsIdea.ToString();
System.IO.FileInfo Source = new System.IO.FileInfo(SourceFile);
string DestFile = string.Format("{0}P{1:00}", path, i + 2, Source.Extension);

System.IO.File.Copy(SourceFile, DestFile);
}
}

I'm sorry, but I just can't seem to get this working, am I missing a step Everything else seems to be working fine, with the exception of the file extensions. Any help and information is greatly appreciated.

Thanks Again,




Re: Visual C# Express Edition How do I save files in a listbox?

MLyons10

The light bulb is a "[ i ]" Not sure why it became a lightbulb....




Re: Visual C# Express Edition How do I save files in a listbox?

IsshouFuuraibou

[ i ] (without the spaces) is a shortcut for the idea emoticon... it's suppressed in code blocks but not elsewhere.

Sorry I forget to add {2} after the {1:00}... I edited my above sample to be correct. But to point it out:

string DestFile = string.Format("{0}P{1:00}", path, i + 2, Source.Extension);
becomes
string DestFile = string.Format("{0}P{1:00}{2}", path, i + 2, Source.Extension);

You should do some reading on string.Format, I'm sure you'll be able to understand that line and the different modifications done with it over the course of this thread.





Re: Visual C# Express Edition How do I save files in a listbox?

MLyons10

Thank you very much for the help and the link. I will read through that as I want to come to a better understanding of this. Everything is working now as it is supposed to.

Thanks Again,