Foreword

f2fguiexample_0 I have since taken up learning Visual Basic and have converted the file2folder script to a feature-filled application called file2folder GUI (original, eh?).  You can read more about it at the Google Code site.  Download is available in the downloads section, or at Google Code.

Otherwise, the script is still available…read on if that’s what you’re interested in…

I keep seeing this crop up more and more as the popularity of Media Browser increases.  It has to do with those that have large movie collections, but do not have their individual movie files (in AVI, MKV, MP4, DIVX, WMV, etc.) in a folder of the same name – which Media Browser requires.

I faced this same issue also when I was wanting to get away from MyMovies.  I had all of my movies thrown into a couple of categorized directories, but that was it.  I couldn’t imagine manually creating 1500 folders for my movies, so I called upon my batch scripting abilities (I’m a systems administrator by trade) and came up with this little snippet of code that I still use to this day (I have a default folder that I convert all of my new DVD/BluRay discs to and then I run this once the output file is completed).

@echo off
for %%a in (*.*) do (
 	md "%%~na" 2>nul
 	move "%%a" "%%~na"
)

Just copy this into a text file and rename it to a .bat extension.  You will place the .bat file into the directory containing your movie files and then double-click it.  It will automatically create a new folder of the same name of each of your movie files and then move that file into it.  Voila!  If you have any subtitles that are named the same as your video files, they will get moved into the same folder as well (any file of the same name will, actually).  So if you have a movie called The Transporter.avi, a The Transporter.srt subtitle file, and a The Transporter.jpg image file inside your Movies directory, when you run the script all three files will be moved to Movies\The Transporter directory.

If you have multiple directories, you will need to do the same for each of them as this is not recursive code.  I’d rather it not be so that you have some level of control over it.  I also would not run this in any TV series folders as it will throw your individual episodes into their own folders.  Also, if you have any of your files marked as hidden or read-only, they will not get moved.

Warning! Depending on where you have your movies stored and/or what is in your movies directory, this could be a very dangerous script.  Keep in mind that this script will move EVERYTHING within the directory it is run in except for hidden and read-only files. Due to the number of issues, do not not run this against network locations unless the drive is mapped to a local drive letter. End Warning!

Anyway, hope this helps some of you out!

