Can someome please tell me why this crashes?

Started by
8 comments, last by fastcall22 8 years, 8 months ago

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?
Advertisement

Make sure you dont have the video_filenames.txt file open in notepad (seeing that your screenie has notepad open), maybe thats an issue.

o3o

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);

Eric Richards

SlimDX tutorials - http://www.richardssoftware.net/

Twitter - @EricRichards22

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?


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)));

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.

"I would try to find halo source code by bungie best fps engine ever created, u see why call of duty loses speed due to its detail." -- GettingNifty

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.

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.

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.

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

This topic is closed to new replies.

Advertisement