I need help to figure how to delete a file when it does not have a filename, per example:
004249 and 004252 do not have a file name and I would like to delete them. I hope I have added all the things you will need to help me with this part of my code. Thank you.
INDEX.TXT 10/19/23 23:28 004253 Batch Output for FCCCU.FTP.REPORTS 10/19/23 23:28 004246 WRG.RDC.TRACKING.FM 10/19/23 23:22 004247 WRG.RDC.TRACKING.FM Journal 10/19/23 23:22 004248 WRG.RDC.TRACKING.FM Exceptions 10/19/23 23:22 004249 10/19/23 23:22 004250 OPS.FILECREATE 318BACKUP1 23:22 10/19/23 23:22 004251 Batch Output for FCCCU.PRIMARY.RUN.1 10/19/23 23:22 004252 004254 ACH.PREPROCESSING 10/19/23 23:40
This is my code:
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];
outputFilePath = args[1];
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)); //is it an specific reprotID?*******
// 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);
}
}
//if (!File.Exists(reportName))
//{
// File.Delete(reportName);
//}
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 = Path.Combine(inputFilePathOnly, FileName);
try
{
if (new FileInfo(reportName).Length == 0)
{
File.Delete(FileName);
}
if (new FileInfo(fileToImport).Length > 0)
{
// reportName used for DocType and for the Report Name Keyword Type
// DocType hard coded to Reports Temp as DocType is assigned form the Report Name in the DocType Assignment
strNewLine = reportID + "|" + "FCCCUReports" + "|" + reportName + "|" + reportDate + "|" + reportDate + " " + reportTime + "|" + FileTypeNum + "|" + FileName;
using (StreamWriter sw = new StreamWriter(outputFilePath, true))
{
sw.WriteLine(strNewLine);
}
}
}
catch
{
//ExceptionDispatchInfo.Capture(e).Throw();
isError = true;
}
}
}
}
catch
{
//ExceptionDispatchInfo.Capture(ex).Throw();
isError = true;
}
if (isError) continue;
}
}
}
// Delete 0-byte files
DeleteZeroByteFiles(inputFilePathOnly);
Environment.ExitCode = 0;
}
}
static void DeleteZeroByteFiles(string directoryPath)
{
var files = Directory.GetFiles(directoryPath);
foreach (var file in files)
{
if (new FileInfo(file).Length == 0)
{
try
{
File.Delete(file);
}
catch (Exception ex)
{
Console.WriteLine($"Failed to delete {file}: {ex.Message}");
}
}
}
}
}
}
I tried to implement it when the files are being created, but it did not work:
if (new FileInfo(reportName).Length == 0)
{
File.Delete(FileName);
}