Update: I have not been able to reproduce this on at least a half-dozen systems using four different OSes (2008 Server x64, 2003 x86/x64, Vista Ultimate x86/x64 and XP x86, but I thought I should put it out since at least one person has commented on it.  Apparently this script created odd-named folders and did not move any files.  As I said, I have not ever had any issue with this script and it has moved over 2000 movies for me.  Anyway, if you plan to use this, please test it out with some dummy files first (you can use any kind of files, just make sure you do it within a safe folder).

Update 2: It appears there may be an issue with copying the code off this post and inserting it into your own .bat file.  So, in order to reduce (or resolve) the issues with it, I’m including the exact .bat file that I use.  So far, there have been no reported issues with it.

You can get it here (RAR format).

  76 Responses to “File-to-folder script.”

Comments (72) Pingbacks (4)
  1. This didn’t work for me.. Running Vista Home Premium. Would’ve been nice, but it ended up creating me more hassle.. It split all the names up, and created tons of extra folders, e.g. Benjamin Button became óbenjamin and óbutton along with tons of other movies. I still don’t know if any of the folders i deleted had any film in them, it created over 200 folders, and i was DAMNED if i was gonna search every one of them individually, i decided to just cut my losses and deleted the lot. The ones i did check had nothing in them, so no files were moved AFAIK. It just created a whole heap of uselessly named folders..

    To end, Once Upon A Time In America, created about 12 extra folders, where the words ‘a’ & ‘in’ already appeared in other file names.. So Home Premium Vista? Stay away..

    • Not sure why it didn’t work for you, I just copied the code into a batch file and ran it against a bunch of files here at work and it worked perfectly (I even created a “Once Upon A Time In America.avi” dummy file and it worked like it always does). So far, it has worked for me on XP 32-bit, Vista Ultimate 32 and 64-bit, 2008 Server 64-bit and 2003 Server 32 and 64-bit. It also works over the network and locally for me (my network location is actually a linux-based server using ReiserFS) I have not made any changes that would directly affect the functionality of this script. Considering the special characters that are being created in your folders, I would have to say something else is wrong.

  2. I had the same issue on Windows 7 Beta (build 7000) I just created a bat named movie_folders.bat using the text from above and got odd named folders with no files in them. I took a screenshot for review here: http://sites.google.com/site/hack7mc/images/share/movie_folders.png

    • You guys have me stumped, I just can’t recreate this and I have no idea what could cause that. Do you have UAC enabled (not that I think that would cause it either, but I don’t even know where to start)? Here is a link to the file2folder.bat that I have used on no less than a half-dozen systems without issue. If either of you want to give it a try and let me know if the same thing happens, I’d appreciate it. Download. Make sure to set the bat file to hidden if it doesn’t carry over.

  3. The downloaded file seems to work just fine when I tried it this time. I have no idea what the problem could have been though. Copy and pasting the text above didn’t work but typing it out myself in notepad did. Odd.

  4. One other thing, do you mind if I post your .bat file with some instructions and links back to this post on my site? I’ll rehost the .bat file so it doesn’t use any excess bandwidth of yours as well.

    • That is odd. I wonder if there is some weird formatting caused by copying the code off of the site (hidden breaks or something)? That would make sense, but I actually did just that and it still worked for me. I’ll try to edit it so that it displays with absolutely no formatting and just go ahead and link the batch file in my post, using the code as an example of its content.

      Post the .bat at will. It’s just common DOS :)

  5. Perfect!!
    Searched in google for an app to move files for MyMovies and found this – the power of batch files is so overlooked!
    Been in IT for 20+years and used to use DOS bat’s all the time, didn’t even occur to me – how lazy is Windows making us ???

  6. I had the same problem as others. When I created the batch script based on your text above it would create the folders but not move the files into them. When I run the bat that you rar'ed and attached to your site it works perfectly.

    Thank you so much for this. I can finally try out Mediabrowser. Whats the best meta tool and could you post a guide to using your favorite?

    • The best is relative. I actually use a combination of Salami’s Movie Organizer and Meta Browser for movies and still use MediaScout for TV.

      I use SMO still because it provides more accurate data using IMDB than tMDB and allows me to pick the poster I want from IMPAwards. After saving there, I open it up in Meta Browser to fill in the blanks (parental rating and whatnot), grab backdrops and actor images. MediaScout has always been my preferred TV fetcher, but I’m starting to let Meta Browser take care of that duty also since MS is no longer developed or supported.

      My best advice is to try them all and find one you like best. I have several posts announcing new tools that have been released over the past couple of months and I believe I have them all linked on the plugin index.

  7. Found this post by googling.
    This script is EXACTLY what I’ve been looking for!
    I have to sort a collection of about 12,000 files and I almost gave up on sorting it!
    You’re a genius!

  8. Just tried this & it worked perfect for me – Saved me LOADS of time creating a movie database – Thank you!

  9. Anybody know the script to do this in linux? All my stuff is on my linux server and the DOS command wont run even if i do it via the network drive on my XP machine or 7mc THANKS

  10. Bash script for the linux kiddies

    *use at your own risk*

    /bin/bash

    DIR=`pwd`

    find $DIR -maxdepth 1 -type f | while read FILE
    do
    FOLDER=`echo $FILE | sed ‘s/\(.*\)…./\1/’`
    echo ‘this is my file’
    echo $FILE
    echo ‘this is my folder’
    echo $FOLDER
    echo ‘==============’
    mkdir “$FOLDER”
    mv “$FILE” “$FOLDER”
    done

  11. its a wonder! I was really worried about how i am gonna find the time to create folders for all my folderless files and out of the blue you came up with a nice and convenient way of doing it.

  12. Please help me.
    I have all my music and video files on the network (NAS drive)
    I like to do identical process as this batch is doing, but on the network.

    I used this batch on my PC, work excellent, but when I try to use this batch on my NAS drive it went south.

    I copied batch to NAS to same directory as my moovies. After execution of batch, nothink happend, but later I found it actually mooved my system files from Windows folder.
    PLEASE DO NOT TRY IT AS I DID. It will destroy your OS.

  13. Windows 64 bit, ulimate – a success story after trials and tribulations.

    I tried this initially. Didn’t work – just created a bunch of empty folders.

    Poked around for another similar utility. No luck. Wasted an hour doing it.

    Came back and actually looked at the error messages as it ran – “Access Denied”.

    Turned off UAC (required a reboot). Ran the script. Worked perfectly.

    Turned back on UAC. 10 minutes and I’m very, very happy!

    Now I’m going to go run XBMC’s Media Companion and finish something that would have taken me weeks to do on my own.

    THANK YOU FOR THIS SCRIPT!!!!!!!!

  14. Hi, nice script :)

    I was just wondering if you had a script to undo what the script did?

    Cheers

    • Unfortunately, no. It’s possible if you output all changes to a text file so that it can be reversed, but I’ve never need to go that far with it.

  15. Hi, I found this due to having so many downloads, not movies, but files. I am a freeware junkie and let me say, this little script is exactly what I’ve been looking for. I create icons for software, like Malwarebytes anti malware icon and others, plus do other graphics work and this will come in handy for all my files there as well. It’s too bad you couldn’t create the undo, add some features, create a small app out of this, hell, I’ll even make the icons and everything if you want. This has to be the handiest script I’ve run across in a long time. Thank you for this!

  16. Firstly thanks for the script, however I’m unable to run it successfully on my windows home server. Giving me Access Denied error message. Any suggestions?

    • Make sure you are running it locally and not over the network. Make certain you have permissions to move files on the location you are running the script. Finally, make sure that the files you are attempting to move are not in use. Other than that, there is no reason for it not to work. There’s nothing complicated going on with it at all.

      • I too am getting Access is Denied on each file. I even ran the .bat by right-clicking and choosing “Run as Administrator”

        I would really love to get this to work – any thoughts?
        Windows 7 Home Premium 32-Bit – Running locally.

        • UPDATE: When working on a partition of the local drive it works. When working on an external RAID (where all my media lives) it does NOT work. I’ve compared the settings (security, etc.) of the two folders and I can’t find any differences.

          • Have you mapped the network drive to a local drive letter? If not, do so. DOS scripts do not work with UNC path names. There’s a big WARNING in the middle of the post that states this.

          • Indeed. It’s a USB drive that is mapped to the R:\ path.

          • Has to be permissions if the drive is mapped to a local drive letter then. The script is too simple to fail from anything but permissions or invalid paths. Have you tried the newer GUI version? You should try using that (if on Windows) as it works with UNC path names. Using Run As Administrator against mapped drives doesn’t work well either since drive mapping is per user.

          • It was security issues. Well, I should say Windows 7 issues. The permissions had been set on the entire drive (root and all subs) but for some reason I had to re-do full access on each individual media folder before it actually worked.

  17. I too had the issue where it moved everything in c:\windows rather than the directory it was in. It might have something to do with the quote marks you use. My issue was on WHS (Windows Server 2003).

    Time to rebuild :(

    • There’s only an issue if you run it against a network share that is not locally mapped to a drive letter. The quotes are necessary if you have space in any of your movie names or paths.

  18. I wasn’t able to get the above Linux script to work so figured another way to do it.

    this is for Linux, not windows. This will work provided there is only one file associated with the movie. If you have other stuff like folder.jpg or something it probably won’t be suitable.

    Thanks,

    #!/bin/bash
    find . -name '*.*' | \
    while read filename
    do
          mkdir "${filename%.*}"
          directory=${filename%.*}
          mv "$filename" "$directory"/"$filename"
    done
  19. It seems when posting on here the tabbed spaces are removed which cause the above linux scripts to not work, as previously mentioned by others.. guess I didn’t see that.

    The lines starting with mkdir, directory, and mv, are tabbed spaced.

    • I fixed it for you. WordPress does this by design…it’s not so much a bug (but an inconvenience either way). You can copy in tabbed spaces so long as you enclose you enclose it within <pre></pre>.

      I also used the code tag since not using can introduce some oddities when copying text directly from WordPress…especially script code.

  20. Update 2:

    It appears there may be an issue with copying the code off this post and inserting it into your own .bat file. So, in order to reduce (or resolve) the issues with it, I’m including the exact .bat file that I use. So far, there have been no reported issues with it. You can get it here (RAR format).

    Hi Guys this file no longer exists. Can anyone help.

  21. I used this and it worked perfectly, thanks!

    But – how would I reverse this action?

    Is their a script that can undo this, and take the files back out of folders??

    I ask, because I may one day not want Thumbnails, and want my movies in list format as they were before I ran this script.

    Thanks

    • I don’t have anything to undo it. You could do a simple search by extensions (ex.: *.avi, *.mkv) and then cut and paste all the found files to a different directory quite easily though.

  22. this file doesn’t work for me at all. I was so disappointed, I have soo many movie files I need to organize and was hoping this would’ve helped. Tried it on my local hdd’s and also on my external hdd’s… absolutely didn’t work. Oh wells. guess I have to find another way. thanks anyways jon

  23. Just a note that

    Movie.avi
    and
    Movie.avi.properties

    will not be sorted into the same directory. Note sure how many others have files setup like this, but I just wanted to note it. Otherwise, works great! Thanks.

  24. I suspect the reason that some people have it work and others don’t is due to the fact some editors mess with the ” when it sees pairs of them. If they are converted to open ” followed by a close ” then the command process (thing that runs bat files) will see files with spaces in them as multiple parameters instead of one.

    Pasting the script into notepad and checking that the ” is a normal one as per the standard shift ‘ should make this issue go away.

  25. most excellent! thanks a lot man. saved me soooo much time and aggravation. :D

  26. Thanks for this program. Just saved me a lot of time!

  27. Try Rar file download in test folder as suggested. Worked perfectly. Thankyou for the scripted.

  28. Just saying thanks! This saved me from creating 200+ folders, exactly what I needed! You rock

  29. Using Windows 7 Premium x64 with all updates, worked perfectly for me. I also had 200+ movies that I wanted to organize. I started creating folders and copying file names and moving and after 7 of them I gave up. Did a search and this worked perfectly! Thanks so much!

    • Oh and I downloaded the file, didn’t create my own. Plus I had to make hidden files visible. .bat files are hidden by default I guess.

      • .bat files aren’t hidden by default, I specifically set the file2folder .bat to hidden so that when it is run, it doesn’t get put in a folder also.

        It’s was by necessity :)

  30. Works great, even on Windows 7 Professional x64.
    Thank you so much for this. I had about 3000 movies which had got subtitles and covers and they were all rounded up into folders.
    You’re a genious, whoever you are. Again, thanks a lot.

  31. Bloody awesome, worked great, thanks!

  32. Freakin A Man!

  33. I have a similar desire, maybe you can help? I have a txt file of thousands of music artists that I would like to make a directory structure out of. Any way to pipe a delimited file into the md command?????

    • Create a batch file with the following and save it as folderlist.txt

      for /f %d in (folderlist.txt) do md %d

      run this from the command prompt within the directory that contains that text file and it will create the directories. The text file should be a line-by-line list of each folder (artist) name.

  34. Works Beautifully Thank You So Much

  35. thanks, worked flawlesly, saved lot of time. thanks once again.

  36. Worked like a charm! Thanx

  37. It worked perfectly! just to give you an idea, i used it in one folder with hundreds jpeg and it created all subfolders named as the pictures, with the files inside the folders. The nice fact, is that I ran the batch file in a WinXP session thorugh Parallel under Mac OS X Snow Leopard!

    • PS= if you could write the similar script for Automator on Mac, then i would not need Parallel anymore and i would be more super grateful than what I already am! THANKS

  38. Just ran this against 2900 Roms and guess what it worked perfectly!!!

    Many thanks for this cute little script really really handy to have for future reference!!

    thanks a mill

  39. This is fantastic. I even ran it on an unmapped network (I may have clicked ‘start’ before reading the whole page, oops!) with no issue. Amazing work.

  40. Wow, you are good! Willing to take a shot at a more complex example? I have a folder with 4000 files in it named like this – 1811055_FILE_xxxxxxxxx_20110922.tif (or .xml). The xxxxxxxxx is a unique file number and there are mutiple files for each number. I previously created new folders and moved the files in manually. I can’t figure out how to substring the file number within the batch file. Thanks so much if you can help.

  41. Congratulations ! Please can you help me here? I have several files with names separate with _, like 12234_XXX.TIF. it is possible to create folder named 12234 after subfolder XXX and then move the file on it ?

    Thank you for your time.

  42. I used this for my hundreds of ebooks to put each books in the author’s folder. Unfortunately my books are currently names like this:

    Shakespierre, William – Othello
    Homer – Illiad, The
    McEwan, Ian – Atonement

    But I ran your BAT file idea and it worked perfectly for me, although I now had one folder for each book of each author, rather than one folder for all the author’s works. Most of the works I have are single-book authors, so as a workaround I did this:

    Go to main folder with all the Shakespierre book-folders, change the view to Sort by Type, and cut all the book files. I pasted them in one of the Shakespierre folders and renamed it William Shakespierre.

    I wish there was a simnpler way to do this!!

    My bigger problem still is, I have some files called The 7 Habits – Stephen Covey (ie author name comes after book name) in the same folder as the other books. Yes, it’s a big mish mash and I would really love some tips on getting organised!!

    [I know about Calibre, but for some reason it is screwing up my Library:( )

  43. I haven’t done batch programming for a while and have a question.

    What it the use of the “2″ in the md line?

    @echo off
    for %%a in (*.*) do ( :: for each file in the directory do
    md “%%~na” 2>nul :: create a folder with the current filename no output??
    move “%%a” “%%~na” :: move the current file in the newly created folder
    )

    Thanks for the file, it’s very useful!

  44. Great, thanks! Never used this type of method before.
    Is there a quick way to create and name folders by the first word of file only? I have files named 2000s, 2001s, 2009s, 2010s….

  45. This is great! You saved me so much time and effort!

  46. Thank you very much for this. I am not very skilled in these matters, but I know that if you google just right you can find some Angel on High who has shared his or her gifts with the world. This did exactly what I wanted it to and thank goodness because I would have had to do it manually because I do not have the kung fu to write a batch script to do it.

    You rock.

  47. This worked great for me on Win7 Pro x64. Copied the text straight from your post. Saved me tons of time. Many thanks.

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

   

Sponsored Links:

Subscribe:

Enter email address:

© 2011 theHTPC.net Suffusion theme by Sayontan Sinha