开发者

How to compare the datetime picker value with some folder name

I have a backup files in folder path is like this C:\Folder

the files are like this in below 开发者_运维问答image

How to compare the datetime picker value with some folder name

the file name like this...20111011 means today date 095523 means time

and i have a date time picker and i have button when the user select the value in datetime picker and select the button ,

I need to compare the date time picker value with the file name stored in that folder , and then if date time picker value matches the part of the folder name (backup-{this is part}) i want to extract zip file into the given folder ....

how can i get compare date time picker value with the folder name and extract the files in to given path ....


In .net Directory.GetFiles method used to get files from specific path.

Below code mentioned in link make changes in foreach loop as you need. This loop give you filename and you can compare it.

public static void ProcessDirectory(string targetDirectory) 
{
    // Process the list of files found in the directory.
    string [] fileEntries = Directory.GetFiles(targetDirectory);
    foreach(string fileName in fileEntries)
      Do work here which you need.
}

Check this link for datetimepicker value


You can read the file names using System.IO.File

When you have read them, you can extract their dates using DateTime.ParseExact to parse the file date and time.

And then you just have to compare the file date and time with the value of your date picker.


You can try something like this:

DateTime dateSelected = <date coming from the picker>;
string fileRequested = string.Format("backup-{0}{1}{2}{3}{4}{5}",dateSelected.Year,dateSelected.Month,dateSelected.Day,dateSelected.Hour,dateSelected.Minute,dateSelected.Second;

Then you can use the answer from Emaad to get work with the file.


Just a it "hacky" - but it works.

        var dateToParse = "20111011095323";
        var date = new DateTime(int.Parse(dateToParse.Substring(0, 4)),
                                int.Parse(dateToParse.Substring(4, 2)),
                                int.Parse(dateToParse.Substring(6, 2)),
                                int.Parse(dateToParse.Substring(8, 2)),
                                int.Parse(dateToParse.Substring(10, 2)),
                                int.Parse(dateToParse.Substring(12, 2)));


First of all, you will have to set the DateTimePicker date-time format properly in order to select both date and time.

dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "MM dd yyyy hh mm ss";  

Next I think you have to construct the file name by using the DateTimePicker.Value. Look at here, you can ToString the DateTime to your required format.

For example...

DateTime dt = dateTimePicker1.Value;
string fileName = string.Format("backup-{0}",dt.ToString(yyyyMMddHHmmss));

Not sure about your time format. If it is 12 hour use "H" and use "HH" for 24 hours. Look at here for more examples. The rest won't be difficult...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