Jump to content

c++ პროგრამის სერვისად გაშვება


Recommended Posts

მოკლედ ბევრი ხლაფორთით და სხვების დახმარებით დავწერე 186 ხაზიანი პროგრამა c++ ში   :bis:

პროგრამა რომელიღაც დირექტორიაში კითხულობს ფაილებს და იქიდან თარიღის საშუალებით ქმნის დირექტორიებს წელი/თვე/დღე/ და აქ აგდებს ამ ფაილს.

ეს ფაილები მუდმივად შემოდის და საჭიროა რომ ჩემმა პროგრამამ სულ იმუშაოს. გადავწყვიტე რო გამეშვა სერვისად თუმცა ბევრი ძებნის შემდეგ აღმოჩნდა რომ კოდი უნდა გადამეკეთებინა. ამის შესაძლებლობა არ მაქ და არც დრო ამიტომ მოვძებნე სხვა solution  -- >  http://www.davidjnice.com/articles/windows-server_run-as-service.html

და ეს ჩემი პროგრამა გაეშვა სერვისად. იხილეთ სურათზე

 

38837f900fb5.jpg

 

მაგრამ პროგრამა თავის საქმეს აკეთებს მხოლოდ გაშვებისას, მერე კი რომ ჩავაგდო ისეთი ფაილი რაც უნდა დაამუშაოს და გადაიტანოს თავის შექმნილ საქაღალდეში, ეს პროგრამა არ აკეთებს ამას. ხოდა ვერ დამეხმარებით? იქნებ რამე პროგი იყოს რო გადააკეთოს კოდი სერვისად, ან იქნებ პატარა რედაქტირება უნდა და ამიხსნათ როგორ ვქნა ან სხვა solution რო იყოს. ერთი ვიფიქრე windows shedular-ში ჩავამატო ყოველ 4 წუთში მეთქი (ის ფაილები ყოველ 1 საათში შემოდის, მაგრამ შეიძლება ზოგჯერ ხელით ჩაყარონ) მაგრამ მაგას ბოლო ვარიანტად ვიტოვებ. 

  • Upvote 1
Link to comment
Share on other sites

source კოდი დადე პირველ რიგში და მერე დაგეხმარებიან 

  • Upvote 1
Link to comment
Share on other sites

უსასრულო ლუპი არ გაქვს გაშვებული რო არ გაითიშოს? სორსი დადე და შევხედავთ ;)

  • Upvote 1
Link to comment
Share on other sites

source კოდი დადე პირველ რიგში და მერე დაგეხმარებიან 

 

 

უსასრულო ლუპი არ გაქვს გაშვებული რო არ გაითიშოს? სორსი დადე და შევხედავთ ;)

