Ivonne Aspilcueta

Ivonne Aspilcueta

  • 1.1k
  • 594
  • 7.4k

If FileName is blank or empty, delete it.

Jun 25 2024 11:27 PM

Hello,

I need help to figure how to delete a file when it does not have a filename, per example:

 

Code:

using System;
using System.IO;
using System.Linq;

namespace FCCCU318SymitarPreprocessor
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                string inputFilePath;
                string outputFilePath;

                string inputFilePathOnly;
                string fileToImport;

                string strNewLine;

                string reportID;
                string reportName;
                string reportDate;
                string reportTime;
                string FileName;
                string FileTypeNum;

                inputFilePath = args[0].ToString();
                outputFilePath = args[1].ToString();

                inputFilePathOnly = Path.GetDirectoryName(inputFilePath);

                string inputFile = inputFilePath;

                // Read Index.txt file
                using (StreamReader sr = new StreamReader(inputFile))
                {
                    string lineData;
                    lineData = sr.ReadLine();

                    while (!sr.EndOfStream)
                    {
                        lineData = sr.ReadLine();

                        if (lineData.Length > 6)
                        {
                            bool isError = false;
                            try
                            {
                                reportID = lineData.Substring(0, 6);

                                var isNumeric = reportID.All(c => "0123456789".Contains(c));

                                // if the reportId is numeric process the line
                                if (isNumeric)
                                {
                                    // reportName is the OnBase DocType
                                    reportName = lineData.Substring(7, 41).Trim();
                                    if (reportName.Length > 1)
                                    {
                                        if (reportName.Length > 18)
                                        {
                                            if (reportName.Substring(0, 17).ToUpper() == "BATCH OUTPUT FOR ")
                                            {
                                                reportName = reportName.Substring(17);
                                            }
                                        }

                                        reportDate = lineData.Substring(49, 8).Trim();
                                        reportTime = lineData.Substring(61, 5).Trim();

                                        FileTypeNum = "1"; // Assumption is that all the Symitar Reports are text
                                        FileName = reportID;  // Assumption is that the reportID will always be the file name without an extension

                                        fileToImport = inputFilePathOnly + @"\" + FileName;
                                        try
                                        {
                                            if (new FileInfo(fileToImport).Length > 0)
                                            {
                                                // reportName used for DocType and for the Report Name Keyword Type
                                                // DocType hard coded to Symitar Reports Temp as DocType is assigned form the Report Name in the DocType Assignment Workflow
                                                strNewLine = reportID + "|" + "FCCCUReports" + "|" + reportName + "|" + reportDate + "|" + reportDate + " " + reportTime + "|" + FileTypeNum + "|" + FileName;

                                                using (StreamWriter sw = new StreamWriter(outputFilePath, true))
                                                {
                                                    sw.WriteLine(strNewLine);
                                                }
                                            }
                                        }
                                        catch
                                        {
                                            isError = true;
                                        }
                                    }
                                }
                            }
                            catch
                            {
                                isError = true;
                            }
                            if (isError) continue;
                        }
                    }
                    Environment.ExitCode = 0;
                }
            }
        }
    }
}

 


Answers (2)