File-to-folder script.

By | February 19, 2009


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

96 thoughts on “File-to-folder script.

  1. Bobo

    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..

    1. Jon Post author

      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. sam

      Thanks alot!!! Helped me so much was exactly what I was looking for and you saved me alot of time!

    1. Jon Post author

      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.

  2. MHealy

    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.

  3. MHealy

    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.

    1. Jon Post author

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

  4. Jason

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

  5. Colin pastuch

    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?

    1. thehtpc Post author

      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.

  6. Ben

    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!

  7. Bob

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

  8. Thomas Lewis

    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

  9. bbg454

    Bash script for the linux kiddies

    *use at your own risk*



    find $DIR -maxdepth 1 -type f | while read FILE
    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”

  10. Anoop

    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.

  11. Peter

    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.

  12. lance

    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.


  13. scoutfu

    Hi, nice script :)

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


    1. Jon Post author

      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.

  14. PChammer

    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!

  15. DK

    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?

    1. Jon Post author

      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.

      1. Michael F

        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.

        1. Michael F

          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.

          1. Jon Post author

            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.

          2. Michael F

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

          3. Jon Post author

            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.

          4. Michael F

            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.

  16. Matt

    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 :(

    1. Jon Post author

      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.

  17. MattWA

    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.


    find . -name '*.*' | \
    while read filename
          mkdir "${filename%.*}"
          mv "$filename" "$directory"/"$filename"
  18. MattWA

    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.

    1. Jon Post author

      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.

  19. kay_emi

    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.

  20. Richard

    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.


    1. Jon Post author

      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.

  21. nick

    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

  22. matt

    Just a note that


    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.

    1. Jon Post author

      Associated files are on the board as I learn. Hopefully. It won’t be a long wait.

  23. TCBW

    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.

  24. Alden

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

  25. Paul

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

  26. Mike

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

  27. Joe

    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!

    1. Joe

      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.

      1. Jon Post author

        .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 :)

  28. Wesley

    It seems like you are very knowledgeable when it comes to scripting, I was wondering if you knew how to do the following. Currently we have directories A-Z with no subfolders. Within the A directory there are files AAE, AE2, A23, ABF (Examples).
    We have a new folder structure where the file AAE should go to the folder A/A/E
    Do you have a script that could accomplish something like this or could you point me in the right direction?

  29. Ed

    Just wanted to comment that I copied my code from the gray box at the top of this page, pasted it into Notepad NOT Wordpad. Ran it and it works great. This little script just saved me hours.FYI – Notepad strips away any formatting.

  30. Netshark

    This worked great on my Win 7 x64 (UAC turned off).

    I have 2300 Half Life Deathmatch map screenshots that were not inside the map folders of the same name. so this script basically merged the jpgs into their already existing folders. Very nice.

    Example: I had a folder called maps. I
    Inside the maps folder I have folders called boot, boot1, and boot2.
    Also in the maps folder I have boot.jpg, boot1.jpg, and boot2.jpg and the batch file I copied and pasted from above.

    I ran the script and it merged those jpgs into their already existing folders nicely.

    Once the bat file is run though, it moves itself into it’s own folder. lol no big deal though.

    1. Jon Post author

      Read the rest of the post…change the attributes of the bat file to “hidden” and it won’t get moved :)

      1. Netshark

        Yes, I caught that after I posted.

        I altered it a little to make sub-directories to put them in, instead of the root. I have not written a batch file since the 1980’s so your info helped a lot.

        Thanks again!

  31. Hakon Gram

    Thank you so much for this script, saved me a whole lot of time putting movies into folders. Win 7 x64, no problems !

  32. Genie Bohn

    Absolutely did what I have been looking for, for over a year.

    I did not use it on a server, but on my pc with over 1400 files and it converted them an moved the files like a champ. Then they were moved to my website.

    Thank you

  33. Coenraad

    Thanks,this worked like a dream….Did more than 3000 movie files perfect.

    Am running Windows7 64bit Ultimate.Just followed your instructions and was done in like 2 minutes.

    Thanks again

  34. ArieS

    Thanks a bunch for the script but 1 little problem. I’m using that for XBMC and it moves everything to 1 folder (i.e nameofmovie.mkv, nameofmovie.tbn.nameofmovie.nfo) except nameofmovie-fanart. This one is moved to its own folder. Any way to avoid that? Can I add a line to your script to let say, ignore -fanart or something?

    1. Jon Post author

      I messed around with this a bit and am getting unexpected results. I’m sure I could figure it out if given time, but time is short as of late. I do have an alternate solution for you, however.

      Go to the root of the directory containing the files you want moved and perform a search using a specific string that will display only what you do NOT want moved (ex. *-fanart*). That example will show only files like movie-fanart.jpg. Select them all and choose Properties. Give them ALL the HIDDEN attribute. Re-run the script (it ignores hidden files, including itself if marked hidden). Perform the search again, remove the HIDDEN attribute on ALL files.

      Not pretty, but quick and easy.

      1. ArieS

        Thanks for the quick answer Jon.
        Doing your workaround, I would still have to move each individual -fanart file to its corresponding folder 1 by 1, so around 1250! A lot of work…
        I posted in a batch help forum and will report back if I get an answer.
        Thanks again for your script!

  35. ArieS

    Hello, I said I would report back in case I find the solution.
    Google brought me here the 1st time so maybe it can help someone else…
    Here’s the script that will move everything to 1 folder:

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


  36. MARIO

    Worked PERFECTLY! Saved me a lot of time on an office project I’m working on sorting .pdfs. THANK YOU VERY, VERY MUCH!!!! I’m running Windows 7 Professional 64.

  37. George

    many thinks for this handy wee script! works great!
    im only starting to organise my media for xbmc and this has worked brilliantly!

  38. Moondust

    I used the batch file and it worked perfectly. Hundreds of files are now in seperate folders. You saved me a huge amount of time! Thank you so much.

  39. Grosaty

    Thanks for this script.

    I’ve searched for years in various parts of the site and could not find it. This code works perfectly as I wanted, I’m testing on Windows XP and then I will prove it in Windows 8. A tip for those who do not work, they just have to write it or copy it exactly as written, with its lines and spaces. That’s it.

    Good luck and congratulations

  40. David Hill

    Amazing – worked on Windows 8 instantly – thanks so much!!

  41. Grosaty

    Hi, i was researching and change this script for two “for” and I did not understand the line that you put – 2>nul – thanks. Can you explain it does and how you can describe this part of command, this is a good trick.

    The lines below show the same effect for you wrote.

    @ echo off
    for%% a in (*. *) do (
    md “%% ~ na”

    for%% a in (*. *) do (
    move “%% a” “%% ~ na”

    These command lines work well on Windows XP, 7, 8

    Thank you for your script.

    I think – 2>nul – does the same as 2>nul, is this true?

    1. Jon Post author

      2>nul simply pipes any errors to the null device so they are not displayed.

  42. Tim

    works well !
    but what chance you could make an option to move all of the files with a similar name into a folder with that name?

    this would be awesome for when you download a whole bunch of episodes and need to put them into their own folder…i end up with long lists of files.

    1. Jon Post author

      That goes beyond the capabilities of a simple DOS script, I’m afraid.

  43. Pee Bee

    Love ya work.

    I used it across a network on a QNAP and it copied about 700 Movies in about 20 min with out a glitch

    Thank you

  44. JD Gaming And IT Solutions

    Thank you so much for that your program has made light work of over 300 invoices

  45. AXXXET


  46. Raj Satarkar

    Thanks for your valuable gui
    i have used its work perfectly moving and undo also
    great work keep it up!!!


  47. X


    Worked for me.

    Windows 7 Pro x64.
    2200 Movies.
    i copied the code off the page.
    No errors and happened immediately.

  48. Richard

    Perfect! Saved me hours and hours! Anyone having issue with this, well cannot see why you would! Simply move anything in a folder out of the directory or move only movies out to separate folder, run script, move back. Done!


  49. Ahmed Nasr

    you deserve alot of thanks ,,
    Really helped me in critical time

    thanks .. keep thinking & teaching us

  50. Joe

    Dude thank you so much!!! I am looking for this thing for a long time!!! Is it possible somehow to include this function in the right click – context menu? Thanks again!!!

  51. LazyThanks

    Absolutely amazing. been waiting on this find for years. and since i know nothing about code and contribute nothing to society in general, i appreciate the talented work of others!!!

  52. Words With Friends Cheat

    Thanks for this script. It worked perfect for me on 64 Bit Windows 7. I pasted the script from the post into a text file and renamed it test.bat since the download links to your RAR are both broken. I was a little leery after reading the other comments, so I copied my files over to a new folder before running it. It worked perfect the first time though! Saved me a ton of time. Thanks again for sharing!

  53. TEQGuy

    I wanted to move all the MyMovies meta data along with the ISO files. I modified this script to handle the specific files that are created by MyMovies.

    @echo off
    for %%a in (*.iso) do (
    md “%%~na” 2>nul
    move “%%a” “%%~na”
    move “%%~na.jpg” “%%~na”
    move “%%~na.file.jpg” “%%~na”
    move “%%~na.dvdid.xml” “%%~na”
    move “%%~na.mymovies.xml” “%%~na”
    move “%%~na.mymovies-back.jpg” “%%~na”
    move “%%~na.mymovies-front.jpg” “%%~na”

  54. Brett

    Dude, You rock so hard! Saved me HOURS! I’ve been using this code as a foundation to open doors to new things as well. You’re a wizard!

  55. mike

    PRECISELY WHAT WAS NEEDED!!! THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  56. vanarie

    Thanks, used it on a mapped network drive under Win7 and it worked like a charm. Just make sure any supporting files have the same name (extension doesn’t matter) and they’ll all be dumped into the same dir. Much appreciated and thanks for sharing.

  57. Ken

    Copied the text and ran in Windows 8.1 – worked perfectly for 700 files.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.