ესეც source code. აუ ტab-ები არ აიღო :(  აჰა ჩავასწორე

 

#include <io.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <fstream>
#include <dirent.h>
#include <string.h>
#include <windows.h>
 
using namespace std;
string time ("Time, Georgia Time");
 
size_t get_column_index(string str, string column_name){
   size_t contain;
   int count = 0, character_index, i = 0 ;
   character_index = str.find(column_name, 0);
   string nawili = str.substr (0, character_index);
     while((contain = nawili.find(""",i)) != string::npos){
        count++;
        i = contain + 1;
     }
return ((count + 1) / 2);
}
 
size_t get_comma_index(string str, size_t comma_number){
   size_t contain;
   int i = 0, count = 0;
   while((contain = str.find(",",i)) != string::npos){
      count++;
      if ( comma_number == count ){
          return contain;
      }
   i = contain + 1;
   }
return 0;
}
 
string get_column_value(string str, size_t column_index){
   size_t pirvelicomma = get_comma_index(str, column_index-1);
   size_t meorecomma = get_comma_index(str, column_index);
   str = str.substr(pirvelicomma + 1,meorecomma-pirvelicomma - 1);
   return str;
}
 
bool is_correct_format(string filename){
   int curline = 0;
   string str;
   ifstream file(filename.c_str());
   while (getline(file, str)){
      if (str.find("dateTimeDelimiter", 0) != string::npos){
         return true;
      }
   curline++;
      if (curline > 3){
         return false;
      }
   }
return false;
}
 
string get_time(string file_name){
   size_t time_index;
   bool csvheader = true;
   ifstream file(file_name.c_str());
   string str, rtrn;
   while (getline(file, str)){
          if (str.find(""#","Time,", 0) != string::npos){
             csvheader = false;
             time_index = get_column_index(str, time);
             continue;
         }
         if(csvheader == false){
             rtrn = get_column_value(str, time_index);
             break;
         }
    }
return rtrn;
}
 
string get_ymd(string date, string hint){
   string day, month, year;
   int space_pos, slash1_pos, slash2_pos;
   space_pos = date.find (' ', 0);
   slash1_pos = date.find ('/', 0);
   slash2_pos = date.find ('/', slash1_pos + 1);
   if ( hint == "month" ){ 
      day = date.substr(0, slash1_pos);
      return day;
   }
   if ( hint == "day" ){
       month = date.substr(slash1_pos + 1, slash2_pos - slash1_pos - 1);
       return month;
   }
   if ( hint == "year" ){
      year = date.substr(slash2_pos + 1, space_pos - slash2_pos -1);
      return year;
   }
}
 
size_t check_folder (string str_path){
    if ( access( str_path.c_str(), 0 ) == 0 ){
       struct stat status;
       stat( str_path.c_str(), &status );
       if ( status.st_mode & S_IFDIR ){
           //cout << "folderi arsebobs" << endl;
           return 1;
       }else{
           //cout << "mititebuli misamarti failis saxelia" << endl;
           return 3;
       }
   }else{
       //cout << "folderi ar arsebobs" << endl;
       return 2;
   }
}
 
int main(){
   DIR *dir;
   struct dirent *ent;
   size_t len;
   string str;
   string date;
   int space_pos;
   //ofstream out("output.txt");
   string day, month, year, folder;
   string source_file_name, source_file_path, dest_file_path;
   if ((dir = opendir ("C:UserssysopDesktopmate")) != NULL) {
       while ((ent = readdir (dir)) != NULL) {
           len = strlen (ent->d_name);
           if (len >= 4){
               if (strcmp (".csv", &(ent->d_name[len - 4])) == 0){
                  str = ent->d_name;
                  str = "C:UserssysopDesktopmate""" + str;
                  if (is_correct_format(str)){
                     date = get_time(str);
                      day = get_ymd(date, "day");
                     month = get_ymd(date, "month");
                    year = get_ymd(date, "year");
                     folder = "C:UserssysopDesktopmate20" + year;
                    source_file_name = ent->d_name;
                    source_file_path = "C:UserssysopDesktopmate""" + source_file_name;
                    if ( check_folder(folder) == 1 ){
                        folder += "" + month;
                        if (check_folder(folder) == 1){
                           folder += "" + day;
                           if (check_folder(folder) == 1){
                                 //vakopireb fails
                                dest_file_path = folder + "" + source_file_name;
                                 MoveFile(source_file_path.c_str(), dest_file_path.c_str());
                              }else{
                                 CreateDirectory (folder.c_str() , NULL);
                                 //vakopireb fails
                                 dest_file_path = folder + "" + source_file_name;
                               MoveFile(source_file_path.c_str(), dest_file_path.c_str());
                              }
                          }else{
                             CreateDirectory (folder.c_str() , NULL);
                              folder += "" + day;
                              CreateDirectory (folder.c_str() , NULL);
                              //vakopireb fails
                              dest_file_path = folder + "" + source_file_name;
                              MoveFile(source_file_path.c_str(), dest_file_path.c_str());
                           }
                        }else{
                            CreateDirectory (folder.c_str() , NULL);
                            folder += "" + month;
                            CreateDirectory (folder.c_str() , NULL);
                            folder += "" + day;
                            CreateDirectory (folder.c_str() , NULL);
                            //vakopireb fails
                            dest_file_path = folder + "" + source_file_name;
                            MoveFile(source_file_path.c_str(), dest_file_path.c_str());
                        }
                    }
                }
          }
     }
     closedir (dir);
  }else{
     perror ("");
     return EXIT_FAILURE;
  }
  return 0;
}

Link to comment
Share on other sites

კოდი არ წამიკითხია მაგრამ დარწმუნებული ვარ არასწორია

 

სერვისად გაშვება არ არის გამოსავალი ამ შემთხვევაში. ლისენერი და ჰენდლერი დააყენე ფოლდერზე რო როცა ახალი ფაილი ჩავარდება გაუშვას ახლიდან კოდი. ან თუ ვერ მოხაერხე ჰენდელრის დაყნებეა მაშინ დრო დააყენე რო ყოველ რამდნიმე წუთში გადაამომწოს და ჩაყაროს ფოლდერებში

  • Upvote 1
Link to comment
Share on other sites

ეს კოდი ერთ გაშვებაზე იმუშავებს და შენ თუ გინდა რო სულ უთვალთვალოს რაღაც დროის ინტერვალიან უსასრულო 
ციკლში ჩასვი:

 bool done = false;
 while(!done)
 {
     if ((dir = opendir ("C:UserssysopDesktopmate")) != NULL)
     {
        ...
     }
     ...
     Sleep(5000); // 5 წამი მაგალითად
} 

და შიგნით შეგიძლია done-ით აკონტროლო როდის მორჩეს ციკლის მუშაობა...

 

P.S. ეს მაგალითი შეგიძლია სცადო კარგადაც არის ახსნილი:

http://www.codeproject.com/Articles/499465/Simple-Windows-Service-in-Cplusplus

  • Upvote 1
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.