c# - How to avoid duplicate files (of different extensions) when auto generating new files -


i have created program cleans access , error logs , outputs them in new file in same directory. input in format .txt , output in format .csv - giving me 2 output files, 1 in .csv format , 1 in .txt format(.txt file empty) instead of .csv file? can't understand why happening. below 2 ouput files shown in directory:

the 2 files generated code:

below code generates new file unique name:

 static filestream createfilewithuniquename(string folder, string filename, int maxattempts = 1024)         {             var filebase = path.getfilenamewithoutextension(filename);             var ext = path.getextension(filename);              // build hash set of filenames performance              var files = new hashset<string> (directory.getfiles(folder));      (var index = 0; index < maxattempts; index++)          {             // first try original filename, else try incrementally adding index             var name = (index == 0)                 ? filename                 : string.format("{0} ({1}){2}", filebase, index, ext);              // check if exists              var fullpath = path.combine(folder, name);              if(files.contains(fullpath))                 continue;              // try create file             try             {                  return new filestream(fullpath, filemode.createnew, fileaccess.write);             }             catch (directorynotfoundexception) { throw; }             catch (drivenotfoundexception) { throw; }             catch (ioexception)              {              }           }     throw new exception("could not create unique filename in " + maxattempts + " attempts");        } 

and code below code reads in existing file , cleans it:

public static void readfile(string filename)     {         using (var stream = createfilewithuniquename(@"c:\users\michael\desktop\windowsformsapplication1\windowsformsapplication1\bin\debug\", filename))         {             console.writeline("created \"" + stream.name + "\"");              newfilename = stream.name;             globals.cleanederrorfilename = newfilename;         }          string csvfilename = path.changeextension(newfilename, ".csv");         streamreader reader = new streamreader(filename);         streamwriter writer = new streamwriter(csvfilename);         string line;          string personalidentifier = new string(filename.take(4).toarray());         string gender = filename.substring(filename.length - 5, 1);         string classification = filename.substring(filename.length - 8, 2);         string text = string.empty;           while ((line = reader.readline()) != null)         {             string[] cleanarray;             cleanarray = new string[5];              var result = line.split('[')                  .select((element, index) => index % 2 == 0                   ? element.split(new[] { ' ' }, stringsplitoptions.removeemptyentries)                   : new string[] { element })                  .selectmany(element => element).tolist();              cleanarray[0] = personalidentifier;             cleanarray[1] = gender;             cleanarray[2] = classification;             cleanarray[3] = result[0];             cleanarray[4] = result[2];              cleanarray[4] = cleanarray[4].substring(7);             cleanarray[4] = cleanarray[4].replace("]", " ");             cleanarray[4] = cleanarray[4].insert(15, ",");              cleanarray[3] = cleanarray[3].remove(cleanarray[3].length - 2);              cleanarray[4] = cleanarray[4].substring(0, cleanarray[4].indexof(":") + 1);               //re-formatting date can accepted machine learning             var datestring = cleanarray[3];             var date = datetime.parseexact(datestring, "ddd mmm dd hh:mm:ss yyyy", cultureinfo.invariantculture);             var newdatestring = date.tostring("yyyy-mm-dd hh:mm:ss");              //inserting new date , time array             cleanarray[3] = newdatestring;              //push each clean array onto file has been automatically created @ top             writer.writeline(string.join(", ", cleanarray.select(v => v.tostring())));             writer.writeline();         } 

i'm hoping issue small can't seem find it! in advance!!

this line culprit:

return new filestream(fullpath, filemode.createnew, fileaccess.write); 

at point, file name in fullpath still has .txt extension, creates empty .txt file. change extension .csv , this:

streamwriter writer = new streamwriter(csvfilename); 

which creates new .csv file

also, both streams never closed in code.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -