Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Name files incrementally 1

Status
Not open for further replies.

speedyrudolf

Programmer
Sep 2, 2009
27
RO
Hi. Can someone tell me how to name files incrementally.
For example, I'm trying to make an app that downloads once a day a file from the same site. The problem I'm facing is: I want to name the files that are downloaded something like this day1.jpg, day2.jpg, day3.jpg etc. I already did most of the code...I just need to name the files properly...
Code:
private void timer1_Tick(object sender, EventArgs e)
{
i = i + 1;
if (i == 3600)
{
WebClient Download = new WebClient();
Download.DownloadFile("[URL unfurl="true"]http://www.csharpfriends.com/Members/index.aspx",[/URL] "C:/Users/Rudolf/Pictures/day"a".jpg");
Date_update();
a = a + 1;
i=0;
}
}
a and i are declared globally, a starts at 1, i at 0, the timer is updated once a minute, that isn't the site it will download from (I will copy the correct link later), date_update is already done and working great, and all that needs to be done is naming the file correctly.
So....anyone know to do this?
Also, if the image is linked to through a php page (I mean, the page is a php page which only contains an image (just the image, there is no text or anything else) which is replaced on a daily basis), can C# download the image, not the php? And if the image is jpg, can C# save it as bmp?
Thank you in advance. Bye.
 
The first thing I would change is the timer interval, right now you have it set for 1 second, why not set it to 3600??

You have most of the code right for renaming the file, increment a counter to keep track, you just need to apply the variable to the name of the jpg.

Code:
private void timer1_Tick(object sender, EventArgs e)
{
a = a + 1;
string _newFile="day" + a + ".jpg";
//
WebClient Download = new WebClient();
Download.DownloadFile("[URL unfurl="true"]http://www.csharpfriends.com/Members/index.aspx",[/URL] "C:\Users\Rudolf\Pictures\" + _newFile);
Date_update();
}

Patrick
 
Thanks...I just needed to put + before and after the variable....:D
 
I think the timer's still off. There are 3600 seconds in an hour, 86,400 seconds in a day.

Is this part of an application that has to be up and running 24 hours a day? You could simplify matters and make things more reliable by just writing a plain backup routine and just run it as a scheduled task once a day from Windows.

Geoff Franklin
 
Yeah...I know...I actually ended up writing the whole app from scratch...Now it has a lot more functionality, and is more reliable...Anyway, thank you very much.
 
timers are not not based on seconds. they are based on milliseconds. so 3600 ms is is 3.6 seconds.

you said you restructured your code so this may be mute, but there is no need for global variables to track incrementing files. to increment the file name I would either:
1. get the count of the number of files in the directory +1
2. get a list of the files in the directory, sort by name, takes last, parse number from filename.
3. track the incrementation in memory. when the application stops persist this number to file. when the application starts read the number from file and start incrementing.

option 1 is the simplest, but could become a bottleneck if the number of files is large. option 2 is overly complex and will have the same preformance problems as option 1. option 3 it more complex than option 1 but it will handle naming a large number of files.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Ok...thank you everyone...but, like I already said, I rewrote the whole code from scratch...I'm now using TimeSpan, and when it reaches 0, it downloads the file...I ended up using this to make it crash/accidental restart/shutdown proof (meaning that if windows crashes etc, when windows restarts, the timer won't start from 0, so it will always download on time (as long as the pc is turned on)). And also I ended up using the third option (the number is in a file, it reads the number, it downloads the file, increments the number, writes it in the file and closes the file). I also added some other features to make it more configurable (now the next download time and date can be configured; I added a "download now" button; it minimizes to the system tray; if windows or the app crashes or I restart/shutdown the pc, when the app loads (I put it in windows startup) it automatically sets the next download date and time). I could add another feature, but it's not really necessary...especially as it is for personal use...:)
Anyway...Thanks everyone, but (I think) I have everything covered.:D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top