-2

Taking Hilgreths answer made the code better however the actual problem was a variable data type , changing the type from string to bool to allow for files with no data fixed the issue .

Can't seem to find an answer to this on here or other forums just wondering if there is a way to ignore files of length 0 .

My program is searching through a DIR and returning all the files , I then want to search through the dir and find the most recent file , if a file length is 0 i want to skip to the next file but the system keeps crashing , my code so far looks like

if(fileinfo.Length > 0)
{
    GetLatestWritenFileFileInDirectory(directoryInfo, keywordEH,  keywordINTER, keywordM&M);
}

else if(result.Length == 0)
{

}

very rough at the moment as I'm not looking for it to be written for me (obviously) jus want to know if i can skip the empty files in some way without using linq as I'm using framework 1.0

thanks

user2546071
  • 51
  • 1
  • 10
  • 1
    Why do we have to guess what these variables are? How can we guess what `result` is? Or even `fileInfo`. And why do we have to guess how your code fails. You just said "the system keeps crashing". You were given an error message. Pass it on! – David Heffernan Jul 09 '13 at 09:11
  • possible duplicate of [C# find most recent file in dir](http://stackoverflow.com/questions/1179970/c-sharp-find-most-recent-file-in-dir) – Damith Jul 09 '13 at 09:13
  • @DavidHeffernan didnt include details as didnt think they matter as I'm looking for a way to skip the empty files using the if rather than the end result ... the error given is Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Microsoft.SqlServer.Dts.Runtime.DtsRuntimeException: The type of the value being assigned to variable "User::FileHasNoData" differs from the current variable type. Variables may not change type during execution. Variable types are strict, except for variables of type Object. – user2546071 Jul 09 '13 at 09:16
  • Of course that error makes no sense to us without the real code. You should have made an SSCCE. It's not too late to fix the question. – David Heffernan Jul 09 '13 at 09:17
  • @Damith no I've already looked at that and it doesn't account for empty files – user2546071 Jul 09 '13 at 09:17

4 Answers4

1

Use fileinfo.Length instead of result.Length in the else if statement.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • changed result to fileinfo.length but just wondering how to then tell it to check the next file in the dir as trying to call the function again within the else if just makes it crash – user2546071 Jul 09 '13 at 09:07
0

you can get all the files by calling GetFiles method with SearchOption.AllDirectories

FileInfo[] allfiles = directoryInfo.GetFiles("*.*", SearchOption.AllDirectories);

then do as below

for (int i = 0; i < allfiles.Length; i++)
{
    if (allfiles[0].Length > 0)
    {
        GetLatestWritenFileFileInDirectory(allfiles[0], keywordEH, keywordINTER, keywordM & M);
    }
}
Damith
  • 62,401
  • 13
  • 102
  • 153
0

you should do

   if(File.ReadAllText(yourfileName).Length == 0)
         continue;

And this should be all.

Ehsan
  • 31,833
  • 6
  • 56
  • 65
0

I think you can drop the else if statement, because the FileInfo.Length cant be less than 0.

if(fileInfo.Length > 0)
{
    ////Do your stuff with non empty files
}

This will be enough unless you want to handle the files with length == 0, which seems you don't.

Zee
  • 2,240
  • 4
  • 18
  • 18
  • if the file length is 0 I want to continue the search through the directory , previously tried doing it the way you suggested but still wont work – user2546071 Jul 09 '13 at 09:25