• Advertisement
Sign in to follow this  

Can someome please tell me why this crashes?

This topic is 989 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to clear some space from my hard drive. I have a few mp4s I downloaded from youtube and I just don't have time to watch them all. I want to delete them to make space on my hard drive but I don't want to lose them as I might not remember the title of a video of I want to go find it again. So I made this program to find all the files with .mp4 in a directory and write the file names to file so I can delete the .mp4s, thus freeing space in my computer. Problem is....the application crashes with this error: 

 

4f39a60f73.png

 

Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace FileData
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "video_filenames.txt";
            DirectoryInfo di = new DirectoryInfo("C:\\Users\\admin\\Documents");

            if (di != null)
            {
                Console.Write("Directory exists");
                FileInfo[] fi = di.GetFiles("*.mp4");
                string str = "";
                if (fi != null)
                {
                    File.Create(fileName);

                    if (File.Exists(fileName))
                    {
                        foreach (FileInfo file in fi)
                        {
                            str += file.FullName;
                        }
                        
                    }
                    File.WriteAllText(fileName, str + "\n");
                }
            }
        }
    }
}

What should I do?
Edited by Enerjak

Share this post


Link to post
Share on other sites
Advertisement

 

My guess (haven't tried running the code) is that you're leaking the file-handle, calling File.Create(), and then later calling File.WriteAllText().  File.Create() will create an empty file, and return you the FileStream pointing to it.  File.WriteAllText() will try to again create the file, which it will not be able to do, because the FileStream has not been Close()'d or Dispose()'d properly.

 

There are a number of things wrong with what you're doing here, though, and it could be written more succinctly like so:

var files= Directory.GetFiles("C:/Users/admin/Documents", "*.mp4");
File.AppendAllLines("video_filenames.txt", files);

It couldn't have been that easy......could it? two lines of code and all my problems are solved? nice. Though What if I wanted to add the number next to the file name to keep track of how many there are?

Share this post


Link to post
Share on other sites

If you happen to have a cygwin shell installed (e.g. if you installed git) you could also just use:

find /c/Users/admin/Documents -name "*.mp4" -exec basename {} \; > video_filenames.txt

Maybe someone can translate that to the Windows cmd equivalent.

Edited by TheComet

Share this post


Link to post
Share on other sites
var files = Directory.GetFiles("C:/Users/admin/Documents", "*.mp4");
for (int i=0; i<files.Length; ++i)
    files[i] = string.Format("{0}: {1}", i+1, files[i]);
File.WriteAllLines("video_filenames.txt", files);
Or, if you're feeling crazy:
 
File.WriteAllLines("video_filenames.txt", Directory.GetFiles("C:/Users/admin/Documents", "*.mp4").Select((x,i) => string.Format("{0}: {1}", i+1, x)));

Thanks, man. That helped a lot. I was going to use C++ but thought using C# would have been far easier to do so. I was right, just was making it complicated I guess.

Share this post


Link to post
Share on other sites

If you happen to have a cygwin shell installed (e.g. if you installed git) you could also just use:

find /c/Users/admin/Documents -name "*.mp4" -exec basename {} \; > video_filenames.txt

Maybe someone can translate that to the Windows cmd equivalent.

Thanks for responding also, oh herald of doom and gloom. 

Share this post


Link to post
Share on other sites

On Windows, use the following on a command-line:

dir *.mp4 /s /b > myFile.txt

Execute in any directory you want to gather all the .mp4s in that directory and all sub-directories, recursively.

Share this post


Link to post
Share on other sites

Maybe someone can translate that to the Windows cmd equivalent.


Powershell:

ls *.mp4 |%{$idx=0}{"{0}: {1}" -f (++$idx),$_.FullName} | out-file video-filenames.txt Edited by fastcall22

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement