private void OnFormLoad(object sender, System.EventArgs e) { //Event handler for form load try { SvcLog.LogMessage("UTILTIY STARTED"); this.btnGo.Enabled = false; IDictionary oDict = (IDictionary)ConfigurationManager.GetSection("dirs"); int dirs = Convert.ToInt32(oDict["count"]); for (int i = 1; i <= dirs; i++) { oDict = (IDictionary)ConfigurationManager.GetSection("dir" + i.ToString()); string name = oDict["name"].ToString(); string src = oDict["src"].ToString(); string pattern = oDict["pattern"].ToString(); string dest = oDict["dest"].ToString(); string terminal = oDict["terminal"].ToString(); FileMgr oSvc = new FileMgr(name, src, pattern, dest, terminal); if (Program.TerminalCode.Trim() == terminal || Program.TerminalCode.Trim() == "All") { this.cboTerminal.Items.Add(oSvc); } } if (this.cboTerminal.Items.Count > 0) { this.cboTerminal.SelectedIndex = 0; } this.cboTerminal.Enabled = this.cboTerminal.Items.Count > 0; OnTerminalChanged(this.cboTerminal, EventArgs.Empty); } catch (Exception ex) { SvcLog.LogMessage("STARTUP ERROR\t" + ex.Message); } }
public void Execute() { //Execute this service try { //Perform file operations in source folder SvcLog.LogMessage("CHECKING FOR FILES ON " + this.mFtpClient.Server + "/" + this.mFtpClient.RemotePath + "..."); this.mFtpClient.Login(); string[] srcFiles = this.mFtpClient.GetFileList(this.mFilePattern); for (int i = 0; i < srcFiles.Length; i++) { //Rename for uniqueness string srcFile = srcFiles[i]; string destFile = ""; try { if (srcFile.Trim().Length > 0 && srcFile.Substring(0, 1) != "_") { //Move file and rename source destFile = getDestinationFilename(); this.mFtpClient.RenameFile(srcFile, destFile, true); DBLog.LogFileMove(srcFile, destFile); SvcLog.LogMessage("RENAMED FILE\t" + srcFile + "\t" + destFile); } } catch (Exception ex) { SvcLog.LogMessage("MOVE FILE ERROR\t" + srcFiles[i] + "\t" + destFile + "\t" + ex.Message); } } this.mFtpClient.Close(); } catch (Exception ex) { SvcLog.LogMessage("UNEXPECTED ERROR\t" + ex.Message); } }
private ArrayList split(string record) { //Split method that takes into account a token that is not a token //Eg: in the middle of a quoted field like 0123,"jph","Heary, James","Argix","001" //NOTE: Only accounts for a single token within the token- better if the code // handled this case as well: "Heary, James P, Mr." ArrayList fields = null; try { fields = new ArrayList(); string[] tokens = record.Split(new string[] { "," }, StringSplitOptions.None); for (int i = 0; i < tokens.Length; i++) { //Form correct tokens if (tokens[i].StartsWith("\"") && !tokens[i].EndsWith("\"")) { //Add this and next token (see note above) fields.Add(tokens[i] + "," + tokens[i + 1]); i++; } else { fields.Add(tokens[i]); } } } catch (Exception ex) { SvcLog.LogMessage("SPLIT ERROR\tsplit(); " + ex.Message); } return(fields); }
private void reportError(Exception ex, bool displayMessage, bool logMessage) { //Report an exception to the user try { string src = (ex.Source != null) ? ex.Source + "-\n" : ""; string msg = src + ex.Message; if (ex.InnerException != null) { if ((ex.InnerException.Source != null)) { src = ex.InnerException.Source + "-\n"; } msg = src + ex.Message + "\n\n NOTE: " + ex.InnerException.Message; } if (displayMessage) { MessageBox.Show(msg, "PCS File Utility", MessageBoxButtons.OK, MessageBoxIcon.Error); } if (logMessage) { SvcLog.LogMessage("ERROR\t" + ex.ToString()); } } catch (Exception) { } }
private void manageSourceFiles(FileInfo srcFolder) { //Move/copy all files in subfolder <srcFolder> that meet the file pattern try { //Enumerate source folder files string[] srcFiles = Directory.GetFiles(srcFolder.FullName, this.mSrcFilePattern); for (int i = 0; i < srcFiles.Length; i++) { //Move source file; rename for uniqueness FileInfo file = new FileInfo(srcFiles[i]); string destFile = ""; try { if (file.Name.Substring(0, 1) != "_" && file.Length > 0) { //Move file and rename source string srcFilename = file.Name; destFile = getDestinationFilename(); file.MoveTo(destFile); FileInfo _file = new FileInfo(destFile); DBLog.LogFileMove(srcFilename, _file.Name); SvcLog.LogMessage("MOVED FILE\t" + srcFiles[i] + "\t" + destFile); } } catch (Exception ex) { SvcLog.LogMessage("MOVE FILE ERROR\t" + srcFiles[i] + "\t" + destFile + "\t" + ex.Message); } } } catch (Exception ex) { SvcLog.LogMessage("UNEXPECTED ERROR\tDDUFileSvc::manageSourceFiles(); " + ex.Message); } }
private string getDestinationFilename() { //Determine a unique destination filename string filename = ""; try { //Ensure time-based filename is unique (i.e. next second) string year = DateTime.UtcNow.ToString("yy").Substring(1, 1); int day = DateTime.UtcNow.DayOfYear; int seconds = 3600 * DateTime.UtcNow.Hour + 60 * DateTime.UtcNow.Minute + DateTime.UtcNow.Second; filename = this.mDestFolder + year + day.ToString("000") + seconds.ToString("00000") + (this.mDestSuffix.Length > 0 ? this.mDestSuffix : ""); while (File.Exists(filename)) { //Wait until the next second in time System.Threading.Thread.Sleep(1000 - int.Parse(DateTime.Now.ToString("fff"))); //Determine next filename day = DateTime.UtcNow.DayOfYear; seconds = 3600 * DateTime.UtcNow.Hour + 60 * DateTime.UtcNow.Minute + DateTime.UtcNow.Second; filename = this.mDestFolder + year + day.ToString("000") + seconds.ToString("00000") + (this.mDestSuffix.Length > 0 ? this.mDestSuffix : ""); } } catch (Exception ex) { SvcLog.LogMessage("UNEXPECTED ERROR\tDDUFileSvc::getDestinationFilename(); " + ex.Message); } return(filename); }
private void OnFormLoad(object sender, System.EventArgs e) { //Event handler for form load try { //Hide this.Visible = false; this.mIcon = newTrayIcon("Linda Svc", this.Icon); this.mService.Start(); } catch (Exception ex) { SvcLog.LogMessage("STARTUP ERROR\t" + ex.Message); } }
private void OnFormClosed(object sender, System.EventArgs e) { //Event handler for form closed try { //Close task tray icon if applicable; log application as stopped if (this.mIcon != null) { this.mIcon.Visible = false; } this.Visible = false; SvcLog.LogMessage("UTILTIY STOPPED"); } catch (Exception) { } finally { Application.Exit(); } }
private void OnFormLoad(object sender, System.EventArgs e) { //Event handler for form load try { //Hide this.Visible = false; SvcLog.LogMessage("UTILTIY STARTED: user="******"STARTUP ERROR\t" + ex.Message); } }
private void OnTerminalChanged(object sender, EventArgs e) { //Event handler for change in terminal if (this.cboTerminal.SelectedItem == null) { return; } FileMgr svc = (FileMgr)this.cboTerminal.SelectedItem; this.lblSource.Text = svc.SourceFolder; this.lblPattern.Text = svc.SourceFilePattern; this.lblDestination.Text = svc.DestinationFolder; SvcLog.LogMessage("SOURCE FILES FOUND\t" + svc.SourceFiles.ToString()); this.btnGo.Enabled = svc.SourceFiles > 0; }
private string getDestinationFilename() { //Determine a unique destination filename string filename = ""; try { //Ensure time-based filename is unique (i.e. next second) System.Threading.Thread.Sleep(1000); string year = DateTime.UtcNow.ToString("yy").Substring(1, 1); int day = DateTime.UtcNow.DayOfYear; int seconds = 3600 * DateTime.UtcNow.Hour + 60 * DateTime.UtcNow.Minute + DateTime.UtcNow.Second; filename = this.mFilePrefix + year + day.ToString("000") + seconds.ToString("00000") + this.mFileSuffix; } catch (Exception ex) { SvcLog.LogMessage("UNEXPECTED ERROR\t" + ex.Message); } return(filename); }
public void Execute() { //Execute this service try { //Check for the specified source folder SvcLog.LogMessage("CHECKING FOR FILES IN " + this.mSrcFolder + "..."); if (Directory.Exists(this.mSrcFolder)) { //Perform file operations in source folder FileInfo srcFolder = new FileInfo(this.mSrcFolder); manageSourceFiles(srcFolder); } else { SvcLog.LogMessage("DIRECTORY NOT FOUND\t" + this.mSrcFolder); } } catch (Exception ex) { SvcLog.LogMessage("UNEXPECTED ERROR\tExecute(); " + ex.Message); } }
private void manageSourceFiles(FileInfo srcFolder) { //Move/copy all files in subfolder <srcFolder> that meet the file pattern try { //Enumerate source folder files string[] srcFiles = Directory.GetFiles(srcFolder.FullName, this.mSrcFilePattern); for (int i = 0; i < srcFiles.Length; i++) { //Get a source file FileInfo srcFile = new FileInfo(srcFiles[i]); string destFilename = this.mDestFolder + srcFile.Name; try { //Ignore previously processed files (i.e. _xxx) if (srcFile.Name.Substring(0, 1) != "_" && srcFile.Length > 0) { //Validate destination directory if (Directory.Exists(this.mDestFolder)) { //Create an editied version and move to destination folder FileInfo editFile = createEditFile(srcFile); if (editFile != null) { //Success- move edited copy and archive (rename) original source file SvcLog.LogMessage("DESTINATION FILE ARCHIVED AS " + editFile.FullName); editFile.CopyTo(destFilename, true); SvcLog.LogMessage("FILE COPIED\t" + editFile.FullName + "\t" + destFilename); string srcFilename = srcFile.Directory + "\\_" + srcFile.Name + "_s_" + DateTime.Now.ToString("MMddyyyyHHmmss"); srcFile.MoveTo(srcFilename); SvcLog.LogMessage("SOURCE FILE ARCHIVED AS " + srcFilename); } } else { SvcLog.LogMessage("DESTINATION DIRECTORY NOT FOUND\t" + this.mDestFolder); } } } catch (Exception ex) { SvcLog.LogMessage("MOVE/COPY ERROR\t" + srcFiles[i] + "\t" + destFilename + "\t" + ex.Message); } } } catch (Exception ex) { SvcLog.LogMessage("UNEXPECTED ERROR\tmanageSourceFiles(); " + ex.Message); } }
/* * Basic Logging function */ public static async Task LoggingAsync(LogMessage LogMsg) { string CurrentTime = DateTime.Now.ToString(); Console.WriteLine(CurrentTime + " > " + Environment.NewLine + LogMsg.Message); if (Config.LogWithoutStamp == false) { SvcLog.Logged += "\r\n" + CurrentTime + " > " + Environment.NewLine + LogMsg.Message; } else if (Config.LogWithoutStamp == true) { SvcLog.Logged += "\r\n" + Environment.NewLine + LogMsg.Message; } else if (Config.LogWithoutStamp == null) { Console.WriteLine("\r\nValue cannot be null."); return; } else { Console.WriteLine("\r\nValue is invalid, but not null."); return; } SvcLog.DumpLog(); string[] HTTPMethods = { "DELETE", "GET", "PATCH", "POST", "PUT" }; if (!HTTPMethods.Contains(LogMsg.Message.Split(' ').First())) { EmbedBuilder Embed = new EmbedBuilder(); Embed.WithTitle(LogMsg.Source + " > " + CurrentTime); Embed.WithColor(new Color(236, 183, 4)); Embed.WithDescription(LogMsg.Message); const int Delay = 500; await Task.Delay(Delay); await((IMessageChannel)_DiscordClient.GetChannel(Config.BotChannelId)).SendMessageAsync(String.Empty, false, Embed.Build()); } }
private FileInfo createEditFile(FileInfo srcFile) { //Edit file contents FileInfo editFile = null; StreamReader reader = null; StreamWriter writer = null; try { //Edit per config string destFile = srcFile.Directory + "\\_" + srcFile.Name + "_edit"; Hashtable dict = (Hashtable)ConfigurationManager.GetSection("terminals/" + this.mTerminal); writer = new StreamWriter(new FileStream(destFile, FileMode.Create, FileAccess.ReadWrite)); reader = new StreamReader(new FileStream(srcFile.FullName, FileMode.Open, FileAccess.Read)); reader.BaseStream.Seek(0, SeekOrigin.Begin); string record = null; while ((record = reader.ReadLine()) != null) { if (dict.Count > 0) { //Parse record into fields; update cust field value if required ArrayList fields = split(record); string newRecord = ""; for (int i = 0; i < fields.Count; i++) { if (i == 0) { newRecord += fields[i]; } else if (i == 5) { if (dict.ContainsKey(fields[5])) { newRecord += "," + "\"LTA\"" + "," + dict[fields[5]].ToString(); } else { newRecord += "," + fields[5] + "," + fields[6]; } i++; } else { newRecord += "," + fields[i]; } } writer.WriteLine(newRecord); } else { //Pass through writer.WriteLine(record); } } writer.Flush(); editFile = new FileInfo(destFile); } catch (Exception ex) { SvcLog.LogMessage("EDIT ERROR\teditContents(); " + ex.Message); } finally { if (reader != null) { reader.Close(); } if (writer != null) { writer.Close(); } } return(editFile); }