#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

char content[50000],name[5000],starttime[5000],endingtime[5000],data[10000],temp[10000],output[1000000];
char cmd[200];
char cmd1[200]; 
char cmd2[200];

 
int main()
{
        HANDLE events,events_years,events_years_months,events_location;
        WIN32_FIND_DATA FindFileData,FindFileData_month,FindFileData_days,FindFileData_location;
        char path[100]="d:events\\";
        char path1[100]="d:events\\";
        char path2[100];
        //system("C:\\Users\\xucin\\Desktop\\EW\\earthworm_7.7\\bin\\sac2tb D:\\EVENTS\\201204\\20120427_144800_MAN\\W13A.HHE.TW.-- D:\\EVENTS\\201204\\20120427_144800_MAN\\123.tb");
        strcpy (cmd,path);
        strcat (cmd,"2*.*");
        events = FindFirstFile(cmd, &FindFileData);
        //FindFileData: get D:\events\****
        
        
        bool  years = false;
        bool  months = false;
        bool  days = false;
        while(!years)
        {
                strcpy (path,"e:events\\");
                strcat (path,FindFileData.cFileName);
                strcpy (cmd,path);
                strcat (cmd,"\\2*.*  ");
               
                events_years = FindFirstFile(cmd, &FindFileData_month);
                //FindFileData_month: get D:\events\****\****
               while(!months){
                               strcpy(path1,path);
                               strcat(path1,"\\");
                               strcat(path1,FindFileData_month.cFileName);
                               strcpy(cmd1,path1);
                               strcat(cmd1,"\\W*.txt ");

                               days=false;
                               events_years_months = FindFirstFile(cmd1, &FindFileData_days);
                               //FindFileData_days: get target
                               
                               while(!days){ 
                                             FILE *txt,*result;
                                             int i=0;
                                             strcpy(path2,path1);
                                             strcat(path2,"\\");
                                             strcat(path2,FindFileData_days.cFileName);
                                             //strcat(path2,".txt");
                                             
                                             strcpy(cmd2,"del "/*"C:\\Users\\xucin\\Desktop\\EW\\earthworm_7.7\\bin\\dumpwave "*/);
                                             strcat(cmd2,path2);
                                             strcat(cmd2," ");
                                             //strcat(cmd2,path2);
                                             //strcat(cmd2,".txt");
                                             printf(path2);
                                             printf("\n");
                                             putenv("EW_LOG=C:\\users\\xucin\\Desktop\\EW\\run\\logs");
                                             //system(cmd2);
                                             
                                             
                                                                                    
                                             txt = fopen(path2, "r+");
                                             result= fopen(strcat(path2,".data"),"w+");
                                             if (txt) {
                                                      printf("檔案開啟成功\\n");
                                                      while (fscanf(txt,"%s",content) != EOF) {
                                                             for(i=1;i<=2;i++)fscanf(txt,"%s",content);
                                                             fscanf(txt,"%s",name);
                                                             for(i=1;i<=8;i++)fscanf(txt,"%s",content);
                                                             fscanf(txt,"%s",starttime);
                                                             for(i=1;i<=2;i++)fscanf(txt,"%s",content);
                                                             fscanf(txt,"%s",endingtime);
                                                             for(i=1;i<=9;i++)fscanf(txt,"%s",content);
                                                             strcpy(data,temp);
                                                             for(int j=0;j<=100;j++){fscanf(txt,"%s",temp);
                                                             strcat(data,temp);
                                                             strcat(data,",");
                                                       }
                                             strcat(output,name);
                                             strcat(output," ");
                                             strcat(output,starttime);
                                             strcat(output," ");
                                             strcat(output,endingtime);
                                             strcat(output," ");
                                             strcat(output,data);
                                             strcat(output," ");
                                             strcat(output,"\n");
                                             }
                                             fprintf(result,output);
                                             
                                             printf("檔案關閉\n");
                                             
                                             fclose(txt);
                                             //Sleep(100);
                                             output[0]='\0';
                                             content[0]='\0';
                                             data[0]='\0';
                                             endingtime[0]='\0';
                                             starttime[0]='\0';
                                             name[0]='\0';
                                           
                                             free(txt);
                                             free(result);
                                             }
                                             else {printf("檔案開啟失敗...\n");}
                                             
                                             
                                             
                                             
                                             if (!FindNextFile(events_years_months, &FindFileData_days)){
                                                 days = true;
                                                 break;
                                             }
                                            
                               }
                               
                               if (!FindNextFile(events_years, &FindFileData_month)){
                                     months = true;
                                     break;
                               }
                              
                               
                               
                }
                if (!FindNextFile(events, &FindFileData)){
                                     years = true;
                                     break;
                }
        months=false;       
        }
        
     
        system("pause");
        return 0;
}
