Hello,
I need help on how to create a code to delete the 0 Bytes files once I run the code and after create the files. Thank you.
C#
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;
}
}
}
}
}

Amit MohantyRank #112 · 20K reputationPosted Jun 24, 2024, 1:50 PM EDT
Check this:
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]; 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)); // 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 = Path.Combine(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; } } } // 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}"); } } } } } }Ivonne AspilcuetaRank #4,887 · 320 reputationPosted Jun 24, 2024, 2:12 PM EDT
@Amit Mohanty Thank you so much for teaching me!